Home >Backend Development >PHP Tutorial >PHP loop structure example explanation_PHP tutorial

PHP loop structure example explanation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:16996browse

for loop statement
PHP loop structure example explanation_PHP tutorial

Print pyramid

PHP loop structure example explanation_PHP tutorial

Complete pyramid

Copy code The code is as follows:

//Print pyramid
$n=25;
for($i=1;$i<=$n;$i++){
//Space loop
for($k=1;$k<=$n-$i;$k++ ;
                                                                                                            || $ j==$i*2-1){
                                                                                                                                                                                                                            . 🎜>                                                                                                                                                        🎜> for($j=1;$j<=($i-1)*2+1 ; $j++) {
echo '.';
}*/

echo '
';


}



switch statement:



Copy code

The code is as follows:


/*$a="1";
switch ($a) {

case 1:

echo $a;
break;

default:

echo "Error";
break;

} //Automatically convert strings and numbersHandling of Boolean values ​​encountered in switch selection statements:


Copy code

The code is as follows:


$b=true;
switch($b){
case false:
echo "Does not match";
break;
//Represents non-false values ​​​​that can be true-----automatic conversion type
        case "1":
                                                                                                                                                                                                                                                          . "ko";
}
//1. The default statement is executed last regardless of the order. If no other case is matched, the default statement is executed //2. If there is no break statement, then The result of the next case will be output until there is a break.
while loop and do..while loop:



Copy code

The code is as follows:

/*while loop
$i=0;
while($i<10){
echo "paxster
".$i;
$i++;
}
//do..while loop--------execute first and then judge, execute at least once
/*$do=0;
do{
echo '< ;br />Paxster';
$do=$do+1;
}while($do<8);*/

The combination of while loop and switch selection statement:

Constant:

Copy code The code is as follows:

//Define constants-----Two methods
define('TAX',200);
echo TAX;

const Tab=100;
echo Tab;


Write a simple calculator:

step1: Write input interface

Copy code The code is as follows:


 




< input type="text" placeholder="Enter a number" name="num2">






step2: Write calculation background code



Copy code
The code is as follows: $num1=$_REQUEST['num1'];
$num2=$_REQUEST['num2'];

$operation=$_REQUEST['operation'];
$res=0;

switch($operation){
case '+':

$res=$num1+$num2;

break;
case '-':
$res=$num1 - $num2;
break;
case '*':
$res=$num1*$num2;
break;
case '/':
$res=$num1/ $num2;
break;
default:
echo 'Incorrect input';
}

echo 'The result is'.$res;
?>


continue statement: skip the code after this loop. You can specify the number of levels to jump out of, such as continue 2; it means jumping out of two levels, similar to break 2;



goto statement: Just like the C language, jump to the labeled code, the code in the middle will not be executed and will be ignored directly. PHP loop structure example explanation_PHP tutorial

Copy code

The code is as follows:

//goto statement
//i is only executed once
for($i=0,$j=50;$i<100;$i++){
while($j --){
                                                                                                                                                                                                                 . .$i.'j='.$j;


Keep it simple,keep it clear.——PAXSTER



http://www.bkjia.com/PHPjc/732384.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/732384.htmlTechArticlefor loop statement prints the pyramid. The complete pyramid copy code is as follows: //Print pyramid $n=25; for( $i=1;$i=$n;$i++){ //Space loop for($k=1;$k=$n-$i;$k++){ echo ''; } //Characters...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn