Easy Tutorial
❮ Func String Str Repeat Func Mysqli Field Seek ❯

PHP Operators


In this section, we will discuss the application of different operators in PHP.

In PHP, the assignment operator = is used to assign values to variables.

In PHP, the arithmetic operator + is used to add values together.


PHP Arithmetic Operators

Operator Name Description Example Result
x + y Addition Sum of x and y 2 + 2 4
x - y Subtraction Difference of x and y 5 - 2 3
x * y Multiplication Product of x and y 5 * 2 10
x / y Division Quotient of x and y 15 / 5 3
x % y Modulus (Division Remainder) Remainder of x divided by y 5 % 2 <br>10 % 8 <br>10 % 2 1 <br>2 <br>0
-x Negation Opposite of x <?php<br>$x = 2;<br>echo -$x;<br>?> -2
~x Bitwise Not Inverts the bits of x. Rules: ~1=-2; <br>~0=-1; <?php<br>$x = 2;<br>echo ~$x;<br>?> -3
a . b Concatenation Concatenates two strings "Hi" . "Ha" HiHa

The following example demonstrates the results of using different arithmetic operators:

Example

<?php 
$x=10; 
$y=6;
echo ($x + $y); // Outputs 16
echo '<br>';  // Line break

echo ($x - $y); // Outputs 4
echo '<br>';  // Line break

echo ($x * $y); // Outputs 60
echo '<br>';  // Line break

echo ($x / $y); // Outputs 1.6666666666667
echo '<br>';  // Line break

echo ($x % $y); // Outputs 4
echo '<br>';  // Line break

echo -$x;
?>

PHP 7+ introduces the integer division operator intdiv(), which returns the integer quotient of the division (rounded down). Example:

Example

<?php
var_dump(intdiv(10, 3));
?>

The above example will output:

int(3)

PHP Assignment Operators

In PHP, the basic assignment operator is =. It means that the left operand is set to the value of the expression on the right. For example, the value of $x = 5 is 5.

Operator Equivalent Description
x = y x = y The left operand is set to the value of the right expression
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus (Division Remainder)
a .= b a = a . b Concatenates two strings

The following example demonstrates the results of using different assignment operators:

Example

<?php 
$x=10; 
echo $x; // Outputs 10

$y=20; 
$y += 100;
echo $y; // Outputs 120

$z=50;
$z -= 25;
echo $z; // Outputs 25

$i=5;
$i *= 6;
echo $i; // Outputs 30

$j=10;
$j /= 5;
echo $j; // Outputs 2

$k=15;
$k %= 4;
echo $k; // Outputs 3
?>

The following example demonstrates the results of using different string operators:

Example

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // Outputs Hello world! 

$x="Hello";
$x .= " world!";
echo $x; // Outputs Hello world! 
?>

PHP Increment/Decrement Operators

Operator Name Description
++x Pre-increment Increments x by 1, then returns x
x++ Post-increment Returns x, then increments x by 1
--x Pre-decrement Decrements x by 1, then returns x
x-- Post-decrement Returns x, then decrements x by 1

The following example demonstrates the results of using increment/decrement operators:

Example

<?php
$x=10; 
echo ++$x; // Outputs 11

$y=10; 
echo $y++; // Outputs 10

$z=5;
echo --$z; // Outputs 4
?>
$i=5;
echo $i--; // Outputs 5
?>

PHP Comparison Operators

Comparison operators allow you to compare two values:

Operator Name Description Example
x == y Equal Returns true if x is equal to y 5==8 returns false
x === y Identical Returns true if x is equal to y, and they are of the same type 5==="5" returns false
x != y Not Equal Returns true if x is not equal to y 5!=8 returns true
x <> y Not Equal Returns true if x is not equal to y 5<>8 returns true
x !== y Not Identical Returns true if x is not equal to y, or they are not of the same type 5!=="5" returns true
x > y Greater Than Returns true if x is greater than y 5>8 returns false
x < y Less Than Returns true if x is less than y 5<8 returns true
x >= y Greater Than or Equal To Returns true if x is greater than or equal to y 5>=8 returns false
x <= y Less Than or Equal To Returns true if x is less than or equal to y 5<=8 returns true

The following example demonstrates the results of using some comparison operators:

Example

<?php
$x=100; 
$y="100";

var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";

$a=50;
$b=90;

var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?>

PHP Logical Operators

Operator Name Description Example
x and y And Returns true if both x and y are true x=6 <br>y=3 <br>(x < 10 and y > 1) returns true
x or y Or Returns true if either x or y is true x=6 <br>y=3 <br>(x==6 or y==5) returns true
x xor y Xor Returns true if either x or y is true, but not both x=6 <br>y=3 <br>(x==6 xor y==3) returns false
x && y And Returns true if both x and y are true x=6 <br>y=3 <br>(x < 10 && y > 1) returns true
x y Or Returns true if either x or y is true x=6 <br>y=3 <br>(x==5 y==5) returns false
! x Not Returns true if x is not true x=6 <br>y=3 <br>!(x==y) returns true

PHP Array Operators

Operator Name Description
x + y Union Union of x and y
x == y Equality Returns true if x and y have the same key/value pairs
x === y Identity Returns true if x and y have the same key/value pairs in the same order and of the same types
x != y Inequality Returns true if x is not equal to y
x <> y Inequality Returns true if x is not equal to y
x !== y Non-identity Returns true if x is not identical to y

The following example demonstrates the results of using some array operators:

Example

<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

Ternary Operator

Another conditional operator is the "?:" (or ternary) operator.

Syntax

(expr1) ? (expr2) : (expr3)

expr2 is returned if expr1 evaluates to TRUE, and expr3 is returned if expr1 evaluates to FALSE.

Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, otherwise it returns expr3.

Example

The following example checks if the $_GET request contains a 'user' value. If it does, it returns $_GET['user']; otherwise, it returns 'nobody':

Example

<?php
// Standard syntax
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
echo $username, PHP_EOL;

// PHP 5.3+ syntax
$username = $_GET['user'] ?: 'nobody';
echo $username, PHP_EOL;
?>

Note: PHP_EOL is a newline character that is compatible with multiple platforms.

In PHP 7+, there is a NULL coalescing operator ??, as shown in the following example:

Example

<?php
// Returns 'nobody' if $_GET['user'] does not exist, otherwise returns $_GET['user']
$username = $_GET['user'] ?? 'nobody';
// Similar to the ternary operator
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
?>

Combined Comparison Operator (PHP 7+)

PHP 7+ supports the combined comparison operator (also known as the spaceship operator), denoted by <=>. This operator can be used to easily compare two variables, not limited to numeric data.

The syntax is as follows:

$c = $a <=> $b;

The interpretation is:

Here is an example:

Example

<?php
// Integer
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Float
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// String
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>

Operator Precedence

The table below lists operators in order of precedence from highest to lowest. Operators in the same row have the same precedence, and their associativity determines the order of evaluation.

Note: Left = left to right, Right = right to left.

Associativity Operators Additional Information
None clone new clone and new
Left [ array()
Right ++ -- ~ (int) (float) (string) (array) (object) (bool) @ Types and increment/decrement
None instanceof Types
Right ! Logical operators
Left * / % Arithmetic operators
Left + - . Arithmetic and string operators
Left << >> Bitwise operators
None == != === !== <> Comparison operators
Left & Bitwise and references
Left ^ Bitwise
Left | Bitwise
Left && Logical operators
Left || Logical operators
Left ? : Ternary operator
Right = += -= *= /= .= %= &= |= ^= <<= >>= => Assignment operators
Left and Logical operators
Left xor Logical operators
Left or Logical operators
Left , Used in multiple places

In operator precedence, or and ||, && and and are logical operators with the same effect but different precedence levels.

Example

<?php
// Precedence: && > = > and
// Precedence: || > = > or

$a = 3;
$b = false;
$c = $a or $b;
var_dump($c);          // Here $c is an int value of 3, not a boolean value of true
$d = $a || $b;
var_dump($d);          // Here $d is a boolean value of true
?>

The above example outputs:

int(3)
bool(true)

Use of Parentheses

By using parentheses to explicitly mark the order of operations, we can often increase the readability of the code, rather than relying on operator precedence and associativity.

Example

<?php
// Parentheses take precedence in calculations

$a = 1;
$b = 2;
$c = 3;
$d = $a + $b * $c;
echo $d;
echo "\n";
$e = ($a + $b) * $c;  // Using parentheses
echo $e;
echo "\n";
?>

The above example outputs:

7
9
❮ Func String Str Repeat Func Mysqli Field Seek ❯