Skip to main content

PHP Operators

By SamK
0
0 recommends
Category(s)
Topic(s)

PHP operators are utilized to execute operations on variables and values.

Operators are categorized into the following groups, each serving specific purposes:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

PHP arithmetic operators are employed with numeric values to execute common arithmetical operations, including addition, subtraction, multiplication, and more.

Addition: + => $x + $y : Sum of $x and $y
Subtraction: - => $x - $y : Difference of $x and $y
Multiplication: * => $x * $yProduct of $x and $y 
Division: / => $x / $y : Quotient of $x and $y
Modulus: % => $x % $y : Remainder of $x divided by $y
Exponentiation: ** => $x ** $y : Result of raising $x to the $y'th power

// Example usage
<?php
$x = 8;
$y = 4;

echo $x + $y;  // Output: 12
echo $x - $y;  // Output: 4
echo $x * $y;  // Output: 32
echo $x / $y;  // Output: 2
echo $x % $y;  // Output: 0
echo $x ** $y;  // Output: 4096
?>

PHP Assignment Operators

PHP assignment operators are utilized with numeric values to assign a value to a variable.

The fundamental assignment operator in PHP is "=", indicating that the left operand is set to the value of the expression on the right.

$x = $y  : The left operand gets set to the value of the expression on the right
$x = $x + $y => $x += $y : Addition assignment
$x = $x - $y => $x -= $y : Subtraction assignment
$x = $x * $=> $x *= $y : Multiplication assignment
$x = $x / $y => $x /= $y : Division assignment
$x = $x % $y => $x %= $yModulus assignment

// Example usage
<?php
$X = 8;
$y = 4;

echo $x = $y  // Output: 4
echo $x = $x + $y  // Output: $x = 12
echo $x = $x - $y  // Output: $x = 4
echo $x = $x * $y  // Output: $x = 32
echo $x = $x / $y  // Output: $x = 2
echo $x = $x % $y  // Output: $x = 0
?>

PHP Comparison Operators

PHP comparison operators are utilized to compare two values, whether they are numbers or strings.

Equal: == => $x == $y : Returns true if $x is equal to $y
Identical: === => $x === $y : Returns true if $x is equal to $y, and they are of the same type
Not equal: != => $x != $y : Returns true if $x is not equal to $y
Not equal: <> => $x <> $y : Returns true if $x is not equal to $y
Not identical: !== => $x !== $y : Returns true if $x is not equal to $y, or they are not of the same type
Greater than: > => $x > $y : Returns true if $x is greater than $y
Less than: < => $x < $y : Returns true if $x is less than $y
Greater than or equal to: >= => $x >= $y : Returns true if $x is greater than or equal to $y
Less than or equal to: <= => $x <= $y : Returns true if $x is less than or equal to $y
Spaceship: <=> => $x <=> $y : Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.

// Example usage
<?php
$x = 8;
$y = 4;

var_dump($x == $y); // Output: bool(false)
var_dump($x === $y); // Output: bool(false)
var_dump($x != $y); // Output: bool(true)
var_dump($x <> $y); // Output: bool(true)
var_dump($x !== $y); // Output: bool(true)
var_dump($x > $y); // Output: bool(true)
var_dump($x < $y); // Output: bool(false)
var_dump($x >= $y); // Output: bool(true)
var_dump($x <= $y); // Output: bool(false)
var_dump($x <=> $y); // Output: int(1)
?>

PHP Increment / Decrement Operators

PHP increment operators are used to increase a variable's value. PHP decrement operators are used to decrease a variable's value.

Pre-increment: ++$x : Increments $x by one, then returns $x
Post-increment: $x++ : Returns $x, then increments $x by one
Pre-decrement: --$x : Decrements $x by one, then returns $x
Post-decrement: $x-- : Returns $x, then decrements $x by one

// Example usage
<?php
$x = 8;

echo ++$x;  // Output 9
echo $x++;  // Output 8
echo --$x;  // Output 7
echo $x--;  // Output 8
?>

PHP Logical Operators

PHP logical operators are used to combine multiple conditional statements.

And: and => $x and $y : True if both $x and $y are true
Or: or => $x or $y : True if either $x or $y is true
Xor: xor => $x xor $y : True if either $x or $y is true, but not both
And: && => $x && $y : True if both $x and $y are true
Or: || =>  $x || $y : True if either $x or $y is true
Not: ! => !$x : True if $x is not true

// Example usage
<?php
$x = 80;  
$y = 40;

if ($x == 80 and $y == 50) {
    echo "Wellcome";  // No output
}
if ($x == 80 or $y == 50) {
    echo "Wellcome";  // Output: Wellcome
}
if ($x == 80 xor $y == 50) {
    echo "Wellcome";  // Output: Wellcome
}
if ($x == 80 && $y == 50) {
    echo "Wellcome";  // No output
}
if ($x == 80 || $y == 50) {
    echo "Wellcome"; // Output: Wellcome
}
if (!($x == 80)) {
    echo "Wellcome"; // No output
}
?>

PHP String Operators

PHP has two operators specifically designed for strings.

Concatenation . => $x1 . $x2 : Concatenation of $x1 and $x2

// Example usage
<?php
$txt1 = "Welcome";
$txt2 = " to my website.";
echo $txt1 . $txt2;
?>

/*
Output:
Welcome to my website.
*/

Concatenation assignment .= => $x1 .= $x2 : Appends $x2 to $x1

// Example usage
<?php
$txt1 = "Welcome";
$txt2 = " to my website.";
$txt1 .= $txt2;
echo $txt1;
?>

/*
Output:
Welcome to my website.
*/

PHP Array Operators

PHP array operators are used to compare arrays.

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

// Example usage
<?php
$x = array("a" => "apple", "b" => "banana");  
$y = array("c" => "orange", "d" => "watermelon");

print_r($x + $y); // Array ( [a] => apple [b] => banana [c] => orange [d] => watermelon ) 
var_dump($x == $y); // bool(false)
var_dump($x === $y); // bool(false)
var_dump($x != $y); // bool(true)
var_dump($x <> $y); // bool(true)
var_dump($x !== $y); // bool(true)
?>

PHP Conditional Assignment Operators

PHP conditional assignment operators are used to assign a value based on certain conditions.

Ternary: ?: => $x = expr1 ? expr2 : expr3 : Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE

Null coalescing: ?? => $x = expr1 ?? expr2 : Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2.

// Example usage
<?php

$y = 2;
$z = 3;
$i = true;
$k = false;

echo $x = $i ? $y : $z; // Output: 2
echo $x = $k ? $y : $z; // Output: 3
?>

Questions & Answers