PHP Introductory Tutorial Flow control Statement _php Tutorial

Source: Internet
Author: User
In PHP, the Process Control statements include the While,for,if,switch,foreach,declare statement, I would like to introduce you to the introduction of the Process Control statement use method, I hope this method to your friends have to rent.

In PHP, flow control statements consist mainly of conditional statements and loop statements. Flow control statements have if/else and switch statements, and loop statements have while and for statements.

1. If statement

In PHP, the IF statement has two syntactic constructs. A curly brace represents a statement block, a colon that represents a statement block. The former is typically used in pure code, which is typically used when code and HTML are combined. The two formulations are as follows:

The code is as follows Copy Code

Curly braces represent statement blocks
if ($value) {
Operation
} elseif ($value) {
Operation
} else {
Operation
}

A colon represents a statement block
if ($value):
Operation
ElseIf ($value):
Operation
else:
Operation
endif

2. Switch statement

The switch statement is somewhat similar to the C language, and it can be judged using numbers and strings. It executes the code, starting with the first matching value (or default), until a break is encountered. So do not forget to write a break when writing a program. Otherwise, all branches will be executed. The code examples are as follows:

The code is as follows Copy Code

Switch ($value) {
Case ' a ':
Case ' B ':
Echo ' value is a or B. ';
Break
Case ' C ':
Echo ' value is C. ';
Break
Default
Echo ' value is others. ';
Break
}

3. While statement

The while statement, like the IF statement, also uses two syntactic constructs. Curly braces represent the statement block and the colon represents the statement block representing the statement block. The two formulations are as follows:

The code is as follows Copy Code
Curly braces represent statement blocks
$i = 0;
while ($i < 5) {
echo $i;
}
A colon represents a statement block
$i = 0;
while ($i < 5):
echo $i;
Endwhile;

4. Do...while statements

The difference between the Do...while statement and the while statement is that the while statement is executed first, while the Do...while statement is first executed and then judged. That is, even if the condition does not meet the requirements, the Do...while statement is executed once. The wording is as follows:

The code is as follows Copy Code

$i = 0;
do {
echo $i;
} while ($i < 5);

5. For statement

In the For statement, it is important to note that it executes the initialization code first, and then each time the loop executes the judgment statement, and the execution loop executes the self-increment decrement function. That is, if the condition is not met, the loop will not be executed. There are two syntactic structures, curly braces represent statement blocks, and colons denote statement blocks that represent block statements. The two formulations are as follows:

The code is as follows Copy Code
Curly braces represent statement blocks
for ($i = 0; $i < 5; $i + +) {
echo $i;
}
A colon represents a statement block
for ($i = 0; $i < 5; $i + +):
echo $i;
ENDfor;

6. foreach statement

The foreach statement is used to traverse the entire array. As with for, it has two syntactic structures, curly braces for statement blocks, and colons for statement blocks representing statement blocks. The two formulations are as follows:

The code is as follows Copy Code
Curly braces represent statement blocks
foreach ($array as $value) {
Each $value;
}
A colon represents a statement block
foreach ($array as $value):
Each $value;
Endforeach;

7. Declare statements

The Declare statement refers to a function that is called after each N-statement execution. Such as

The code is as follows Copy Code
Register_tick_function (' Test ');
DECLARE (ticks = 2) {
print ' 1 ';
print ' 2 ';
print ' 3 ';
print ' 4 ';
print ' 5 ';
}
function Test () {
print ' Declare
';
}

The result of this code is

1
2
3

http://www.bkjia.com/PHPjc/628627.html www.bkjia.com true http://www.bkjia.com/PHPjc/628627.html techarticle in PHP, the Process Control statements include the While,for,if,switch,foreach,declare statement, let me give you the introduction of the Process Control statement use method, I hope this method of ...

  • 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.