php教程

流程控制的替代语法

(PHP 4, PHP 5, PHP 7)

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;
?>

Note:

不支持在同一个控制块内混合使用两种语法。

Warning

switch 和第一个 case 之间的任何输出(含空格)将导致语法错误。例如,这样是无效的:

<?php switch ($foo): ?>
    <?php case 1?>
    ...
<?php endswitch ?>

而这样是有效的,因为 switch 之后的换行符被认为是结束标记 ?> 的一部分,所以在 switchcase 之间不能有任何输出:

<?php switch ($foo): ?>
<?php 
case 1?>
    ...
<?php endswitch ?>

更多例子参见 whileforif

User Contributed Notes

razvan_bc at yahoo dot com 29-Sep-2018 07:39
nice the first example(so i'll repeat again to understand what i want to say) :

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

but will not be nice if you combine all examples and to add
more lines codes ?
try to run this !

<?php

$a
=5;
(
$a==5 ) AND print<<<yepbaby
yeah baby !!! php roolez !!!
yepbaby;

?>
jcmarchi at gmail dot com 31-Aug-2017 04:24
After a long and useless discussion at https://bugs.php.net/bug.php?id=75138, while trying to place a bug report for this problem, I came to a sad conclusion that PHP developers nowadays rather live with the code syntax problem instead of fixing it, then blame issues to the developers' code style.

Well... I can accept it as not being a "BUG", as far as a "NEW SYNTAX requirement" is explicitly defined for it (rules are rules).

I'd suggest a modification in the language manual in regards to semicolon usage definition:

====================
When using "alternative syntax for control structures" observe the fact that when an "IF" statement is required to be nested into another IF-ELSEIF-ELSE block and it casually ends up being the last statement of the nested block, it is a LANGUAGE REQUIREMENT to terminate it with double semicolon (;;) to prevent the "dangling else" effect.
====================

Or, better, we can add it to the same (miss)interpretation concept that aread exist in this very same page, keeping documentation standards intact:

====================
Note:
Mixing syntaxes in the same control block is not supported.

Note:
A nested "if" statement immediately before an "else:" or "elseif: " statement in control block is not supported.
====================

Oh, that solves all problems, sure!!!!

Or, even better! Let's simply ignore it all and leave it as it is. What harm can an extra ";" after another ";" do anyway, right?

It is always developers' fault anyway... Poor bastards that don't know how to code! I can't believe anyone will have an IF statement as the last nested statement inside another IF... Can you imagine it? :P
Julio Marchi 30-Aug-2017 03:16
Consider the following hypothetical PHP example:

<?php
$bar
= 'bar';
$foo = 'foo';

if (isset(
$bar)):
   if (isset(
$foo)) echo "Both are set.";
elseif (isset(
$foo)):
   echo
"Only 'foo' is set.";
else:
   echo
"Only 'bar' is set.";
endif;
?>

Disconsider the dumb logic and focus on the elseif line. If you try it yourself you will get a PHP EXCEPTION error saying: syntax error, unexpected ':' .

Now, you may think the fix is to have the sub-if enclosed in between { } instead of being a single line statement, like this:

<?php
$foo
= 'foo';
$bar = 'bar';

if (isset(
$bar)):
   if (isset(
$foo)) {
     echo
"Both are set.";
   }
elseif (isset(
$foo)):
   echo
"Only 'foo' is set.";
else:
   echo
"Only 'bar' is set.";
endif;
?>

Wrong! The error remains. Exactly the same EXCEPTION as before...
   
Well, here is what I found: if you put a semicolon (;) AFTER the curly bracket (}) which resides immediately before the elseif statement, then the error is gone! Try it:

<?php
$foo
= 'foo';
$bar = 'bar';

if (isset(
$bar)):
   if (isset(
$foo)) {
     echo
"Both are set.";
   };
elseif (isset(
$foo)):
   echo
"Only 'foo' is set.";
else:
   echo
"Only 'bar' is set.";
endif;
?>

Weird enough, if you go back to the first example and DOUBLE the semicolon immediately before the elseif statement, it will also work:

<?php
$foo
= 'foo';
$bar = 'bar';

if (isset(
$bar)):
  if (isset(
$foo)) echo "Both are set.";;
elseif (isset(
$foo)):
  echo
"Only 'foo' is set.";
else:
  echo
"Only 'bar' is set.";
endif;
?>

But, it doesn't end there. You can also do this:

<?php
$foo
= 'foo';
$bar = 'bar';

if (isset(
$bar)):
  if (isset(
$foo)): echo "Both are set.";
elseif (isset(
$foo)):
  echo
"Only 'foo' is set.";
else:
  echo
"Only 'bar' is set.";
endif;
?>

However, in this last example, the logic gets totally scrambled! The elseif will now belong to the sub-if instead of the first if, and the rest of the logic will all behave as a "one single statement" in response to the first if only. Very confusing and error prone (be careful).

The differences are very subtle and can deceive the eyes (especially while debugging). For this reason, I strongly suggest the first example from this answer: when using IF-ELSEIF blocks (AKA "Alternative Syntax"), if another IF is required inside it, enclose it in between {} and don't forget to add a semicolon after the last }. Example:

<?php
if (isset($bar)):
   if (isset(
$foo)) {
     echo
"Both are set.";
   };
elseif (...):
?>

Maybe the truth is that someone screwed up in the language parsing process for those PHP Block Alternative Statements or failed to document this very important detail!
howeson dot h at gmail dot com 21-Mar-2017 01:45
The following if statement works if you want to do a shorthand if statement and would rather use echo (or any other function) instead of print:

<?php
$value
= 'Jesus';

echo isset(
$value) ? $value : ''; // Would return 'Jesus'
?>

The issue with this is that you must also declare a value to be printed if the statement returns false (after the colon).

An example of an if else statement would be:

<?php
echo isset($value) ? $value : 'Value is not set'; // Would return 'Value is not set'

$value = 'Jesus';

echo isset(
$value) ? $value : 'Value is not set'; // Would return 'Jesus'
?>
v14t at gmx dot com 06-Apr-2014 02:14
isset( $value ) AND print( $value );

the reason why it doesn't work with echo, it's because echo does not return anything, while print _always_ returns 1, which is considered true in the expression
josh at qaribou dot com 05-Mar-2014 10:17
The reason temec987's approach of using boolean operators as an alternative to control structures won't work for an 'echo' is because the result of evaluating the expression will always be a boolean.

Other languages (e.g. ruby) are much better suited to this approach, as the expression evaluated will be the resultant value, e.g.:
5 && 4

In ruby, this would be 4, but in PHP, this would be true (type-juggled equivalent is 1), which isn't useful for anything but further binary logic.

You can still use logical operators as conditionals, but only for executing logic, not for getting a value back, e.g.:
<?php
defined
('USER_CAN_EXECUTE') or die('Access denied.');
?>

is a nice one to use for access control, or say you want to put in a quick check that your object has all the data loaded it needs to call a webservice (functions are just examples):
<?php
$this
->readyForService() and $this->postData('http://endpoint.com');
?>

What you can't use them for is something like this:
<?php
echo (strlen($mystring) > 5) and $mystring;
?>

instead, you'd use ternaries for that:
<?php
echo (strlen($mystring) > 5) ? $mystring : null;
?>
timeroot dot alex at gmail dot com 16-Dec-2013 12:54
The reason for the "workaround" jeremiah mentioned, in the case of the switch statement, can be understood as follows; in any place where you can have an echo statement (an if block, a switch's case, whatever), that's where you can have the raw HTML. In PHP this basically gets handled just like that -- like an echo statement.

In between a switch and a case, though, you can't echo anything. By placing the switch and the case in two separate blocks of PHP, with a raw HTML newline echo'ed in between them, PHP basically had to try to find where that statement would be. And it can't be there, hence the difficulty.
temec987 at gmail dot com 02-Oct-2011 07:00
A simple alternative to an if statement, which is almost like a ternary operator, is the use of AND. Consider the following:

<?php
     $value
= 'Jesus';

    
// This is a simple if statement
    
if( isset( $value ) )
     {
          print
$value;
     }

     print
'<br />';

    
// This is an alternative
    
isset( $value ) AND print( $value );
?>

This does not work with echo() for some reason. I find this extremely useful!
flyingmana 26-Mar-2009 01: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
jeremia at gmx dot at 28-Jan-2008 06: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>
skippy at zuavra dot net 27-Jun-2005 04: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.

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788