Example of PHP loop structure explaining _php example

Source: Internet
Author: User
Tags goto

For Loop statement

Print Pyramid

The Complete pyramid

Copy Code code as follows:

Print Pyramid
$n = 25;
for ($i =1; $i <= $n; $i + +) {
Space loop
For ($k =1 $k <= $n-$i; $k + +) {
Echo ';
}
Character Loops
for ($j =1; $j <= $i *2-1; $j + +) {

if ($i ==1 | | $i = = $n) {
echo '. ';
}
else{
if ($j ==1 | | $j = = $i *2-1) {
echo '. ';
}else{
Echo ';
}
}
}
/*
For ($j =1 $j <= ($i-1) *2+1; $j + +) {
echo '. ';
}*/
Echo ' <br/> ';

}

Switch statement:

Copy Code code as follows:

/* $a = "1";
Switch ($a) {
Case 1:
echo $a;
Break

Default
echo "Error";
Break
}
Automatically convert strings and numbers

Processing when a Boolean value is encountered in a switch selection statement:
Copy Code code as follows:

$b =true;
Switch ($b) {
Case false:
echo "Mismatch";
Break
A value that is not false can be true-----an automatic conversion type
Case "1":
echo "Successful Match";
Break
Default
echo "Ko";
}
1.default statements are executed regardless of order, and if no other case is matched, the default statement is executed
2. If there is no break statement, the result of the next case will be output until there is a break.

While loops and do. While loop:

Copy Code code as follows:

/*while Cycle
$i = 0;
while ($i <10) {
echo "paxster<br>". $i;
$i + +;
}
Do.. While Loop--------perform a second judgment first, execute at least once
/* $do = 0;
do{
Echo ' <br/>paxster ';
$do = $do +1;
}while ($do <8);

The combination of the while loop and the switch selection statement:

Constant:

Copy Code code as follows:

Two methods of defining constants-----
Define (' TAX ', 200);
Echo TAX;

Const TAB=100;
Echo Tab;


To write a simple calculator:

Step1: Writing Input interface

Copy Code code as follows:

<title></title>

<body>

<form action= "cal.php" method= "Get" >
<input type= "text" placeholder= "Enter a number" name= "NUM1" >
<input type= "text" placeholder= "Enter a number" name= "num2" >
<select name= "Operation" >
<option value= "+" >+</option>
<option value= "-" >-</option>
<option value= "*" >*</option>
<option value= "/" >/</option>
</select>
<input type= "Submit" value= "calculation" >
</form>
</body>


Step2: Write Calculation background code
Copy Code code as follows:

<?php
$num 1=$_request[' NUM1 '];
$num 2=$_request[' num2 '];

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

Switch ($operation) {
Case ' + ':
$res = $num 1+ $num 2;
Break
Case '-':
$res = $num 1-$num 2;
Break
Case ' * ':
$res = $num 1* $num 2;
Break
Case '/':
$res = $num 1/$num 2;
Break
Default
echo ' input error ';
}

Echo ' result is '. $res;
?>


Continue statement: Skips the code following this loop. You can specify that you want to jump out of the specified number of layers, such as continue 2, to jump out of two layers, similar to break 2;

Goto statement: Like the C language, jump to the tagged code, the middle of the code will not be executed, directly ignored.

Copy Code code as follows:

Goto statement
I only perform one time
For ($i =0, $j =50 $i <100; $i + +) {
while ($j-) {
if ($j ==17) goto end;
}
}
Echo ' i= $i ';
End
Echo ' i= '. $i. ' j= '. $j;

Keep it simple,keep it clear.--paxster

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.