PHP process control and file inclusion: basics and key points

Table of contents

PHP process control

Sequential structure:

Branch structure:

Switch branch:

PHP loop structure

for loop

while loop

do-while loop

The difference between while and do-while:

cycle control

Flow Control Replacement Grammar

The php file contains

The role of PHP file inclusion

The four forms contained in the PHP file

PHP file loading

Principle of file loading

file load path

Nested file contains

The difference between include and require


PHP process control

Flow control: the direction of code execution

Sequential structure: code is executed sequentially from top to bottom.

Branch structure: Given a condition, there are multiple executable code blocks at the same time, and then a certain piece of code will be executed according to the condition

Loop structure: Within the range controlled by a certain condition, the specified code can be executed repeatedly

Sequential structure:

Execute sequentially from top to bottom

Branch structure:

if branch:

Given a condition, set multiple situations for the condition at the same time, and then implement specific code blocks through conditional judgment.

if-else format

if(条件表达式)
{
//满足条件执行语句
}
else
{
//不满足条件执行语句
}

if-else if-else format

if(条件表达式)
{
//满足条件执行语句
}
elseif(条件表达式)
{
//if条件如果不满足会来判断这个条件是否满足
}
else
{
//不满足条件执行语句
}

Switch branch:

There is a set of situations where, through a condition, there are many values, but each value corresponds to a different code for execution. Switch judgment method: the condition is placed inside the branch structure for judgment.

Switch syntax format:

switch(条件表达式)
    case 1:
    //执行的代码段1
    break; 
#这是是让匹配到的结果以后直接退出,不再匹配,如果不加break,它会继续向下匹配。
    case 2:
    //执行的代码段2
    ...
    default
    //都没有匹配到的执行的代码段n

PHP loop structure

The code segment can be executed multiple times under certain control

for loop

for循环的语法格式:

for(条件表达式1,条件表达式2,条件表达式3)
{
    循环体
}
//条件表达式1:定义初始条件
//条件表达式2:边界判定,限制循环执行的次数
//条件表达式3:用来执行条件变换(自操作)

For example, use a for loop to output ten numbers from 0-9

<?php
for ($i = 1; $i <= 10; $i++) {
    echo $i;
}
echo '<br/>';
for ($i = 1; ; $i++) {
    if ($i > 10) {
        break;
    }
    echo $i;
}
echo '<br/>';
$i = 1;
for (;;) {
    if ($i > 10) {
        break;
    }
    echo $i;
    $i++;
}
echo '<br/>';
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?> 

while loop

Grammar format:

条件初始化
while(条件表达式)
{
循环体
//循环条件的变化
}
//条件表达式就是边界条件

For example:

<?php
$i=1;
while($i<=10)
{
    echo $i,'<br/>';
    $i++;
}
?> 

 

do-while loop

do-while syntax format:

初始化变量
do
{
循环体
//循环条件改变
}
while(条件表达式)

The difference between while and do-while:

while first performs condition matching and then executes the loop body. If the condition is not met once, it will fail directly (the loop body is not executed once). DO-while is to execute the loop body once, and then judge the condition (at least once).

cycle control

Controlling the loop itself within the loop

Interrupt control: Restart the loop, and there are other contents in the loop body, and execute it again.

continue: The current internal loop will no longer be executed, and if there is still a loop body in the internal loop, it will not be executed. Start again

Termination control: the loop ends immediately

break: the current self-loop ends, and the external ones also end (if there are external ones that are not affected)

Flow Control Replacement Grammar

Flow Control Alternative Syntax: Alternative Syntax for Branching and Looping

PHP itself is a scripting language embedded in HTML. It is necessary to write some structural syntax about judgment or loop in HTML. It must conform to the PHP tag specification, and HTML and PHP need to be mixed and matched. If you use the original PHP code, it will be very ugly.

Requirement: Print a ninety-nine multiplication table and display it in a table.

<table border=1>
    <?php for($i=1;$i<10;$i++){?>
    <tr>
        <?php for($j=1;$j<=$i;$j++){?>
            <td>
                <?PHP echo $i .'*' . $j . '=' . $j*$i;?>
            </td>
        <?php }?>
    </tr>
    <?php } ?>
</table>

 

These curly braces written in PHP into HTML are very unsightly, so PHP provides a replacement mechanism so that it can be written without curly braces

for(;;){---->for(;;):

}------------>endfor;

The php file contains

File inclusion: In a PHP script, to include another PHP file to cooperate to complete one thing

The role of PHP file inclusion

The meaning of the file contains:

1. Either use the contents of the included file to achieve code sharing (reuse); include upward; include other files before the current script uses a certain code

2. Either what you have can be used by others to realize code sharing (reuse); downward inclusion; when you have something, you need other scripts to display it

Role: Division of labor and cooperation, each script does different things, so you can use a collaborative approach to complete one thing together

The four forms contained in the PHP file

include: include files

include_once: The system will automatically judge the file inclusion process, whether it has been included (a file can only be included once at most)

require: same as include

require_once: same as include_once

For example:

include1.php:

<?php
//被包含文件
define PI 3.14;
$a=100;
echo $a;
?>

include2.php:

<?php
​
//包含文件:使用数据
    include 'include1.php';
    //引入被包含文件
    echo $a,PI;
?>

In this way, include2 can use the variables in include1

PHP file loading

Principle of file loading

Execution process of PHP code:

1. Read the code file (PHP program)

2. Compile: Convert PHP code to bytecode (generate opcode)

3. Use zendengine to parse opcode and perform logical operations according to bytecode

4. Convert to corresponding HTML code

File loading principle:

1. When the file is loaded (include or require), the system will automatically say that the code in the included file is equivalent to being embedded in the code corresponding to the current file

2. Loading location: Where to load, the location where the code in the corresponding file is embedded is the corresponding include location;

3. The files included in PHP are compiled separately.

If there is a language error in the compilation process of the PHP code, it will fail; but if there is an error in the included file, the system will not report an error until the include statement is executed;

file load path

When the file is loaded, the file path needs to be specified to ensure that PHP can find the corresponding file correctly.

There are two types of paths for file loading:

Absolute path:

  1. Start at the root of the disk (local absolute path)

    Windows: drive letter C:/path

    Linux:/path

  2. Start from the root of the website (web absolute path)

    /: The path corresponding to the relative website host name

Relative path: the path starting from the directory where the current file is located

.| ./: Indicates the current folder

../: Upper level directory (the upper level directory of the current folder)

The difference between absolute path and relative path loading:

1. The absolute path is inefficient, but relatively safe;

2. The relative efficiency of road strength is relatively high, but it is easy to make mistakes;

Nested file contains

Nested file inclusion: One file contains another file, and the included file contains another file.

For example:

include3.php
<?php
//包含文件:使用数据
    $a=100;
    const PI 99;
?>
include4.php
<?php
​
//包含文件:使用数据
    include 'include3.php';
    //引入被包含文件
    echo $a,PI;
?>
include5.php
<?php
​
//包含文件:使用数据
    include 'include4.php';
    //引入被包含文件
    echo $a,PI;
?>

Nested inclusion is prone to relative path errors: the relative path will change due to the inclusion of the file.

The difference between include and require

The difference between include and include_once:

The include system will encounter it once and execute it once; if the same file is loaded multiple times, the system will execute it multiple times.

include_once: If the system encounters multiple times, it will only be executed once.

The difference between require and include:

The essence is the same, but when an error is encountered, the serious form of the error is different;

The error level of include is lighter; if the previous code makes an error, the latter code will be executed

The error level of require is heavier; if the previous code makes an error, the following code will not be executed

Guess you like

Origin blog.csdn.net/qq_68163788/article/details/131361759