PHP process control statement

Posted by JMM on Sun, 05 Dec 2021 22:36:23 +0100

1. Conditional control statement

1. if statement

if(expression){
    Statement 1;
    Statement 2;
}
  • rand() function: get a random integer.
<?php
    $num=rand(1,31);
    if ($num%2==0){
        echo "\$num=$num";
        echo '<br>$num It's an even number';#Single quotation marks output an ordinary string
    }
?>

2. If else statement

if(expression){
    Statement 1;
}else{
    Statement 2;
}
<?php
    $num=rand(1,31);
    if ($num%2==0){
        echo "\$num=$num";
        echo '<br>$num It's an even number';
    }else{
        echo 'Variables are odd';
    }
?>

3. Else if statement  

if(expression){
    Statement 1;
}elseif(expression){
    Statement 2;
}else{
    Statement 3;
}
<?php
    $month=date("n");//Numerical representation of month n
    $today=date("j");//Numerical representation of j-day variables
    if ($today>=1 && $today<=10){
        echo 'Today is'.$month.'month'.$today.'day,It's early this month';
    }elseif($today>10 && $today<=20){
        echo 'Today is'.$month.'month'.$today.'It's the middle of this month';
    }else{
        echo 'Today is'.$month.'month'.$today.'It's late this month';
    }
?>

4. switch statement

switch(Variable or expression){
    case Constant expression 1://Notice that it's a colon, not a semicolon
        Statement 1;
        break;
    ...
    case Constant expression n://Notice that it's a colon, not a semicolon
        sentence n;
        break;
    default:
        sentence n+1;
        break;
}
  • The isset() function detects whether the variable has been set and is not NULL
  • The include() function obtains all the text in the specified file and copies the text to the file using the include function  
  • HTML map tag usage

< map: image or map with clickable area. We should add both id attribute and name attribute to < Map > attribute

The area element is always nested inside the map element to define the area in the image map

Shape: defines the shape of the mouse sensitive area in the image mapping

    Circular rect angle default poly

The coords attribute defines the mouse sensitive areas or coordinates of the client image map

    Circle: shape="circle", coords="x,y,z"

        Here, x and y define the position of the center of the circle ("0,0" is the coordinate of the upper left corner of the image), and r is the radius of the circle in pixels.

    Polygon: shape="polygon", coords="x1,y1,x2,y2,x3,y3,..."

        Each pair of "x,y" coordinates defines a vertex of the polygon ("0,0" is the coordinate of the upper left corner of the image). At least three sets of coordinates are required to define a triangle; High latitude polygons require more vertices.

        The polygon is automatically closed, so there is no need to repeat the first coordinate at the end of the list to close the entire area.

    Rectangle: shape="rectangle", coords="x1,y1,x2,y2"

        The first coordinate is the vertex coordinate of one corner of the rectangle, the other pair of coordinates is the vertex coordinate of the diagonal, and "0,0" is the coordinate of the upper left corner of the image.

href attribute: hyperlink

<?php
    switch(isset($_GET['lmbs'])?$_GET['lmbs']:""){  
        //The isset() function detects whether the variable has been set and is not NULL
        /*
        $_GET Gets the data submitted through the GET method
        Here is the variable passed to get the hyperlink
        */
        case"Latest products":
            include"new.php";
            break;
        case"Popular goods":
            include"jollification.php";
            break;
        case"Pick of the week":
            include"commend.php";
            break;
        case"Order item":
            include"order_form.php";
            break;
        default:
            include"new.php";
            break;
    }
?>
<map name="Map",id="Map">
    <area shape="rect" coords="9,92,65,133" href="#" alt="">
    <area shape="rect" coords="78,89,131,115" href="index.php?lmbs=<?php echo urldecode("Latest products");?>" alt="">
    <area shape="rect" coords="145,92,201,114" href="index.php?lmbs=<?php echo urldecode("Pick of the week");?>" alt="">
    <area shape="rect" coords="212,91,268,114" href="index.php?lmbs=<?php echo urldecode("Popular goods");?>" alt="">
    <area shape="rect" coords="474,93,529,113" href="index.php?lmbs=<?php echo urldecode("Order item");?>" alt="">
</map>
<!-- urlencode:Is the pointer to the web page url A coding conversion method of Chinese characters in -->

2. Loop control statement  

1. while loop statement

while(expression){
    sentence;
}
<?php
$num=1;
$str="10 Even number within";
while($num<=10){
    if($num%2==0){
        $str.=$num." ";
    }
    $num++;
}
echo $str;
?>

2. Do while loop statement  

<?php
$num=1;
while($num!=1){
    echo'while loop';
}
do{
    echo'do-while loop';
}while($num!=1);
?>

do-while Loop  

3. for loop

<?php
$num=1;
for($i=1;$i<=100;$i++){
    $num*=$i;
}
echo "100!=".$num;
?>

4. Foreach cycle

For arrays only, in the format

foreach($array as $value)

        sentence;

foreach ($array as $key =>$value)

        sentence;

This statement will traverse the array $array and pass the value in the current array to $value (or $key and $value)

5. break and continue

Both break and continue can add a number to indicate that several layers of loops are skipped

 

Topics: PHP Back-end