A simple tutorial on flow control statements of PHP basics
Any PHP script is composed of a series of statements. A statement can be an assignment statement, a function call, a loop, a conditional statement or even a statement that does nothing (empty statement). Statements usually end with a semicolon.
From the perspective of execution mode, the control structure of the statement is divided into the following three types:
1. Sequential structure : Completely executed sequentially from the first statement to the last statement;
2. Selection structure: based on user input or the middle of the statement As a result, several tasks are performed;
#3. Loop structure: According to a certain Conditional execution of a task several times, or until the goal is achieved.
##There are threecontrol statements in PHP used to implement selection structures and loop structures:
1. Conditional control statement: if , else, elseif and switch;
2. Loop control statements: foreach, while, do while and for;
3. Transfer control statements: break, continue and return.
Conditional control statement:
If statement, usage: ##
If(A) ## Statement1;
Else
Statement2;
Analysis: If A is true, execute statement1; otherwise, execute statement2.
Example, code:
<?php
$a = 59; //根据$a的值,判断是否及格。如果>=60则输出及格
if($a>=60){
echo “及格”;
}else
echo “不及格”;
?>
If···elseif· ··else statement, usage:
##If(A)
Statement1;
Elseif(B)
Statement2;
Else
Statement3;
解析:如果A为TRUE,则执行statement1。否则,如果B的值为TRUE,则statement2;否则执行statement3。当然:if语句也可以嵌套。
下面是个If···elseif···else的例子:
<?php $a = 59; if($a>=60) //在大于等于60的情况里在进行分类 { if($a==100) echo “满分”; elseif($a>=90) echo “优秀”; else echo “及格”; } else echo “不及格”; ?>
Switch语句,语法如下:
Switch(A)
{
Case val1:
Statement1;
Break;
Case val2:
Statement2;
Break;
Default:
Statement3;
}
当一个case语句中的值和switch表达式A的值匹配时,PHP开始执行语句,直到switch程序段结束或者遇到第一个break语句为止
(如果没有遇到break,则PHP将继续执行下一个case)。
下面是一个没有break的例子:
<?php switch($leve1) { case 3: echo “高级”; case 2: echo “中级”; case 1: echo “初级”; default: echo “错误的等级值”; } ?>
由此你想到了什么??
<?php $level = 3; switch($level) { case 3: echo “赋予管理员权限”; case 2: echo “赋予站务权限”; case 1: echo “赋予版主权限”; default: echo “赋予普通用户权限”; } ?>
与if相比switch达到了更高的效率:
<?php $a = 59; switch($a) { case $a == 100; echo “满分”; break; case $a >= 90; echo “优秀”; break; case $a >= 60; echo “及格”; break; default: echo “不及格”; } ?>
那么循环语句是干嘛用的呢?当然是用于反复地执行某一个操作。
While 与do···while
While的语法:
While(A)
Statement;
解析:只要while表达式中的A为TRUE,就执行statement。
do···while的语法:
do
{
Statements;
}
while(A)
do···while与while的区别只是在循环结束时do···while进行检查,不管循环的条件满足与否,do···while都将执行一次。
例如:
<?php $a = 5; //先判断$a是否大于5,如果大于5则执行。 while($a>5) { echo “This is while.”; $a–; } do //先执行do之内的语句,然后进行判断。 { echo “This is do…while.”; $a–; } while($a > 5) ?>
For语句,语法:
For(A;B;C)
Statement;
分析:第一个表达式在循环开始时先无条件的执行一次,一般A都为赋值语句;B在循环开始前运行,如果为TRUE,
则继续循环,执行循环的嵌套语句;C在循环之后执行,一般都是自加自减运算。
代码:
<?php for($a = 5;$a > 5;$a–); echo “This is for”; ?>
Foreach语句,用于数组的遍历,以后将会学到。
转移控制语句
PHP中主要有三种转移控制语句:break、continue和return。
1、 break语句
break语句用于结束当前循环,break可以接受一个可选的数字参数来决定跳出几重循环。
例子:
<?php $a = 5; $b = 10; while($a <100) //$a<100开始循环 { echo “a = “.$a.”<BR>”; //输出$a,“.”时连接运算符,相当于java中的“+” while($b > 0) //$b>0,开始循环 { echo “b = ” .$b.”<BR>”; //输出$b $b–; if($b == 3 ) //如果$b==3,则跳出while($b>0) break; } $a++; if($a == 30) break; //如果$a==30,就跳出while($a<100) } ?>
Continue语句
Continue用于跳出本次循环,与break不同的是,continue跳出后将继续执行下一次循环。
Return语句 Return语句用于结束一个函数或者一个脚本文件。如果在一个函数中调用return语句将立即结束这个函数的执行,并将它的值作为参数返回。
当然,在PHP中也可以将return当做一个函数来使用。如return(),并在括号内写上要返回的参数。这种用法并不常见。
The above is the detailed content of A simple tutorial on flow control statements of PHP basics. For more information, please follow other related articles on the PHP Chinese website!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
