Home > Backend Development > PHP Tutorial > Alternative syntax for php flow control

Alternative syntax for php flow control

伊谢尔伦
Release: 2016-11-24 09:27:11
Original
1553 people have browsed it

PHP provides some alternative syntax for flow control, including if, while, for, foreach and switch. The basic form of the alternative syntax is to replace the left curly brace ({) with a colon (:), and replace the right curly brace (}) with endif;, endwhile;, endfor;, endforeach; and endswitch; respectively.

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

In the example above, the HTML content "A is equal to 5" is nested within an if statement using alternative syntax. The content of this HTML is only displayed when $a is equal to 5.

Alternative syntax can also be used in else and elseif. Here is an example of an if structure including elseif and else written in an alternative syntax format:

<?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;
?>
Copy after login

Note:

does not support mixing the two syntaxes within the same control block.


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template