.::: 一路问情 :::.
  • 注册
  • 登录
  • 会员
  • 生存游戏
  • FTP资源中心
  • 七海争霸
  • 社区设施
  • 帮助
.::: 一路问情 :::. » 『建站技术』 » 流程控制的替代语法
‹‹ 上一主题 | 下一主题 ››
发新话题
  • 发新话题
  • 发布投票
  • 发布商品
  • 发布悬赏
  • 发布活动
  • 发布辩论
  • 发布视频
打印

[程序] 流程控制的替代语法

ivanisme
看贴不回的后果

初级会员

帖子
83 
威望
30 点 
金钱
201 元 
好评
0 点 
  • 个人空间
  • 发短消息
  • 加为好友
  • 当前离线
1楼 大 中 小 发表于 2008-2-26 16:55  只看该作者

流程控制的替代语法

PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch。替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,endwhile;,endfor;,endforeach; 以及 endswitch;。

―――― PHP代码 ――――
   
   <?php if ($a == 5): ?>
   A is equal to 5
   <?php endif; ?>

――――――――――――

在上面的例子中,HTML 内容“A is equal to 5”用替代语法嵌套在 if 语句中。该 HTML 的内容仅在 $a 等与 5 时显示。
替代语法同样可以用在 else 和 elseif 中。下面是一个包括 elseif 和 else 的 if 结构用替代语法格式写的例子:

―――― PHP代码 ――――
<?php   
   if ($a == 5):
    print "a equals 5";
    print "...";
   elseif ($a == 6):
    print "a equals 6";
    print "!!!";
   else:
    print "a is neither 5 nor 6";
   endif;
?>

――――――――――――

更多例子参见 while,for 和 if。
User Contributed Notes
流程控制的替代语法
ssttoo at hotmail dot com
06-Dec-2003 06:20

Nested ternary operators can be used, although not a "good programming practice", as it does not promote readability and is prone to errors.

―――― PHP代码 ――――
  
   <?php
   $one = true;
   $two = true;
   $result = ($one) ? "one" : (($two) ? "two" : "none"); // $result is "one"
   $result = ($one) ? "one" : ($two) ? "two" : "none"; // $result is "two"
   ?>
   
――――――――――――

pemga at freemail dot hu
20-Nov-2003 06:11

My personal preference on alternating writing is like this:

$query="select * from books";
sizeof($myparam) and $query.=" where title like '%$myparam%'";

Which means: if (sizeof($myparam)) $query.= ...

You can even combine this with the ? structure.
Like:

$query="select * from books";
strlen($title) and $c1=" title like '%$title%'";
strlen($year) and $c2=" year=$year";

strlen($c1) and $q2=$c1;
strlen($c2) and $q2.= (strlen($q2)) ? "and $c2" : $c2;

strlen($q2) and $query.=" where $q2";

Well, how about this? Can it be done easier? :)
I think is is called lazy Vs eager calc.


StarLight pl.net.solutions@slt
06-Nov-2003 12:59

In response to justin at iqmail dot net note, the parse error is because of lazy coding and has nothing to do with mixing syntax ;-). You should put a semicolon at the end of the second multi-line if. This works and didn't bark (tested on php 4.0.3pl1):D

<?php
if (condition):
// code
if (another_condition) {
// multi-line code
}; //notice the semicolon
else:
?>
[html here]
<?php
endif;
?>

best regards,
StarLight


i a m 4 w e b w o r k at hotmail dot com
13-Oct-2003 04:38

Good tutorial on using alternative control structure syntax at:
http://www.onlamp.com/pub/a/php/ ... dations.html?page=1


paul at example dot com
06-Sep-2003 06:27

There is an other alternative syntax:

<?php
if ($a > 5) {
echo "big";
} else {
echo "small";
}
?>

can be replaced by:

<?php
echo $a > 5 ? "big" : "small";
?>


matheo at step dot polymtl dot ca
05-Sep-2003 04:37

You can use a short syntax for "if"
$a = (condition) ? value if condition is true : value if condition is false;

<?php
$a = 1 ;
$b = 2 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo $a_is_bigger ;
# returns 0

$a = 2 ;
$b = 1 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo $is_a_bigger ;
# returns 1

?>


christopher dot murtaghNO at mcgill dot SPAMca
16-May-2003 12:19

A comment to the above:

You shouldn't need to change your code syntax because of nested flow control statements. Instead you should strongly think about your coding style and getting a better text editor. For example, indent your structures and sub structures:

if($foo){
if($Bar){
<some code>
}
else{
<something else>
}
}
else{
for($i=0;$i<$foo;$i++){
<do something>
}
}

Also, any text editor worth using for coding (emacs, vi(m), BBEdit, Elvis, etc..) should do bracket balancing in case you are really in deep.


frank at vista dot com
25-Jan-2002 03:26

The alternative syntax is useful for situations where other systems would use templates:

OCIDefineByName($stmt, 'NAME', &$name);
OCIDefineByName($stmt, 'SALARY', &$salary);

OCIExecute($stmt);
?>
<!-- later, maybe in an included file -->
<table>
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<? while(OCIFetch($stmt)): ?>
<tr>
<td><? echo $name ?></td>
<td><? echo $salary ?></td>
</tr>
<? endwhile; ?>
</table>


justin at iqmail dot net
30-Dec-2000 04:03

This is also relevant when mixing styles and interspersing PHP with HTML. The following example gives a syntax error since it associates the else with the inner if.

<?php
if (condition):
// code
if (another_condition) {
// multi-line code
}
else:
?>
[html here]
<?php
endif;
?>

A way around this is to not mix syntax, and change the inner if to the alternate colon-based syntax.



QQ
UID
73836 
精华
0 
积分
30 
钻石
0 克拉 
宣传
0 点 
来路
搜索引擎 
阅读权限
10 
来自
上海 
在线时间
0 小时 
注册时间
2008-2-25 
最后登录
2008-2-26 

查看详细资料

TOP

‹‹ 上一主题 | 下一主题 ››
发新话题
 

 

当前时区 GMT+8, 现在时间是 2008-7-21 08:03 蜀ICP备08000616号

清除 Cookies - 联系我们 - Ask Love - Archiver - WAP - TOP - 界面风格

  • YUYU_Emera-x

Powered by Discuz! 6.0.0,

Processed in 0.028538 second(s), 5 queries, Gzip enabled.

  • 控制面板首页
  • 编辑个人资料
  • 积分交易
  • 积分记录
  • 公众用户组
  • 个人空间管理
  • 资源中心
  • 名人堂
  • 社区银行
  • 醒目高亮
  • 勋章中心
  • 空间互踩
  • 便民查询
  • 邮箱图标
点击查看问情在alexa中的世界排名