PHP basic consolidation operators and process control
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
?>
Copy after login
<?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; ?>Copy after login
(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) ?>Copy after login
(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 <= $b;//true
var_dump($c)
?>
Copy after login
注:PHP规定使用echo输出布尔类型的时候,echo true输出为1;echo false页面什么都不输出。
(七)逻辑运算符
- &&:逻辑与,两个表达式参与运算,都为真则返回真,否则返回FALSE
- ||:逻辑或,两个表达式参与运算,一个为真就返回真,都为假返回FALSE
- !:逻辑非,一个表达式参与运算,为真则返回FALSE,为假则返回TRUE
<?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 <= $b;//true var_dump($c) ?>
注: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 charset="gbk"> <h1>其他运算符</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 charset = "gbk"> <h3>if——单向分支执行</h3> <?php $pass = 60; $score = 60; if($score >= $pass){ echo "恭喜你通过了!"; }//如果通过了就输出echo,没通过则无回显。 ?> <h3>if——双方向分支执行</h3> <?php $pass = 60; $score = 59; if($score >= $pass){ echo "Pass"; }else{ echo "挂了,sorry!"; } ?> <h3>if——多方向分支执行</h3> <?php $pass = 60; $score = 59; if($score >= 85 && $score<=100){ echo "优秀"; }elseif($score >=75){ echo "良好"; }elseif($score >=60){ echo "及格"; }else{ echo "不及格"; }
补:switch语句(分支执行)
<meta charset = "gbk"> <h3>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 < 5) ?>
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 charset = "gbk"> <h3>break语句</h3> <?php for($i = 0;$i < 6;$i++){ echo "for循环遍历0到5,开始:".$i."<br/>"; for($j=1;$j<=5;$j++){ echo $j; if($j == 2){ #break;//只跳出本层循环 break 2;//跳出两层循环 } } echo "<br>"; } ?>
5.continute语句
用在循环语句中,代表着本次循环轮空,不是结束整个循环语句。
<meta charset = "gbk"> <h3>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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
