Web programming

Operators in PHP

Ngoc Phuong

Ngoc Phuong

2021-02-26 . 133 view

PHP divides operators into the following groups

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Increment / Decrement Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Conditional Assignment Operators

I. Arithmetic Operators

Arithmetic operators in PHP are used to perform mathematical operations such as addition, subtraction, multiplication, division...

Operator Expression Example
+ Addition of two numbers $x + $y : 10 + 3 = 13
- Subtraction of numbers $x - $y : 10 - 3 = 7
- Multiplication of numbers $x * $y : 10 * 3 = 30
/ Division of numbers $x / $y : 10 / 3 = 3,333333
% Modulo operation $x % $y : 10 % 3 = 1
** Exponentiation operation $x ** $y : 10 ** 3 = 1000

II. Assignment operator

This is the most common operator in any language, we use the = sign to assign a value to a variable, in addition, there are some special assignment operations such as +=, -=, *=, /= ...

Operator Expression Example
x = y The value assignment operation $x = 10
x += y Addition operation x = x + y $x = 10; $x += 20; => $x = 30
x -= y Subtraction operation x = x - y $x = 10; $x -= 5; => $x = 5
x *= y Multiplication operation x = x * y $x = 10; $x *= 6; => $x = 60
x /= y Division operation x = x / y $x = 10; $x /= 5; => $x = 2
x %= y Modulus operation x = x % y $x = 10; $x %= 4; => $x = 2

III. Comparison operators

Comparison operators in PHP are used to compare strings or numbers.

Operator Expression Example
== Equality comparison does not care about the data type of the variable $x = 100;$y = "100"; $x==$y ==> Return: True
=== Equality comparison checks both the value and the data type. $x = 100; $y = "100"; $x===$y ==> Return: False
!= Inequality comparison checks both the value and the data type of the variable. $x = 100; $y = "100"; $x!=$y ==> Return: False
<> Inequality comparison $x = 100; $y = "100"; $x<>$y ==> Return: False
!== Inequality comparison without the need for matching data types. $x = 100; $y = "100"; $x!==$y ==> Return: True
> The greater than comparison $x = 100; $y = 50; $x>$y ==> Return: True
< Less than comparison. $x = 100; $y = 50; $x < $y ==> Return: True
>= Greater than or equal to comparison $x = 100; $y = 50; $x >= $y ==> Return: True
<= Less than or equal to comparison. $x = 100; $y = 50; $x <= $y ==> Return: False
<=> Combined comparison => Returns 0 if the two compared values are equal, returns 1 if the left value is larger, and returns -1 if the right value is larger. $x = 5; $y = 10; $x<=>$y ==> Return: -1
$x = 10; $y = 10; $x<=>$y ==> Return: 0
$x = 10; $y = 5; $x<=>$y ==> Return: 1

IV. Increment / Decrement operator

The increment operator in PHP is used to increase the value of a variable

The decrement operator in PHP is used to decrease the value of a variable.

Operator Expression Ví dụ
++$x Increment a value first, then return it. $x = 100; ++$x; ==> Return: 101
$x++ Increment a value after it is called. $x = 100; $x++; ==> Return: 100
--$x Decrement a value first, then return it $x = 100; --$x; ==> Return: 99
$x-- Decrement a value after it is called $x = 100; $x--; ==> Return: 100

V. Logical Operators

Logical operators are used to combine conditional statements.

Operator Expression Example
and $x and $y : TRUE nếu $x và $y all are correct

$x = 100;

$y = 50;

($x==100 and $y==50) ==> TRUE

or $x or $y : TRUE nếu 1 in 2 $x hoặc $y true

$x = 100;

$y = 50;

($x==100 or $y==200) ==> TRUE

xor- $x xor $y : TRUE if 1 in 2 $x or $y true, but both cannot be correct.

$x = 100;

$y = 50;

($x==100 xor $y==50) ==> FALSE

&& $x && $y : TRUE if $x and $y all are correct

$x = 100;

$y = 50;

($x==100 && $y==50) ==> TRUE

|| $x || $y : TRUE if 1 in 2 $x or $y true

$x = 100;

$y = 50;

($x==100 || $y==200) ==> TRUE

! !$x : TRUE if $x not TRUE

$x = 100;

($x!==90) ==> TRUE

VI. String operator

PHP has two operators that are specially designed for strings

Operator Expression Example
. $txt1.$txt2: String concatenation

$txt1 = "Xin chào ";

$txt2 = "kiemtientuweb.com";

$txt1.$txt2 ==> Return: Xin chào kiemtientuweb.com

.= $txt1.=$txt2: Add string $txt2 in $txt1

$txt1 = "Xin chào ";

$txt2 = "kiemtientuweb.com";

$txt1.=$txt2 ==> Return: Xin chào kiemtientuweb.com

VII. Array operator

Array operators are used to compare arrays

Operator Expression Example
+ $array1 + $array2: Append array

$array1 = array("a" => "red", "b" => "green");

$array2 = array("c" => "blue", "d" => "yellow");

print_r($array1 + $array2)

==> Return: Array ( [a] => red [b] => green [c] => blue [d] => yellow )

== $array1 == $array2: Compare two arrays, if the two arrays have the same key pairs and the same value pairs then return TRUE, otherwise return FALSE.

$array1 = array("a" => "red", "b" => "green");

$array2 = array("c" => "blue", "d" => "yellow");

var_dump($array1 == $array2)

==> Return: FALSE

=== $array1 === $array2: Compare two arrays, if the two arrays have the same key-value pairs, in the same order and of the same type, then return TRUE, otherwise return FALSE

$array1 = array("a" => "red", "b" => "green");

$array2 = array("c" => "blue", "d" => "yellow");

var_dump($array1 === $array2)

==> Return: FALSE

!= $array1 != $array2: Compare two arrays, if the two arrays are not equal then return TRUE

$array1 = array("a" => "red", "b" => "green");

$array2 = array("c" => "blue", "d" => "yellow");

var_dump($array1 != $array2)

==> Return: TRUE

<> $array1 <> $array2: Compare two arrays, if the two arrays are different then return TRUE

$array1 = array("a" => "red", "b" => "green");

$array2 = array("c" => "blue", "d" => "yellow");

var_dump($array1 <> $array2)

==> Return: TRUE

!== $array1 !== $array2: Compare two arrays, if the two arrays are different then return TRUE

$array1 = array("a" => "red", "b" => "green");

$array2 = array("c" => "blue", "d" => "yellow");

var_dump($array1 !== $array2)

==> Return: TRUE

VIII. Conditional assignment operator

Conditional assignment operators are used to assign a value to a variable depending on conditions.

Operator Expression Example
?: Ternary operator

$x = 10;

$y = 5;

$z = ($x > $y) ? $x : $y

==> Equivalent: if($x > $y){ $z = $x } else { $z = $y }

?? NULL coalescing operator

$x = 10;

$z = $x ?? 0;

==> Equivalent: if($x){ $z = $x } else { $z = 0 } // If $x does not exist or is NULL then $z = 0 and vice versai.

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback