PHP structure Statements

Source: Internet
Author: User

PHP conditional statements

In PHP, we can use the following conditional statements:

    • If statement-executes the code if the specified condition is true
    • If...else Statement-Executes the code if the condition is true, or the Else statement if the condition is false
    • If...elseif....else statement-Select one of several segment code blocks to execute
    • Switch statement-statement one of several code blocks to execute
If statement

The IF statement is used to execute code when the specified condition is true.

Grammar:

<? PHP if (condition) {     # when the condition is true code executed }?>
If Else statement

Execute code when the IF condition is true when the else condition is false

Grammar:

<? PHP if (condition) {    # condition is true when code executed }else  {    #  Code executed when the condition is False }?>
If ElseIf Else statement

The IF condition is true when the code is executed, the code is executed when the ElseIf condition is true, and the else condition is false when the code is executed

Grammar:

<? PHP if     code}elesif (condition) {# # If the first condition is not satisfied when the condition is true when the conditions of the.      Code executed when the condition is true when the code }else  {   # condition is false } ?>
Switch statement

If you choose to execute one of several blocks of code, use the switch statement to avoid lengthy if: ElseIf. else code block.

Grammar:

<? PHP Switch (expression) {    case "Tom":        echo "Your name is Tom";          Break     Case "Jak":        echo "you name was Jak" break            default:        echo " You name is Error ";}? >

Working principle:

    1. Perform a single calculation on an expression (usually a variable)
    2. Compare the value of an expression with the value of a case in the structure
    3. If there is a match, the code associated with the case is executed
    4. After the code executes, the break statement prevents the code from jumping into the next case to continue execution
    5. If no case is satisfied, use the default statement
PHP Loops

In PHP, there are the following loop statements:

    • While-loop code block whenever the specified condition is true
    • Do...while-Executes the code block first, and then repeats the loop whenever the condition is specified as True
    • For-loop code block specified number of times
    • foreach-iterates through each element in the array and loops the block of code
While statement

When the condition is true, the while loop executes the statement

Grammar:

<? PHP  while (condition) {      # statement executed when condition is true }?>

PHP structure Statements

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.