PHP operators and control structures_PHP tutorial
操作符
操作符是用来对数组和变量进行某种操作运算的符号。
1、算术操作符
操作符 |
名称 |
示例 |
+ |
加 |
$a+$b |
- |
减 |
$a-$b |
* |
乘 |
$a*$b |
/ |
除 |
$a/$b |
% |
取余 |
$a%$b |
2、复合赋值操作符
操作符 |
使用方法 |
等价于 |
+= |
$a+=$b |
$a=$a+$b |
-= |
$a-=$b |
$a=$a-$b |
*= |
$a*=$b |
$a=$a*$b |
/= |
$a/=$b |
$a=$a/$b |
%= |
$a%=$b |
$a=$a%$b |
.= |
$a.=$b |
$a=$a.$b |
前置递增递减和后置递增递减运算符:
$a=++$b;
$a=$b++;
$a=--$b;
$a=$b--;
3、比较运算符
操作符 |
名称 |
使用方法 |
= = |
等于 |
$a= =$b |
= = = |
恒等 |
$a= = =$b |
!= |
不等 |
$a!=$b |
!= = |
不恒等 |
$a!= =$b |
<> |
不等 |
$a<>$b |
< |
小于 |
$a<$b |
> |
大于 |
$a>$b |
<= |
小于等于 |
$a<=$b |
>= |
大于等于 |
$a>=$b |
注:恒等表示只有两边操作数相等并且数据类型也相当才返回true;
例如:0= ="0" 这个返回为true ,因为操作数相等
0= = ="0" 这个返回为false,因为数据类型不同
4、逻辑运算符
操作符 |
使用方法 |
使用方法 |
说明 |
! |
非 |
!$b |
如果$b是false,则返回true;否则相反 |
&& |
与 |
$a&&$b |
如果$a和$b都是true,则结果为true;否则为false |
|| |
或 |
$a||$b |
如果$a和$b中有一个为true或者都为true时,其结果为true;否则为false |
and |
与 |
$a and $b |
与&&相同,但其优先级较低 |
or |
或 |
$a or $b |
与||相同,但其优先级较低 |
操作符"and"和"or"比&&和||的优先级要低。
5、三元操作符
Condition ? value if true : value if false
示例:($grade>=50 ? "Passed" : "Failed")
6、错误抑制操作符:
$a=@(57/0);
除数不能为0,会出错,所以加上@避免出现错误警告。
7、数组操作符
操作符 |
使用方法 |
使用方法 |
说明 |
+ |
联合 |
!$b |
返回一个包含了$a和$b中所有元素的数组 |
= = |
等价 |
$a&&$b |
如果$a和$b具有相同的元素,返回true |
= = = |
恒等 |
$a||$b |
如果$a和$b具有相同的元素以及相同的顺序,返回true |
!= |
非等价 |
$a and $b |
如果$a和$b不是等价的,返回true |
<> |
非等价 |
如果$a和$b不是等价的,返回true |
|
!= = |
非恒等 |
$a or $b |
如果$a和$b不是恒等的,返回true |
操作符的优先级和结合性:
一般地说,操作符具有一组优先级,也就是执行他们的顺序。
操作符还具有结合性,也就是同一优先级的操作符的执行顺序。这种顺序通常有从左到右,从右到左或者不相关。
下面给出操作符优先级的表。最上面的操作符优先级最低,按着表的由上而下的顺序,优先级递增。
操作符优先级
结合性 |
操作符 |
左 |
, |
左 |
Or |
左 |
Xor |
左 |
And |
右 |
|
左 |
= += -= *= /= .= %= &= |= ^= ~= <<= >>= |
左 |
?: |
左 |
|| |
左 |
&& |
左 |
| |
左 |
^ |
左 |
& |
不相关 |
= = != = = = = != = |
不相关 |
<<= >>= |
左 |
<< >> |
左 |
+ - . |
左 |
* / % |
右 |
! ~ ++ -- (int)(double)(string)(array)(object) @ |
右 |
[] |
不相关 |
New |
不相关 |
() |
To avoid priority confusion, you can use parentheses to avoid priorities.
Control structure
If we want to effectively respond to user input, the code needs to be judgmental. The structure that allows the program to make judgments is called a condition.
1. There are three structures of if..else loops
The first one only uses the if condition and treats it as a simple judgment. Interpreted as "what to do if something happens." The syntax is as follows:
if (expr) { statement }
where expr is the condition for judgment, usually using logical operation symbols as the condition for judgment. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
Example: This example omits the curly braces.
if ($state==1)echo "Haha " ;
?>
Special attention here is that the judgment of equality is == instead of =. ASP programmers may often make this mistake, = is an assignment.
Example: The execution part of this example has three lines, and the curly brackets cannot be omitted.
if ($state==1) {
echo "haha;
echo "
" ;
}
?>
The second is to add an else condition in addition to if , can be interpreted as "what to do if something happens, or how to solve it otherwise" The syntax is as follows:
if (expr) { statement1 } else { statement2 }
Example: Modify the above example to be more complete. Processing. Since there is only one line of instructions for execution, there is no need to add curly brackets
echo "Haha" ;
echo "
";
}
else{
echo "Haha";
echo "
";
}
?>
The third type is the recursive if..else loop, usually It is used for various decision making. It combines several if..else and applies it directly:
echo "a is bigger than b" ;
} elseif ( $a = = $b ) {
echo "a is equal to b" ;
} else {
echo "a is smaller than b" ;
}
?>
The above example only uses a two-level if..else loop to compare the two variables a and b. When actually using this kind of recursive if..else loop, please use it carefully, because too many levels of loops can easily make the design Problems with the logic, or missing braces, etc., will cause inexplicable problems in the program.
2. There is only one type of for loop with no changes. Its syntax is as follows:
for ( expr1; expr2; expr3) { statement }
where expr1 is the initial value of the condition. expr2 is the condition for judgment. Logical operators are usually used as the condition for judgment. expr3 is the condition to be executed after executing the statement. The part is used to change the conditions for the next cycle to judge, such as adding one...etc. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
The following example is written using a for loop:
echo "This is the ".$i."th loop
" ;
}
?>
3. The switch loop usually handles compound conditional judgments. Each sub-condition is part of the case instruction. In practice, if many similar if instructions are used, they can be synthesized into a switch loop.
switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; }
The expr condition is usually a variable name. The exprN after case usually represents the variable value. After the colon is the part to be executed that meets the condition. Be sure to use break to break out of the loop.
case "Mon" :
echo "Today is Monday" ;
break;
case "Tue" :
echo "Today is Tuesday" ;
break;
case "Wed " :
echo "Today is Wednesday" ;
break;
case "Thu" :
echo "Today is Thursday" ;
break;
case "Fri" :
echo "Today is Friday" ;
break;
default:
echo "Today is a holiday" ;
break;
}
?>
What needs to be noted here is break; don’t omit it, default, omission is okay.
Obviously, using an if loop in the above example is very troublesome. Of course, when designing, the conditions with the greatest probability of occurrence should be placed at the front and the conditions with the least occurrence at the end, which can increase the execution efficiency of the program. In the above example, since the probability of occurrence is the same every day, there is no need to pay attention to the order of the conditions.

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

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

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

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

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,

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

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.
