What are the php operators?

zbt
Release: 2023-07-26 13:47:58
Original
1774 people have browsed it

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.

What are the php operators?

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
Copy after login

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
Copy after login

3. Comparison operator: used to compare the size relationship between two values ​​and return a Boolean value (true or false). Common comparison operators are "==", "!=", "<", ">", "<=" and ">=". 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
Copy after login

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), "<<" (shift left) and ">>" (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
Copy after login

6. String operator: used to connect two strings. A common string operator is "." (dot), for example:

$a="Hello";
$b="World";
echo$a.$b;//输出HelloWorld
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!