downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

while> <elseif
Last updated: Sun, 25 Nov 2007

view this page in

流程控制的替代语法

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

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

在上面的例子中,HTML 内容“A is equal to 5”用替代语法嵌套在 if 语句中。该 HTML 的内容仅在 $a 等于 5 时显示。

替代语法同样可以用在 elseelseif 中。下面是一个包括 elseifelseif 结构用替代语法格式写的例子:

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

更多例子参见 whileforif



while> <elseif
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
流程控制的替代语法
ej at iconcept dot fo
28-May-2009 08:30
if statement in 1 line
<?php
$hour
= 11;

print
$foo = ($hour < 12) ? "Good morning!" : "Good afternoon!";

?>
return Good morning!
flyingmana
26-Mar-2009 05:43
It seems to me, that many people think that

<?php if ($a == 5): ?>
A ist gleich 5
<?php endif; ?>

is only with alternate syntax possible, but

<?php if ($a == 5){ ?>
A ist gleich 5
<?php }; ?>

is also possible.

alternate syntax makes the code only clearer and easyer to read
SM
04-Feb-2009 09:12
The control structure should be like in BASIC languages:
<?php
if ($a == 12):
  echo
"a is 12\n";
end if;

while (
true):
  echo
"loop loop loop\n";
end while;
?>

or just use end operator like in Ruby
<?php
if ($a == 12):
  echo
"a is 12\n";
end;

while (
true):
  echo
"loop loop loop\n";
end;
?>
mido_alone2001 at yahoo dot com
07-Nov-2008 10:05
Hello , when you going to make a script , you must try easist way to do and fastest way to parse ..
using alternative-syntax is very useful to shorten your code
e.g :
If you want to do:

<?php
    $a
=1 ;
if (
$a==1) {
     echo
"<table border=1><tr><td>$a is equal to one    </td></tr></table> " ;
}
?>

You can do it using alternative-syntax as following :

<?php
    $a
=1 ;
if (
$a==1) :?>
<table border=1><tr><td><?echo $a ;?> &nbsp;is equal to one </td></tr></table>
<?php endif ; ?>

So the HTML code Won't excuted until the condition is true

[EDIT BY danbrown AT php DOT net: Contains a bug fix provided by (gmdebby AT gmail DOT com).]
jeremia at gmx dot at
28-Jan-2008 10:52
If you wan't to use the alternative syntax for switch statements this won't work:

<div>
<?php switch($variable): ?>
<?php
case 1: ?>
<div>
Newspage
</div>
<?php break;?>
<?php
case 2: ?>
</div>
Forum
<div>
<?php break;?>
<?php
endswitch;?>
</div>

Instead you have to workaround like this:

<div>
<?php switch($variable):
case
1: ?>
<div>
Newspage
</div>
<?php break;?>
<?php
case 2: ?>
</div>
Forum
<div>
<?php break;?>
<?php
endswitch;?>
</div>
spa
17-Oct-2007 07:40
[EDITOR'S NOTE: reference to deleted note removed]

The end_; structure sometimes makes it easier to tell which block statement end you are looking at.  It's much harder to tell which nested block a } belongs to than an end_;
skippy at zuavra dot net
27-Jun-2005 07:32
If it needs saying, this alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them.

Interface templates are very often in need of this, especially since the PHP code in them is usually written by one person (who is more of a programmer) and the HTML gets modified by another person (who is more of a web designer). Clear separation in such cases is extremely useful.

See the default templates that come with WordPress 1.5+ (www.wordpress.org) for practical and smart examples of this alternative syntax.

while> <elseif
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites