Process Control in PHP and PHP Process Control

Source: Internet
Author: User

Process Control in PHP and PHP Process Control
If

if () {    statement;} else if {    statement;} elseif {    statement;} else {    statement;}

else ifAndelseifThe meaning of syntactic analysis is slightly different, but the bottom line is that the two will produce the same behavior.

Loop
while () {    statement;}do {    statement;} while();for (;;) {    statement;}foreach (as) {    statement;}                   
Break and continue

An optional numeric parameter can be used to determine the number of iterations

Switch
switch () {    case :        statement;    default:        statement;}

Allow the use of semicolons instead of the colon after the case statement

switch ($beer) {    case 'tuborg';    case 'carlsberg';    case 'heineken';        echo 'Good choice';    break;    default;        echo 'Please make a new selection...';    break;}
Declare

The declare structure is used to set the execution instructions for a piece of code.

Currently, only two commands are known: ticks and encoding.

Ticks

Tick (clock cycle) is an event that occurs when the interpreter executes N low-level statements that can be timed in the declare code segment. The value of N is specified by ticks = N in the directive section of declare.

Not all statements can be timing. Generally, conditional expressions and parameter expressions do not support timing.

Tick indicates an event. The event is defined in register_tick_function, and the event execution frequency is (ticks = n ).

Purpose. An event is triggered every time several statements are executed, for example, an event is recorded. This will check the execution speed of php code in declare. You can find the location of the code segment where the slow part occurs.

<?php    function doTicks ()    {        echo 'Ticks';    }    register_tick_function('doTicks');    declare(ticks = 1) {        for ($x = 1; $x < 10; ++ $x) {            echo $x * $x . '<br />';        }    }?>

Calculation Result:

1TicksTicks4TicksTicks9TicksTicks16TicksTicks25TicksTicks36TicksTicks49TicksTicks64TicksTicks81TicksTicksTicksTicks

First, the complete for loop is regarded as a statement, but it must wait until the loop ends. Therefore, the echo in the for loop is regarded as the first statement during compilation.
Therefore, the first doTicks is executed after the first echo, that is, the first tick event occurs only after 1 output.
In In a loop from 1 to 9, each loop contains two statements, one echo and one for loop. After 81 is output, because echo is a statement, the first ticks is output. At the same time The for loop of x = 9 is also over. This is another statement, and the second ticks is output; the loop of $ x = 10 is started, but the cycle condition is no longer met, the execution of the for loop ends. This loop is a statement, and the third ticks is output.
Finally, declare is itself considered a statement, so the fourth ticks is output.

Encoding

You can use the encoding command to specify the encoding method for each script.

<?phpdeclare(encoding='ISO-8859-1');// code here?>
Reutrnrequire, include, require_once, include_once

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, it will cause the script to stop and include will only generate a warning (E_WARNING), and the script will continue to run.

Goto

The goto operator can be used to jump to another position in the program. The target location can be marked by the target name with a colon, while the jump command is the tag of the target location after the goto.

The goto in PHP has certain restrictions. The target location can only be in the same file and scope. That is to say, you cannot jump out of one function or class method, or jump into another function. And cannot jump into any loop or switch structure. You can jump out of a loop or switch. Generally, you can use goto to replace multiple levels of break.

<?phpgoto a;echo 'Foo';a:echo 'Bar';?>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.