Home > Article > Backend Development > What are the php operators?
php operators include arithmetic operators, assignment operators, comparison operators, logical operators, bit operators, string operators and ternary conditional operators (?:), array operators, types operators etc. 1. Arithmetic operators are used to perform basic arithmetic operations; 2. Assignment operators are used to assign values to variables; 3. Comparison operators are used to compare the size relationship between two values and return a Boolean value (true or false); 4. Logical operators: used to perform logical operations, return Boolean values (true or false), etc.
The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.
PHP is a widely used server-side scripting language with a rich set of operators for implementing various mathematical and logical operations. In this article, we will introduce the commonly used operators in PHP, as well as their functions and usage.
1. Arithmetic operators: used to perform basic arithmetic operations, including addition (), subtraction (-), multiplication (*), division (/), modulo (%) and exponentiation (* *). For example:
$a=5; $b=3; echo$a+$b;//输出8 echo$a-$b;//输出2 echo$a*$b;//输出15 echo$a/$b;//输出1.6666666666667 echo$a%$b;//输出2 echo$a**$b;//输出125
2. Assignment operator: used to assign values to variables. Common assignment operators include "=", "=", "-=", "*=", "/=" and "%=". For example:
$a=5; $b=3; $a+=$b;//等同于$a=$a+$b,结果为8 $a-=$b;//等同于$a=$a-$b,结果为5 $a*=$b;//等同于$a=$a*$b,结果为15 $a/=$b;//等同于$a=$a/$b,结果为5 $a%=$b;//等同于$a=$a%$b,结果为2
3. Comparison operator: used to compare the size relationship between two values and return a Boolean value (true or false). Common comparison operators are "==", "!=", "6745434bf92a2979d589ede8e8e7bea9", "0e255770985d87a4229666e91fd6140f=". For example:
$a=5; $b=3; var_dump($a==$b);//输出false var_dump($a!=$b);//输出true var_dump($a<$b);//输出false var_dump($a>$b);//输出true var_dump($a<=$b);//输出false var_dump($a>=$b);//输出true
4. Logical operators: used to perform logical operations and return Boolean values (true or false). Common logical operators are "&&" (and), "||" (or) and "!" (not). For example:
$a = true;
$b = false;
var_dump($a && $b); // Output false
var_dump ($a || $b); // Output true
var_dump(!$a); // Output false
5. Bit operators: used to perform bit-level operations on integers . Common bitwise operators include "&" (and), "|" (or), "^" (exclusive or), "~" (negation), "ce3916c902be40088b00e3523adbd5f7>" (shift right). For example:
$a=5;//二进制表示为00000101 $b=3;//二进制表示为00000011 echo$a&$b;//输出1 echo$a|$b;//输出7 echo$a^$b;//输出6 echo~$a;//输出-6 echo$a<<1;//输出10 echo$a>>1;//输出2
6. String operator: used to connect two strings. A common string operator is "." (dot), for example:
$a="Hello"; $b="World"; echo$a.$b;//输出HelloWorld
In addition to the operators mentioned above, PHP also includes some other special operators, such as the ternary conditional operator ( ?:), array operators, type operators, etc.
To sum up, PHP provides a rich variety of operators, allowing developers to easily perform various mathematical and logical operations. Proficiency in these operators is very important for writing efficient PHP code.
The above is the detailed content of What are the php operators?. For more information, please follow other related articles on the PHP Chinese website!