Introduction to PHP Operators
Operators are symbols that are used to perform mathematical calculations like addition, subtraction, multiplication. PHP supports various operators to perform not only simple mathematical operations but also to perform some logical operation like AND, OR, NOT, comparison operations like greater than, less than, and many more. Operators are nothing that takes one or more value and yields another value.
Top 6 Types of PHP Operators
The different operators used in PHP is as follows:
1. Arithmetic PHP Operators
Like every programming language, PHP also supports Arithmetic operators which are used to 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, this 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 denominator.
Syntax:
$x / $y
v) Modulus Operator( % ): This operator is used to give the remainder of the division. Suppose X and Y are two Values then this modulus operator first divides the numerator by 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
2. Assignment PHP Operators
Assignment operators are used with numeric values to assign a value to a variable. In PHP, = is the basic assignment operator which means that the left Value gets set to the value of the assignment expression on the right. Below is the list of Assignment operators used in PHP
- Simple Assignment Operator( = ): This operator Assigns values from the right Values to the left Value.
- Add AND Operator( += ): This operator adds the right Value to the left Value and assign the output to the left Value.
- Subtract AND Operator( -= ): This operator subtracts the right Value from the left Value and assigns the result to the left Value.
- Multiply AND Operator( *= ): This operator multiplies the right Value with the left Value and assigns the result to the left Value.
- Divide AND Operator( /= ): This operator divides the left Value with the right Value and assigns the result to the left Value.
- Modulus AND Operator( %= ): This operator takes modulus using two Values and assigns the result to the left Value.
3. Comparison of PHP Operators
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
4. Increment/Decrement PHP Operators
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—
5. String PHP Operators
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
6. Logical PHP Operators
Logical operators are used to combining 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
Conclusion
It plays an important role in PHP when it comes to mathematical calculations. It also supports various operators like logical operators, string operators, etc.
Recommended Articles
This is a guide to PHP Operators. Here we discuss the top 6 types of PHP Operators i.e. Arithmetic, Assignment, Comparison, Increment/Decrement, Logical, String in detailed manner. You may also look at the following articles to learn more –
5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses