Search

PHP Operators

PHP Operator is a symbol i.e used to perform arithmetic or logical operations on operands. In simple words, operators are used to perform operations on variables or values.

PHP divides the operators in the following groups:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic operators

An operator that performs arithmetic operations on groups and numbers. Binary plus and minus symbols are used between groups and numbers to indicate addition and subtraction. etc

Operator Name Example
+Addition $a + $b
-Subtraction$a - $b
*Multiplication$a * $b
/Division$a / $b
%Modulus$a % $b
**Exponentiation$a ** $b

PHP Assignment Operators

The PHP basic assignment operator is "=". and used with numeric values to write a value to a variable.

Operator DescriptionExample
The left operand gets set to the value of the expression on the right $a = $b
+=Addition$a += $b
-=Subtraction$a -= $b
*=Multiplication$a *= $b
/=Division$a /= $b
%=Modulus$a %= $b

PHP Comparison Operators

The PHP Comparison operators — operators that compare values and return true or false .

Operator NameReturnExample
==Equal (bool)true if $a is equal to $b after type juggling.$a == $b
===Identical (bool)true if $a is equal to $b, and they are of the same $a === $b 
!=Not equal (bool)true if $a is not equal to $b after type juggling.$a != $b
<>Not equal (bool)true if $a is not equal to $b after type juggling.$a <> $b
!==Not identical (bool)true if $a is not equal to $b, or they are not of the same type.$a !== $b
<Less than (bool)true if $a is strictly less than $b.$a < $b
>Greater than (bool)true if $a is strictly greater than $b.$a > $b
<=Less than or equal to (bool)true if $a is less than or equal to $b.$a <= $b
>=Greater than or equal to (bool)true if $a is greater than or equal to $b.$a >= $b
<=>Spaceship (bool)An int less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively.$a <=> $b

PHP Increment/Decrement operators

The PHP Increment and decrement operators are unary operators that add or subtract one, to or from their operand 
Increment and decrement operators are two different category

  • Pre-increment and decrement operators.
  • Post-increment and decrement operators.
Operator NameDescription
++$aPre-incrementIncrements $a by one, then returns $a.
$a++Post-incrementReturns $a, then increments $a by one.
--$aPre-decrementDecrements $a by one, then returns $a.
$a--Post-decrementReturns $a, then decrements $a by one.

PHP Logical Operators

The PHP logical operators reason for the two different variations of "and" and "or" operators is that they operate at different precedence's.

Operator NameDescriptionExample
andAndtrue if both $a and $b are true.$a and $b
orOrtrue if either $a or $b is true.$a or $b
xorXortrue if either $a or $b is true, but not both.$a xor $b
!Nottrue if $a is not true.!$a
&&Andtrue if both $a and $b are true.$a && $b
||Ortrue if either $a or $b is true.$a || $b

PHP String Operators

There are two PHP string operators. that are specially designed for strings. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.=')

Operator NameDescription
.ConcatenationThis operator  use to concatenation tow string 
.=Concatenation assignmentThis operator  use to append string 

PHP Array Operators

Some operators are used to manage two or more arrays. like add two array or compared tow array.

Operator NameDescriptionExample
=UnionUnion of $a and $b.$a + $b
==Equalitytrue if $a and $b have the same key/value pairs.$a == $b
===Identitytrue if $a and $b have the same key/value pairs in the same order and of the same types.$a === $b
! =Inequalitytrue if $a is not equal to $b.$a != $b
<>Inequalitytrue if $a is not equal to $b.$a <> $b
! ==Non-identitytrue if $a is not identical to $b.$a !== $b

PHP Conditional  Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions like if else :

Operator NameExample
?:Ternary$a == $b ? $a : $b  
??Null coalescing$a= $a ?? $b

Stack Overlode is optimized Tutorials and examples for learning and training. Examples might be simplified to improve reading and learning and understand. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using Stack Overlode, you agree to have read and accepted our terms of use, cookie and privacy policy.