Learn PHP three-process Control with classic examples

Source: Internet
Author: User
First, sequential control:
This is done by default from left to right, from top to bottom in a simple order. Uncontrolled process.

Second, branch control:
Single branch, dual branch, multi-branch;
Single-branch Syntax:

$age =17;if ($age <18) {    echo "minors under 18 years of age";}

Two-branch syntax:

$age =24;if ($age <18) {    echo "is under 18 years of age";} else {    echo "You are 18 years old and compliant";}

Multi-branch Syntax: (ElseIf can be multiple else or not.) Switch statement can also be used to Long branch judgment)

if ($age <18) {    echo "minors under 18 years of age do not meet the standard";} elseif ($age >60) {    echo "exceeds 60 years of age";} else {echo "It's a    good time!") ";}

Switch is also a common use of multi-branch statements, note the point:
There must be a break interrupt after each case Switch,default optional depending on the requirements, the position of default placement has no effect on the execution result, first in case order, and executing the default content when it is not matched. The switch writes the conditional expression, and the case is followed by a constant (a decimal number is also OK).

$day = "1"; switch ($day) {case    ' 1 ':        echo "Today Monday";        break;    Case ' 2 ':        echo "Today Tuesday";        break;    Case ' 3 ':        echo "Today Wednesday";        break;        Default:        echo "The earth is still turning but not knowing the day of the Week";        break;    }

Use scenario analysis for if and switch:
If is a judgment on a range, and switch is the judge of a point, when the branch is a few points (such as judging the direction of the tank, gender), you should use switch, if the branch is a number of areas (range) of the judgment, then consider using IF. switch is more efficient.

Third, Cycle control:
For loop, while, do and

For loop for ($i =0; $i <; $i + +) {     echo "<br/>goodgoodstudy";}
while$i=0;//loop control variable while ($i <10) {    echo "<br/> hello Changsha";    $i ++;//here to the loop control variable self-increment}
Do    while $i =0;//loop control variable do    {        echo "<br/> hello Changsha";        $i + +;    } while ($i <10);

Print half of the pyramid exercises:

Half of the pyramid for    ($i =0; $i < 5; $i + +) {//Outer loop control layer for        ($j =0; $j <= $i; $j + +) {//Inner loop control the number of each layer            echo "*";        }        echo "<br/>";    }



Print Pyramid Exercises:

Pyramid for ($i =0; $i <= 5; $i + +) {for     ($k =0; $k < 5-$i, $k + +) {//* play * Before the space        echo ";    }    for ($j =0; $j < ($i-1) *2+1; $j + +) {//Line number * rule: ($i-1) *2-1        echo "*";    }     echo "<br/>";}



To print a hollow pyramid exercise:

Hollow Pyramid $n=5;for ($i =1; $i <= $n; $i + +) {for     ($k =1; $k <= $n-$i; $k + +) {//play * Before the space        echo ";    }    for ($j =1; $j <= ($i-1) *2+1; $j + +) {//lines per line * rule: ($i-1) *2-1        if ($i ==1 | | $i = = $n) {            echo "*";        } else {
  
   if ($j ==1 | | $j = = ($i-1) *2+1) {                echo "*";            } else {                echo ";     }}} echo "<br/>";}
  



Calculator Exercises:
Calculator interface:

<! DOCTYPE html>


Calculation Result:

<?php    //Receive users from calculator.php (corresponding to the static page browser) submitted data    //1, receive number1 number2; The request method can receive the user's post or GET request data    $number 1=$_request[' number1 ');    $number 2=$_request[' number2 '];    Receive operator    $oper =$_request[' oper '];    $res =0;    Switch ($oper) {case        ' + ':            $res = $number 1+ $number 2;            break;        Case '-':            $res = $number 1-$number 2;            break;        Case ' * ':            $res = $number 1* $number 2;            break;        Case '/':            $res = $number 1/$number 2;            break;        Default:            echo "operator is incorrect";            break;    }    The echo "Operation result is:". $res;? ><a href= "calculator.php" > Back to Calculator page </a>

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.