On the process control and its implementation in the speech of PHP

Source: Internet
Author: User
Tags emit readfile unpack vars
The script consists of statements, which are implemented by Process control, and this section mainly describes the use of several keywords.

ElseIf

elseifAnd else if The behavior is exactly the same, if you use a colon to define the if/elseif condition, then you can not use two words else if , or PHP will produce parsing errors.


   
    $a = 1;$b = 2;if($a > $b) :echo"$a is greater than $b";elseif($a == $b) :echo"$a equals $b";else :echo"$a is neither greater than or equal to $b";endif;

Alternative syntax

The following keywords can use the alternative syntax for process control, with the basic form of replacing the left curly brace with a colon and closing curly brackets for the following characters.

ifendifwhileendwhileforendforforeachendforeachswitchendswitch

Attention! PHP does not support mixing of two syntaxes within the same control block.

Do-while

The loop looks familiar at first glance, but ignores some of its usage.

The handbook says that senior C users may be familiar with another different do-while circular usage, putting statements in do-while(0) . I have heard of this technique for the first time, it seems I am still a C language small white.

By the way, search do-while(0) for the benefits of this particular usage.

    1. Code chunking is more intuitive than just using curly braces.
    2. Use break skipping over the remaining piece of code.
    3. Useful for macro definition functions, using the end of the sentence can be a semicolon, looks more like a function call.
    4. Block-level scopes, which prevent variable names from spreading to the upper scope.
    5. The deformed goto statement.

This post is very good, do{}while (0) Role-C + +-Segmentfault.

Foreach

foreachCan only be applied to the traversal of arrays and objects. The foreach syntax structure provides a simple way to iterate through an array, with the following two syntaxes.

foreachas$value)statementforeachas$key$value)statement

To modify the value of an array element, you need to use a reference assignment, which is implemented by the $value preceding addition & .


   
    $arr = array(1, 2, 3, 4);foreach($arras &$value) {$value = $value * 2;}unset($value);foreach($arras$value) {echo"$value ";  // 2 4 6 8}

Attention! The reference to the last element of the array $value foreach remains after the loop, and unset() it is recommended to use it for destruction.

List-each

In the example program, a special traversal method is also found, which is called list-each .

When foreach execution begins, the pointer inside the array automatically points to the first cell, so it does not need to be foreach called before the loop reset() . whilein the case of list-each , however, the inner pointer of the array $arr is always there, so it needs to be before the next loop reset($arr) .


   
    $arr = array('one', 'two', 'three');// reset($arr);while(list($key, $value) = each($arr)) {echo"Key: $key; Value: $value ";}reset($arr);while(list($key, $value) = each($arr)) {echo"Key: $key; Value: $value ";}

In the above code, the first one reset can be omitted, but the second one reset cannot be saved.

List

PHP 5.5 adds the ability to iterate through an array of arrays and unpack the nested array into the loop variable.


   
    $array = [[1, 2],[3, 4],];foreach($arrayaslist($a, $b)) {echo"A: $a; B: $b";}

list()The cells in the cell can be less than the nested arrays, and the extra array cells are ignored. If more than, an error message will be issued.

Break

breakUsed to end for/foreach/while/do-while/switch the execution of the current structure. breakyou can accept an optional numeric parameter to decide to jump out of a few loops, but the argument cannot be a variable.

breakJump out of the multi-loop or the first encounter, deliberately wrote a small program to try a bit.


   
    while(1) {while(1) {echo'hello ';break2;}}echo'world';

Specially in C language to try a bit, prompt syntax error.

Continue

breakSimilarly, continue you can also accept an optional numeric parameter to decide to skip a few loops to the end of the loop.

Attention! Statements in PHP are switch considered to be continue a looping structure that can be used.

Switch

The manual says that PHP differs from other languages in that the function continue of the statement switch is similar break . What does that mean?

switch/caseDoing is loosely compared == , not strictly compared === . In terms of efficiency, the switch conditions in the statement are only one time and are used to compare with each case statement. An case expression can be any expression that evaluates to a simple type and cannot be used with an array or an object. Semicolons case are allowed in place of the statement after a semicolon is used.

Declare

declareThe structure is used to set the execution instruction of a piece of code, the syntax structure is as follows:

declare(directive)statement

directivesection allows declare to set the code snippet behavior, currently only two commands are known: ticks and encoding . declarestructs can also be used for global scope, affecting all code after expiration. However, if a declare structured file is contained by another file, it does not work for the parent file that contains it.

Tick (clock cycle) is an event that occurs in a declare code snippet when an interpreter executes n a low-level statement that can be timed. The events that appear in each tick are register_tick_function() specified by the. The usage is broadly as follows.

declare1);functiontick_handler(){echo"tick_hander() called.\n";}register_tick_function('tick_hander');

There are a number of low-level statements that can be timed, a periodic event is register_tick_function() invoked after each statement, and a periodic event is invoked at the end of the curly brace.

Note that the expressions in PHP cannot be separated by commas, or syntax errors will occur. This is different from the C language, just noticed.

You can use the encoding directive to specify how each script is encoded. Use the following:

declare'ISO-8859-1);

Return

If it is called at the global scope, the current script file aborts the run. If the current script file is include either or require , control is returned to the calling file. If the current script is being used include , then return the value is treated as the include return value of the call, then require ?

Require

requireAnd include almost exactly the same, in addition to handling the failure in a different way.

requireThe script aborts when a level error occurs on an error E_COMPILE_ERROR . Instead include E_WARNING , only a warning is generated and the script continues to execute.

Include

includeStatement to include and run the specified file, note the search order for the specified file.

    • The included files are searched first by the path given by the parameters. If a path is defined, include_path it is completely ignored.
    • If no directory (only file name) is given, it is searched according to include_path the specified directory. If you do not find it, look in the directory where the script file is located and the current working directory. So the question is, what is the difference between the directory where the script file is called and the current working directory?
    • If the file is not finally found, the include structure emits a warning that the require structure will emit a fatal error.

When a file is contained, it contains code that inherits the include range of variables in the row. From there, the variables defined in the called file can be used in the calling file. When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode, replying to the end of the file.

For the return value, include returns and warns when a failure occurs FALSE . A successful inclusion returns 1 unless the return value is also given in the included file. If a function is defined in the include file, these functions can be return used independently of the master file, either before or after it is defined.

If the files from the remote server should run at the far end and only output the results, use the readfile() function better. Another way to include a PHP file in a variable is to capture its output with an output control function include . The first time encountered, more unfamiliar. The following code can vars.php output the content returned in the script.


   
    $string = get_include_contents('vars.php');functionget_include_contents($filename){if(is_file($filename)) {ob_start();include$filename;$contents = ob_get_contents();ob_end_clean();return$contents;}returnfalse;}echo$string;

Because include and require is a special language structure, its parameters do not need parentheses. If the file is included two times, PHP will make a fatal error because the function is already defined. Recommended Use include_once .

Require_once

require_onceStatements and require statements are identical, the only difference being that PHP checks if the file has already been included, and if it does, it will not be included again.

Include_once

include_onceStatements and include statements are similar, the only difference is that if the file has already been included, it will not be included again.

Goto

gotoThe operator is used to jump to another location of the program, and the target location can be marked with a colon for the target name. gotoThere are certain restrictions in PHP where the target location can only be in the same file and scope. That is, you cannot jump out of a function or class method, or you can skip into any loop or switch structure.

(End of full text)

On the process control and its implementation in the speech of PHP

The script consists of statements, which are implemented by Process control, and this section mainly describes the use of several keywords.

ElseIf

elseifAnd else if The behavior is exactly the same, if you use a colon to define the if/elseif condition, then you can not use two words else if , or PHP will produce parsing errors.


   
     $a = 1;$b = 2;if($a > $b) :echo"$a is greater than $b";elseif($a == $b) :echo"$a equals $b";else :echo"$a is neither greater than or equal to $b";endif;

Alternative syntax

The following keywords can use the alternative syntax for process control, with the basic form of replacing the left curly brace with a colon and closing curly brackets for the following characters.

ifendifwhileendwhileforendforforeachendforeachswitchendswitch

Attention! PHP does not support mixing of two syntaxes within the same control block.

Do-while

The loop looks familiar at first glance, but ignores some of its usage.

The handbook says that senior C users may be familiar with another different do-while circular usage, putting statements in do-while(0) . I have heard of this technique for the first time, it seems I am still a C language small white.

By the way, search do-while(0) for the benefits of this particular usage.

    1. Code chunking is more intuitive than just using curly braces.
    2. Use break skipping over the remaining piece of code.
    3. Useful for macro definition functions, using the end of the sentence can be a semicolon, looks more like a function call.
    4. Block-level scopes, which prevent variable names from spreading to the upper scope.
    5. The deformed goto statement.

This post is very good, do{}while (0) Role-C + +-Segmentfault.

Huaian whipped Egg Download (http://www.gamefrye.com/)

Foreach

foreachCan only be applied to the traversal of arrays and objects. The foreach syntax structure provides a simple way to iterate through an array, with the following two syntaxes.

foreachas$value)statementforeachas$key$value)statement

To modify the value of an array element, you need to use a reference assignment, which is implemented by the $value preceding addition & .


   
     $arr = array(1, 2, 3, 4);foreach($arras &$value) {$value = $value * 2;}unset($value);foreach($arras$value) {echo"$value ";  // 2 4 6 8}

Attention! The reference to the last element of the array $value foreach remains after the loop, and unset() it is recommended to use it for destruction.

List-each

In the example program, a special traversal method is also found, which is called list-each .

When foreach execution begins, the pointer inside the array automatically points to the first cell, so it does not need to be foreach called before the loop reset() . whilein the case of list-each , however, the inner pointer of the array $arr is always there, so it needs to be before the next loop reset($arr) .


   
     $arr = array('one', 'two', 'three');// reset($arr);while(list($key, $value) = each($arr)) {echo"Key: $key; Value: $value ";}reset($arr);while(list($key, $value) = each($arr)) {echo"Key: $key; Value: $value ";}

In the above code, the first one reset can be omitted, but the second one reset cannot be saved.

List

PHP 5.5 adds the ability to iterate through an array of arrays and unpack the nested array into the loop variable.


   
     $array = [[1, 2],[3, 4],];foreach($arrayaslist($a, $b)) {echo"A: $a; B: $b";}

list()The cells in the cell can be less than the nested arrays, and the extra array cells are ignored. If more than, an error message will be issued.

Break

breakUsed to end for/foreach/while/do-while/switch the execution of the current structure. breakyou can accept an optional numeric parameter to decide to jump out of a few loops, but the argument cannot be a variable.

breakJump out of the multi-loop or the first encounter, deliberately wrote a small program to try a bit.


   
     while(1) {while(1) {echo'hello ';break2;}}echo'world';

Specially in C language to try a bit, prompt syntax error.

Continue

breakSimilarly, continue you can also accept an optional numeric parameter to decide to skip a few loops to the end of the loop.

Attention! Statements in PHP are switch considered to be continue a looping structure that can be used.

Switch

The manual says that PHP differs from other languages in that the function continue of the statement switch is similar break . What does that mean?

switch/caseDoing is loosely compared == , not strictly compared === . In terms of efficiency, the switch conditions in the statement are only one time and are used to compare with each case statement. An case expression can be any expression that evaluates to a simple type and cannot be used with an array or an object. Semicolons case are allowed in place of the statement after a semicolon is used.

Declare

declareThe structure is used to set the execution instruction of a piece of code, the syntax structure is as follows:

declare(directive)statement

directivesection allows declare to set the code snippet behavior, currently only two commands are known: ticks and encoding . declarestructs can also be used for global scope, affecting all code after expiration. However, if a declare structured file is contained by another file, it does not work for the parent file that contains it.

Tick (clock cycle) is an event that occurs in a declare code snippet when an interpreter executes n a low-level statement that can be timed. The events that appear in each tick are register_tick_function() specified by the. The usage is broadly as follows.

declare1);functiontick_handler(){echo"tick_hander() called.\n";}register_tick_function('tick_hander');

There are a number of low-level statements that can be timed, a periodic event is register_tick_function() invoked after each statement, and a periodic event is invoked at the end of the curly brace.

Note that the expressions in PHP cannot be separated by commas, or syntax errors will occur. This is different from the C language, just noticed.

You can use the encoding directive to specify how each script is encoded. Use the following:

declare'ISO-8859-1);

Return

If it is called at the global scope, the current script file aborts the run. If the current script file is include either or require , control is returned to the calling file. If the current script is being used include , then return the value is treated as the include return value of the call, then require ?

Require

requireAnd include almost exactly the same, in addition to handling the failure in a different way.

requireThe script aborts when a level error occurs on an error E_COMPILE_ERROR . Instead include E_WARNING , only a warning is generated and the script continues to execute.

Include

includeStatement to include and run the specified file, note the search order for the specified file.

    • The included files are searched first by the path given by the parameters. If a path is defined, include_path it is completely ignored.
    • If no directory (only file name) is given, it is searched according to include_path the specified directory. If you do not find it, look in the directory where the script file is located and the current working directory. So the question is, what is the difference between the directory where the script file is called and the current working directory?
    • If the file is not finally found, the include structure emits a warning that the require structure will emit a fatal error.

When a file is contained, it contains code that inherits the include range of variables in the row. From there, the variables defined in the called file can be used in the calling file. When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode, replying to the end of the file.

For the return value, include returns and warns when a failure occurs FALSE . A successful inclusion returns 1 unless the return value is also given in the included file. If a function is defined in the include file, these functions can be return used independently of the master file, either before or after it is defined.

If the files from the remote server should run at the far end and only output the results, use the readfile() function better. Another way to include a PHP file in a variable is to capture its output with an output control function include . The first time encountered, more unfamiliar. The following code can vars.php output the content returned in the script.


   
     $string = get_include_contents('vars.php');functionget_include_contents($filename){if(is_file($filename)) {ob_start();include$filename;$contents = ob_get_contents();ob_end_clean();return$contents;}returnfalse;}echo$string;

Because include and require is a special language structure, its parameters do not need parentheses. If the file is included two times, PHP will make a fatal error because the function is already defined. Recommended Use include_once .

Require_once

require_onceStatements and require statements are identical, the only difference being that PHP checks if the file has already been included, and if it does, it will not be included again.

Include_once

include_onceStatements and include statements are similar, the only difference is that if the file has already been included, it will not be included again.

Goto

gotoThe operator is used to jump to another location of the program, and the target location can be marked with a colon for the target name. gotoThere are certain restrictions in PHP where the target location can only be in the same file and scope. That is, you cannot jump out of a function or class method, or you can skip into any loop or switch structure.

(End of full text)

The above describes the PHP voice in the process of control and implementation methods, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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