流程控制的替代语法

字体大小: 中小 标准 ->行高大小: 标准
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/2001/05/03/php_foundations.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.

此文章由 www.phpgz.com 收集整理 ,地址为: http://www.phpgz.com/htmls/1141.html

大屏阅读,大屏评论.