PHP Switch Statement

Source: Internet
Author: User
Tags php switch

The switch statement is used to perform different actions based on different conditions.

Switch statement

If you want to selectively execute one of several blocks of code, use the Switch statement.

Use the Switch statement to avoid lengthy if. ElseIf. else code block.

<?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";}? ></body>


PHP while loop

When you write code, you often need to run the same block of code repeatedly. Instead of adding several almost equal lines of code to the script, we can use loops to perform such tasks.

In PHP, we have the following looping 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

PHP while loop

The while loop executes the code block whenever the specified condition is true.

Grammar
while (condition is true) {  the code to execute;}

The example above first sets the variable $x to 1 ($x =1). The while loop is then executed as long as the $x is less than or equal to 5. Each time the loop runs, the $x increments by 1:

Instance
<?php $x = 1; while ($x <=5) {  echo "This number is: $x <br>";  $x + +;} ?>

PHP Do...while Loops

The Do...while Loop executes the code block first, then checks the condition and repeats the loop if the specified condition is true.

Grammar
Do {  the code to execute;} while (condition is true);

The following example first sets the variable $x to 1 ($x =1). Then, the Do While loop outputs a string, and then increments the variable $x by 1. The condition is then checked ($x is less than or equal to 5). As long as the $x is less than or equal to 5, the loop will continue to run:

Instance
<?php $x = 1; Do {  echo "This number is: $x <br>";  $x + +;} while ($x <=5);? >


Note that the Do While loop tests the condition only after executing the statement inside the loop. This means that the Do While loop executes at least one statement at a time, even if the condition test fails the first time.

The following example sets the $x to 6 and then runs the loop, followed by a check of the condition:

Note that the Do While loop tests the condition only after executing the statement inside the loop. This means that the Do While loop executes at least one statement at a time, even if the condition test fails the first time.

The following example sets the $x to 6 and then runs the loop, followed by a check of the condition:

Instance
<?php $x =6;do {  echo "This number is: $x <br>";  $x + +;} while ($x <=5);? >

The result of the output is 6.

PHP Switch Statement

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.