PHP Loop Structure Example Tutorial _php

Source: Internet
Author: User
For Loop statement

Print Pyramids

The Complete pyramid
Copy the Code code as follows:
Print Pyramids
$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 '
';

}

Switch statement:
Copy the Code code as follows:
/* $a = "1";
Switch ($a) {
Case 1:
echo $a;
Break

Default
echo "Error";
Break
}
Convert strings and Numbers automatically

Handling when a Boolean value is encountered in a switch SELECT statement:
Copy the Code code as follows:
$b =true;
Switch ($b) {
Case false:
echo "Mismatch";
Break
A value that represents non-false can be true-----auto-Convert type
Case "1":
echo "Successful Match";
Break
Default
echo "Ko";
}
1.default statements are executed last regardless of the order, and if no other case is matched, then the default statement is executed
2. If there is no break statement, the result of the next case will be output until a break is reached.

While Loop and do: While loop:
Copy the Code code as follows:
/*while Cycle
$i = 0;
while ($i <10) {
echo "Paxster
". $i;
$i + +;
}
Do.. While Loop--------perform the re-judgment first, at least once
/* $do = 0;
do{
Echo '
Paxster ';
$do = $do +1;
}while ($do <8); * *

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

Constant:
Copy the Code code as follows:
Two methods of defining constants-----
Define (' tax ', 200);
Echo tax;

Const TAB=100;
Echo Tab;


Write a simple calculator:

Step1: Writing the input interface
Copy the Code code as follows:









Step2: Write Compute backend Code
Copy the Code code as follows:
$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 behind this loop. You can specify the number of layers to jump out, such as continue 2, to jump out of layer two, similar to break 2;

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

Copy the Code code as follows:
Goto statement
I only perform once
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

http://www.bkjia.com/PHPjc/732384.html www.bkjia.com true http://www.bkjia.com/PHPjc/732384.html techarticle the FOR Loop statement prints 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. .

  • Related Article

    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.