PHP traffic control statement substitution syntax _ PHP Tutorial

Source: Internet
Author: User
The alternative syntax of the PHP traffic control statement. PHP traffic control statement substitution syntax 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 brackets (alternative syntax of the PHP traffic control statement

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

Elseif and else if are considered identical only when curly brackets are used. If you use a colon to define the if/elseif condition, you cannot use two words of else if. otherwise, PHP will produce a parsing error.

The foreach syntax structure provides a simple way to traverse arrays.Foreach can only be applied to arrays and objectsIf you try to apply it to a variable of another data type or an uninitialized variable, an error message is sent. Because foreach depends on the internal array pointer, modifying its value in the loop may lead to unexpected behavior.

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

Require and include are almost the same, except for different methods of handling failures. Require generates an E_COMPILE_ERROR level error when an error occurs. In other words, this will cause the script to stop and include will only generate warnings (E_WARNING), and the script will continue to run. There are require_once and include_once.

The contained file is first searched based on the path given by the parameter. If no directory is provided (only the file name), it is searched based on the directory specified by include_path. If this file is not found in include_path, include is finally located in the directory where the script file is called and in the current working directory. If the file is not found at the end, the include structure will issue a warning; this is different from require, the latter will issue a fatal error.
If the path is defined, whether it is an absolute path (starting with a drive letter or a drive letter in Windows, starting with/in Unix/Linux) or a relative path of the current directory (starting. or ..) -- include_path will be completely ignored. For example, if a file starts with ../, the parser searches for the file in the parent directory of the current directory.

When a file is included, the code contained in the file inherits the variable range of the row where the include is located. From this point on, any variables available to the calling file in this row are also available in the called file. However, all functions and classes defined in the inclusion file have a global scope.

vars.php
 test.php
 
 
 A is equal to 5
  
 $ 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 ($ 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 of the last element of the array is referenced in It will be retained after the foreach loop. We recommend that you use unset () to destroy it. 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;?>



Alternative syntax for http://www.bkjia.com/PHPjc/1025014.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1025014.htmlTechArticlePHP traffic control statements PHP provides alternative syntax for some flow control, including if, while, for, foreach and switch. The basic form of the alternative syntax is to enclose the Left curly braces (...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.