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

Alternative syntax for PHP flow control statements_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:48:011168browse

Alternative syntax for PHP flow control statements

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. .

elseif and else if are considered to be exactly the same only if they use similar braces. If you use a colon to define an if/elseif condition, you cannot use a two-word else if, otherwise PHP will generate a parsing error.

The foreach syntax structure provides a simple way to traverse an array. foreach can only be applied to arrays and objects. If you try to apply it to variables of other data types, or uninitialized variables, an error message will be issued. Because foreach relies on an internal array pointer, modifying its value inside a loop may cause unexpected behavior.

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

require and include are almost identical, except for the way failure is handled. require generates an E_COMPILE_ERROR level error on error. In other words, it will cause the script to abort and include will only generate a warning (E_WARNING), and the script will continue to run. The corresponding ones are require_once and include_once.

The included file is first searched according to the path given by the parameter. If no directory is given (only the file name), it is searched according to the directory specified by include_path. If the file is not found under include_path, include will finally search in the directory where the calling script file is located and the current working directory. The include construct emits a warning if the file is not found at the end; this is different from require, which emits a fatal error.
If a path is defined - whether absolute (starting with a drive letter or on Windows, / on Unix/Linux) or relative to the current directory (starting with . or .. ) - include_path will be completely ignored. . For example, if a file starts with ../, the parser will look for the file in the parent directory of the current directory.

When a file is included, the code contained in it inherits the variable scope of the include line. From that point on, any variables available in the calling file at that line are also available in the called file. However, all functions and classes defined in include files have global scope.

vars.php
<!--?php

$color = &#39;green&#39;;
$fruit = &#39;apple&#39;;

?-->

test.php
<!--?php

echo A $color $fruit; // A

include &#39;vars.php&#39;;

echo A $color $fruit; // A green apple

?-->
<!--HTML 内容A is equal to 5用替代语法嵌套在 if 语句中。该 HTML 的内容仅在 $a 等于 5 时显示-->
<!--?php if ($a == 5): ?-->
A is equal to 5
<!--?php endif; ?--> 

<!--?php
    // elseif 与 else if 只有在类似上例中使用花括号的情况下才认为是完全相同。
    // 如果用冒号来定义if/elseif 条件,那就不能用两个单词的 else if,否则 PHP 会产生解析错误。 
    if($a --> $b):
        echo a is bigger than b;
        echo ...;
    elseif($a == $b):
        echo a is equals b;
    else:
        echo a is smaller than b;
    endif;
    
    // While - do-while(0)
    $i = 1;
    while($i < 10):
        echo $i++. ;
    endwhile;
    
    // for
    for ($i = 10; $i <= 11; $i++):
        echo $i. .
;
    endfor;
      
    // foreach
    $arr = array(1, 2, 3, 4, 5);     
    foreach($arr as $key => & $value):
        $value += 2;
        echo Key1: $key => Value: $value.
;
    endforeach;      

    // 数组最后一个元素的 $val 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。
    unset($value); 
    
    reset($arr);
    while (list($key, $value) = each($arr)):
        echo Key2: $key => Value: $value.
;
    endwhile;
    
    // switch - goto
    $i = 1;
    switch ($i):
    case 0:
        echo i equals 0;
        break;
    case 1:
        echo i equals 1;
        break;
    case 2:
        echo i equals 2;
        break;
    endswitch;    
?>



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1025014.htmlTechArticlePHP Alternative syntax for flow control statements PHP provides some alternative syntax for flow control, including if, while, for, foreach and switch. The basic form of the alternative syntax is to put the left curly bracket (...
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