Home  >  Article  >  Backend Development  >  Detailed explanation of alternative syntax in PHP

Detailed explanation of alternative syntax in PHP

墨辰丷
墨辰丷Original
2018-05-15 17:22:301841browse

This article mainly introduces the detailed explanation of alternative syntax in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

  • What is alternative syntax?
    Simply put, they are some alternative ways of writing grammar.

#2. What syntaxes have alternative syntaxes in PHP?

if, while, for, forforeach, switchThese flow control statements have alternative syntax.


3. Basic form of alternative syntax:

The left curly brace ({) is replaced by a colon (:), and the right The curly braces (}) are replaced with endif;, endwhile;, endfor;, endforeach; and endswitch;


Usage:

if-endif usage:

<?php if ($a<0): ?> 
    <li>if里面的输出</li> 
<?php endif; ?> 
    上面的语句等同于 
<?php if ($a<0){ ?> 
    if里面的输出
<?php } ?>

while-endwhile usage:

<?php while (expr): ?> 
    <li>循环while里面的输出</li> 
<?php endwhile; ?>

for-endfor usage:

<?php foreach (expr1): ?> 
    <li>循环for里面的输出</li> 
<?php endforeach; ?>

switch-endswitch usage:

<?php 
switch ($i): 
    case 1: 
        echo "i 等于 1"; 
        break; 
    case 2: 
        echo "i 等于 2"; 
        break; 
    default: 
        echo "i 不等于1和2"; 
endswitch; 
?>

Related recommendations:

php alternative syntax

Introduction to alternative syntax in PHP, introduction to PHP alternative syntax_PHP tutorial

##Introduction to alternative syntax in PHP, introduction to PHP alternative syntax

The above is the detailed content of Detailed explanation of alternative syntax in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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