Operators are symbols for mathematical calculations like addition, subtraction, and multiplication. PHP supports various operators to perform simple mathematical operations and logical operations like AND, OR, NOT, comparison operations like more significant than, less than, and many more. Operators are nothing that takes one or more values and yields another value.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The different operators used in PHP are as follows:
Like every programming language, PHP also supports Arithmetic operators, which perform simple arithmetical operations, such as addition, subtraction, division, multiplication, etc.
i) Addition Operator( + ): This operator is used to add two Values. Suppose X and Y are two Values; these plus operators will add up these two Values X + Y.
Syntax:
$x + $y
ii) Subtraction Operator( – ): This operator is used to subtracting two Values. Suppose X and Y are two Values; then this minus operator will subtract the value of the second value from the first value.
Syntax:
$x - $y
iii) Multiplication Operator( * ): This operator is used to multiply two Values. Suppose X and Y are two Values; then this multiplication operator will multiply X with Y.
Syntax:
$x * $y
iv) Division Operator( / ): This operator is used to numerator by the denominator. Suppose X and Y are two Values; this division operator divides the numerator by the denominator.
Syntax:
$x / $y
v) Modulus Operator( % ): This operator gives the remainder of the division. Suppose X and Y are two Values; this modulus operator divides the numerator by the denominator and gives the remainder.
Syntax:
$x % $y
vi) Exponentiation( ** ): This operator is used to raise one quantity to the power of another value. Suppose X and Y are two Values; then this exponentiation operator raises the value of X to the power Y.
Syntax:
$x ** $y
We assign a value to a variable using assignment operators with numeric values. The basic assignment operator in PHP is =, which sets the value of the assignment expression on the right to the left value. Below is the list of Assignment operators used in PHP
The PHP comparison operators are used to compare two values; those values can be numbers or strings.
i) Equal to( == ): This operator returns True if both the operands are equal.
Syntax:
$x == $y
ii) Identical( === ): This operator returns True if both the operands are equal and are of the same type.
Syntax:
$x === $y
iii) Not Identical( !== ): This operator returns True if both the operands are not equal and are of different types.
Syntax:
$x !== $y
iv) Not Equal( <> ): This operator returns True if both the operands are unequal.
Syntax:
$x <> $y
v) Not Equal( != ): This operator returns True if both the operands are unequal.
Syntax:
$x != $y
vi) Less Than( < ): This operator returns True if $x is less than $y.
Syntax:
$x < $y
vii) Greater Than( > ): This operator returns True if $x is greater than $y.
Syntax:
$x > $y
viii) Less Than or Equal To( <= ): This operator returns True if $x is less than or equal to $y.
Syntax:
$x <= $y
ix) Greater Than or Equal To( >= ): This operator returns True if $x is greater than or equal to $y.
Syntax:
$x >= $y
These are called the unary operators as it operates on single operands. These operators are used to increment or decrement values.
i) Pre-Increment( ++ ): This operator initially increments $x by one, then return $x.
Syntax:
++$x
ii) Pre-Decrement( — ): This operator initially decrements $x by one, then return $x.
Syntax:
--$x
iii) Post-Increment( ++ ): This operator First returns $x, then increments it by one.
Syntax:
$x++
iv) Pre-Decrement( — ): This operator first returns $x, then decrement it by one.
Syntax:
$x—
String Operators are implemented over strings.
i) Concatenation( . ): This operator Concatenates Two strings.
Syntax:
$text1.$text2
ii) Concatenation and assignment( .= ): This operator Appends two strings.
Syntax:
$text1.$text2
Logical operators are used to combine conditional statements.
i) AND: This operator returns true if both the operands are true; else returns false.
Syntax:
$x and $y
ii) OR: This operator returns true if either of the operands is true; else returns false.
Syntax:
$x or $y
iii) XOR: This operator returns true if either of the operands is true, and if both are true, then I will return false.
Syntax:
$x xor $y
iv) &&: This operator returns true if both the operands are true; else returns false.
Syntax:
$x && $y
v) NOT: This operator returns True if $x is false.
Syntax:
!$x
It plays a vital role in PHP when it comes to mathematical calculations. It also supports various operators like logical operators, string operators, etc.
The above is the detailed content of PHP Operators. For more information, please follow other related articles on the PHP Chinese website!