PHP Operator

tutorial and example
tutorial and example

PHP Operator is used to perform operations on variable and values.

Following are the types of Operator:

PHP Arithmetic Operator

PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Description Example
 + Addition $x + $y
 – Subtraction $x – $y
 * Multiplication $x * $y
/ Division $x / $y
 % Modulus $x %$y
** Exponentiation $x ** $y

PHP Assignment Operators

PHP assignment operator is used to write a value to a variable.

Operator Description Example
x=y , x=y The yvalueassign to X value $x=$y
x+=y , x=x+y Addition $x=$y+$z
x-=y , x=x-y Subtraction $x=$y-$z
x*=y , x=x*y Multiplication $x=$y+$z
x/=y , x=x/y Division $x=$y/$z
x%=y, x=x%y Modulus $x=$y%$z

PHP Comparison Operators

PHP comparison operator are used to compare two values (number and string)

Operator Name Example
== Equal $x == $y
=== Identical $x === $y
!= Not equal $x != $y
<> Not equal $x <> $y
!== Not identical $x !== $y
> Greater than $x > $y
< Less than $x < $y
>= Greater than or equal to $x >= $y
<= Less than or equal to $x <= $y

PHP Increment / Decrement Operators

PHP increment and decrement operator is used to increment and decrement a variable’s value.

Operator Name Description
++$x Pre-increment It Increments $x by 1, then returns $x.
$x++ Post-increment It returns $x, then increments $x by 1.
–$x Pre-decrement It decrements $x by 1, then returns $x.
$x– Post-decrement It returns $x, then decrements $x by one.

PHP Logical Operators

The PHP Operators are used to merge two or more conditional statements.

Operator Name Result
and And True if both $x and $y are true.
or Or True if either $x or $y is true.
xor Xor True if either $x or $y is true, but not both.
&& And True if both $x and $y are true.
|| Or True if either $x or $y is true.
! Not True if $x is not true.

PHP String Operators

PHP string operators are used for string concatenation.

Following are the String Operators

Operator Name Example
. Concatenation $txt1 . $txt2
.= Concatenation assignment $txt1 .= $txt2

PHP Array Operators

PHP array operator is used to compare arrays.

Operator Name Example Result
+ Union $x + $y It is union of $x and $y.
== Equality $x == $y It returns true if $x and $y have the same key/value.
=== Identity $x === $y It returns true if $x and $y have the same key/value pairs in the same order and of the same types.
!= Inequality $x != $y It returns true if $x is not equal to $y.
<> Inequality $x <> $y It returns true if $x is not equal to $y.
!== Non-identity $x !== $y It returns true if $x is not identical to $y.