Learn PHP's statement

Source: Internet
Author: User
Directory

[1]IF statement [2]switch [3]while[4]do-while[5]for statement [6]foreach[7]break[8]continue[9]goto

Previous words

Any PHP script is composed of a series of statements. A statement can be an assignment statement, a function call, a loop, a conditional statement, or even a statement that does nothing (an empty statement). The statement usually ends with a semicolon. In addition, you can encapsulate a set of statements into a group of statements with curly braces. The statement group itself can be treated as a line statement. This article describes the various statement types in detail


If statement

The IF structure is one of the most important features in many languages including PHP, which allows code snippets to be executed conditionally

if (condition) {code executed when the condition is true;} if (condition) {The code executed when the condition is true;} else {The code executed when the condition is false;} if (condition) {The code executed when the condition is true;} elseif (condition) {The code executed when the condition is true;} else {The code executed when the condition is false;} <?php$t=date ("H"), if ($t < ") {echo" has a good morning! ";} elseif ($t <") {echo "has a good day!";} else {echo "has a good night!";}? >
Switch statement

A switch statement is similar to a series of if statements that have the same expression. In many cases it is necessary to compare the same variable (or expression) with many different values and execute different code depending on which value it equals. This is the purpose of the switch statement

[Note that the]switch/case do is loosely compared

switch (expression) {case Label1:code to being executed if expression = Label1; break; case Label2:code to be executed if ex pression = Label2; Break;default:code to being executed if expression is different from both Label1 and Label2;} <?phpswitch ($x) {case 1:echo "number 1"; Break;case 2:echo "Number 2"; Break;case 3:echo "Number 3"; Break;default: echo "No number between 1 and 3";}? >
While statement

The while loop is the simplest type of loop in PHP. The meaning of the while statement is simple, and it tells PHP to repeatedly execute the loop statements in the nested if the value of the while expression is TRUE. The value of the expression is checked every time the loop is started, so even if the value changes in the Loop statement, the statement does not stop executing until the end of the loop. Sometimes a loop statement does not execute once if the value of the while expression starts with FALSE

while (expr) statement<?php$i = 1;while ($i <=) {echo $i + +;} $i = 1;while ($i <=): print $i; $i ++;endwhile;? >
Do-while

The Do-while loop is very similar to the while loop, except that the value of the expression is checked at the end of each loop rather than at the beginning. The main difference from the normal while loop is that the Do-while Loop statement is guaranteed to execute once (the true value of the expression is checked after each loop)

Do {the code to execute;} while (condition is true); <?php$i = 0;do {echo $i;} while ($i > 0); >
For statement

The For loop is the most complex loop structure in PHP. For loop statements, initialization is evaluated unconditionally before the start of the loop, and the loop condition is evaluated before each cycle begins. If the value is true, the loop is resumed, the loop body statement is executed, and if the value is False, the loop is terminated. Increment statements are executed after each loop

for (init counter; test counter; increment counter) {code to being executed;} <?php for ($x =0; $x <=10; $x + +) {echo "number The word is: $x <br> ";} ?>
Foreach

The foreach syntax structure provides an easy way to iterate through an array. foreach can only be applied to arrays and objects, if an attempt is made to apply to a variable of another data type, or an uninitialized variable will emit an error message

For each iteration of the loop, the value of the current array element is assigned to the $value variable, and the array pointer moves one at a time until the last array element is reached. Generally there are two ways: Do not remove the label, remove the

"1" only value, do not remove the label

foreach ($array as $value) {code to is executed;} <?php $colors = Array ("Red", "green", "blue", "yellow"); foreach ($colors as $value) {echo "$value <br>";}? >

"2" simultaneously remove the mark and value

foreach ($array as $index = + $value) {code to is executed;} <?php $colors = Array ("r" = "Red", "g" = "green", "B" = "blue", "y" = "yellow"); /*r:redg:greenb:bluey:yellow*/foreach ($colors as $key = + $value) {echo $key. ":". $value. " <br> ";}? >
Break

Break ends the execution of the current for,foreach,while,do-while or switch structure

Break can accept an optional numeric parameter to decide to jump out of a few loops

$i = 0;while (+ + $i) {switch ($i) {case 5:echo ' at 5<br/>\n '; break 1;/* only exit switch. */Case 10:echo ' at 10; Quitting<br/>\n "; Break 2; /* Exit switch and while loop */default:break; }}
Continue

Continue is used in the loop structure to skip the remaining code in this loop and start the next loop when the condition evaluates to True

Continue accepts an optional numeric parameter to decide to skip a few loops to the end of the loop. The default value is 1, which jumps to the end of the current loop

$i = 0;while ($i + + < 5) {echo "outer<br/>\n"; while (1) {echo "Middle<br/>\n" and while (1) {echo "inner& Lt;br/>\n "; Continue 3; } echo "This never gets output.<br/>\n"; } echo "Neither does this.<br/>\n";}
Goto

The goto operator can be used to jump to another location in the program. The target location can be marked with a colon for the target name, and the jump instruction is the mark after Goto followed by the target location. There are certain restrictions on Goto 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 another function. You cannot jump into any loop or switch structure. You can jump out of a loop or switch, usually using Goto instead of a multi-layered break

<?phpgoto A;echo ' Foo '; a://' Bar ' echo ' bar ';? >

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