YouTip LogoYouTip

Php Operators

PHP Operators | Rookie Tutorial

This chapter 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 (remainder of division) Remainder of x divided by y 5 % 2
10 % 8
10 % 2
1
2
0
-x Negation Opposite sign of x -2
~x Bitwise NOT Inverts the bits of x. Rule: ~1=-2; ~0=-1; -3
a . b Concatenation Connects two strings "Hi" . "Ha" HiHa

The following example demonstrates different results obtained by using different arithmetic operators:

Example

<?php
$x=10; 
$y=6;
echo ($x + $y);
echo '<br>'; 
echo ($x - $y);
echo '<br>'; 
echo ($x * $y);
echo '<br>'; 
echo ($x / $y);
echo '<br>'; 
echo ($x % $y);
echo '<br>'; 
echo -$x;
?>

Try it Β»

The PHP7+ version adds an integer division operator intdiv(), which returns the integer quotient (floor) of the first argument divided by the second argument. 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 right expression. That is, the value of $x = 5 is 5.

Operator Equivalent to Description
x = y x = y 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 (remainder of division)
a .= b a = a . b Concatenates two strings

The following example demonstrates different results obtained by using different assignment operators:

Example

<?php
$x=10; 
echo $x; 
$y=20; 
$y += 100;
echo $y; 
$z=50;
$z -= 25;
echo $z; 
$i=5;
$i *= 6;
echo $i; 
$j=10;
$j /= 5;
echo $j; 
$k=15;
$k %= 4;
echo $k; 
?>

Try it Β»

The following example demonstrates the same results obtained by using different string operators:

Example

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; 
$x="Hello";
$x .= " world!";
echo $x; 
?>

Try it Β»


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 results obtained by using increment/decrement operators:

Example

<?php
$x=10; 
echo ++$x; 
$y=10; 
echo $y++; 
$z=5; 
echo --$z; 
$i=5; 
echo $i--; 
?>

Try it Β»


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 58 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 different results obtained by 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);
?>

Try it Β»


PHP Logical Operators

Operator Name Description Example
x and y And Returns true if both x and y are true x=6 y=3 (x < 10 and y > 1) returns true
x or y Or Returns true if at least one of x or y is true x=6 y=3 (x==6 or y==5) returns true
x xor y Xor Returns true if exactly one of x or y is true x=6 y=3 (x==6 xor y==3) returns false
x && y And Returns true if both x and y are true x=6 y=3 (x < 10 && y > 1) returns true
x || y Or Returns true if at least one of x or y is true x=6 y=3 (x==5 || y==5) returns false
!x Not Returns true if x is not true x=6 y=3 !(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 equal to y

The following example demonstrates different results obtained by using some array operators:

Example

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

Try it Β»


Ternary Operator

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

Syntax

(expr1) ? (expr2) : (expr3)

The value of the expression is expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

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

Example

The following example uses the $_GET request to check if the 'user' value exists. If it does, return $_GET['user'], otherwise return 'nobody'.

Example

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

Note: PHP_EOL is a newline constant that is compatible across platforms.

In PHP7+, there is an additional NULL coalescing operator ??, as shown below:

Example

<?php
$username = $_GET['user'] ?? 'nobody';
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
?>

Combined Comparison Operator (PHP7+)

PHP7+ supports the combined comparison operator (also called the spaceship operator), symbolized as <=>. This operator makes it easy to compare two variables, not limited to numeric data types.

Syntax:

$c = $a <=> $b;

Interpretation:

  • If $a > $b, then $c is 1.
  • If $a == $b, then $c is 0.
  • If $a < $b, then $c is -1.

Example:

Example

<?php
echo 1<=>1;
echo 1<=>2;
echo 2<=>1;
echo 1.5<=>1.5;
echo 1.5<=>2.5;
echo 2.5<=>1.5;
echo "a"<=>"a";
echo "a"<=>"b";
echo "b"<=>"a";
?>

Operator Precedence

The following table lists operators from highest to lowest precedence. Operators on the same row have equal precedence, and their associativity determines the evaluation order.

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

Associativity Operator 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 operators and string operators
Left << >> Bitwise operators
None == != === !== <> Comparison operators
Left & Bitwise operators and references
Left ^ Bitwise operators
Left | Bitwise operators
Left && Logical operators
Left || Logical operators
Left ? : Ternary operator
Right = += -= *= /= .= %= Assignment operators
← Php If ElsePhp String β†’