This article brings you relevant knowledge about PHP, which mainly introduces issues related to operators and process control, including the classification and definition of operators and some common operators Usage, as well as sequence execution, branch execution, loop execution and other related content of process control. I hope it will be helpful to everyone.
PHP Video Tutorial"
Operator
(1) Definition
Operator is a symbol that performs a certain operation on one or more operands (variables or values) , so it is also called an operator.
(2) Classification
- ## Assignment operator
- Arithmetic operators
## Comparison operators- Logical operators
- Concatenation operators
- Error suppression Symbol
- Ternary operator
- Self-operation operator
- Bitwise operators
- ##(3) Arithmetic operators
<?php $a = 5;
$b = 6;
echo -$a;echo "<br>"; //-5
echo $a - $b;echo "<br>"; //-1
echo $a * $b;echo "<br>"; //30
echo $a / $b;echo "<br>";//6/5
echo $a%$b;echo "<br>"; //5
echo $a++;echo "<br>";//6,此时$a的值为6
echo $a;echo "<br>";//6
echo ++$a;echo "<br>";//7
echo $a--;echo "<br>";//7,此时$a的值为6
echo $a;echo "<br>";//6
echo --$a;echo "<br>";//5
?>
(4) String operator (connection operator)
In PHP, use
.for string splicing, also called the connection operator;and in JS, use for string splicing, visit The attributes in the object use .
<?php $name = "xiaofeng"; $str = "hello,".$name;echo "<br>";//.讲$name和hello,拼接起来 //$str = "Hello," + $name;echo "<br>";//0 echo $str; ?>
(5) Assignment operator
=: The right side is assigned to the left side
- .=: The left side is connected to the right side String, and then assign it to the left
- =: add the result on the left to the right, and then assign it to the left
- -=: Subtract the result of the right from the left, and then assign it to the left
- *=: Multiply the result of the left by the right , and then assign the value to the left
- /=: the result of the left cooking skill on the right, and then assign the value to the left
- %=: Take the result on the left modulo the result on the right, and then assign it to the left
<?php $a = 6;$b =5; $b +=$a;echo $b;echo '<br>';//11,此时$b=11 $b -=$a;echo $b;echo '<br>';//11-6=5,此时$b=5 $b *=$a;echo $b;echo '<br>';//5*6=30,此时$b=30 $b /=$a;echo $b;echo '<br>';//30/6=5,此时$b=5 $b %=$a;echo $b;echo '<br>';//5 $b .=$a;echo $b;echo '<br>';//56 .相当于字符串连接符讲5和6拼接起来了,属于字符串 var_dump($b) ?>
(6) Comparison operators
<?php $a = 6;$b = 4;
$c = $a == $b;//false
$c = $a === $b; //false
$c = $a != $b;//true
$c = $a !== $b;//true
$c = $a <> $b;//true
$c = $a > $b;//true
$c = $a >= $b;//true
$c = $a
注:PHP规定使用echo输出布尔类型的时候,echo true输出为1;echo false页面什么都不输出。
(七)逻辑运算符
- &&:逻辑与,两个表达式参与运算,都为真则返回真,否则返回FALSE
- ||:逻辑或,两个表达式参与运算,一个为真就返回真,都为假返回FALSE
- !:逻辑非,一个表达式参与运算,为真则返回FALSE,为假则返回TRUE
注:PHP规定使用echo输出布尔类型的时候,echo true输出为1;echo false页面什么都不输出。
- &&:逻辑与,两个表达式参与运算,都为真则返回真,否则返回FALSE
- ||:逻辑或,两个表达式参与运算,一个为真就返回真,都为假返回FALSE
- !:逻辑非,一个表达式参与运算,为真则返回FALSE,为假则返回TRUE
<?php $a = true; $b = false; $c = $a and $b;//true,赋值运算的优先级比and高 $c = ($a and $b);//flase $c = ($a && $b);//false $c = ($a or $b);//true $c = ($a || $b);//true $c = ($a xor $b);//true $c = !$a;//flase var_dump($c) ?>
(八)运算符的优先级
谁的优先级高就先算谁的,并且规定了从哪个方向开始算的问题。
(九)其他运算符
<meta> <h1 id="其他运算符">其他运算符</h1> <?php #?问号——三元运算符 $a = 10; $b = 20; $c = $a>$b?$a:$b;//20 意思是如果a>b,输出a反之输出b echo $c;echo "<hr>"; #~反引号——执行cmd命令 $cmd="whoami"; echo "<pre class="brush:php;toolbar:false">".`$cmd`;//执行whoami命令 $d = "net user"; echo `$d`;echo "<hr>"; #@符号——屏蔽运算错误 $name; echo @$name;//屏蔽没有定义的错误 ?>
流程控制
(一)顺序执行
自上而下的执行即可,PHP语句默认执行的过程就是顺序执行这点跟PHP一样。
<?php echo "first";echo "<hr>"; echo "second";echo "<hr>"; echo "third";echo "<hr>"; ?>
(二)分支执行
- 单向条件
- 双向条件
- 多向条件
<meta> <h3 id="if-单向分支执行">if——单向分支执行</h3> <?php $pass = 60; $score = 60; if($score >= $pass){ echo "恭喜你通过了!"; }//如果通过了就输出echo,没通过则无回显。 ?> <h3 id="if-双方向分支执行">if——双方向分支执行</h3> <?php $pass = 60; $score = 59; if($score >= $pass){ echo "Pass"; }else{ echo "挂了,sorry!"; } ?> <h3 id="if-多方向分支执行">if——多方向分支执行</h3> <?php $pass = 60; $score = 59; if($score >= 85 && $score=75){ echo "良好"; }elseif($score >=60){ echo "及格"; }else{ echo "不及格"; }
补:switch语句(分支执行)
<meta> <h3 id="switch-分支执行">switch——分支执行</h3> <?php $day = 29; switch($day){ case 30: echo "小月"; break; case 31: echo "大月"; break; case 28: echo "平月"; break; case 29: echo "没有一个月是29天的!"; } ?>
用switch语句注意一下几点:
- case后面的语句是不需要()的
- 每个case后面都不要忘记后面跟上break语句跳出循环
- 如果case后面没有接上break,说明内容是同下的!
(三)循环执行
- while语句
- do...while语句
1.while语句
<?php $i = 0;//计数器 while($i < 4){ //循环条件 echo $i++."<hr>";//.是将来分割线连接起来,相当于Python里面的end="" } ?> <hr> <?php $i = 1; //int(0)是flase,flase是不会循环 while($i){ echo $i++."<br>"; if($i == 2){ break; } } ?>
2.do...while循环
<?php $i = 0; do { echo $i++."<br>"; }while($i
3.for语句
<?php for($i = 0;$i < 6;$i++){ echo "for循环遍历0到5,开始:"."$i"."<hr>"; } ?>
附加练习:用php输出乘法口诀
<?php for($i = 1;$i < 10;$i++){ for($j = 1;$j < $i+1;$j++){ echo $i.'x'.$j.'='.$i*$j." "; } echo "<br>"; } ?>
4.break语句
用于for、while、do...while、foreach、switch中断这些语句!后面用数字表示跳出几层循环,默认没有数字就表示跳出当前循环。
<meta> <h3 id="break语句">break语句</h3> <?php for($i = 0;$i < 6;$i++){ echo "for循环遍历0到5,开始:".$i."<br/>"; for($j=1;$j"; } ?>
5.continute语句
用在循环语句中,代表着本次循环轮空,不是结束整个循环语句。
<meta> <h3 id="continue语句">continue语句</h3> <?php for($i = 0;$i < 6;$i++){ if($i == 3){ continue;//当i=3的时候,结束! } echo "for循环遍历0到5,开始:"."$i"."<hr>"; } ?>
6.exit()语句
用处是结束当前整个php脚本,awd的时候经常用到包括die()语句也是!
<?php for($i = 0;$i < 6;$i++){ if($i == 3){ exit("整个脚本到此执行完毕了哦!");//当i=3的时候,整个脚本结束! } echo "for循环遍历0到5,开始:"."$i"."<hr>"; } ?>
推荐学习:《PHP视频教程》
The above is the detailed content of PHP basic consolidation operators and process control. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

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.