Loading...
Loading...
00:00:00

PHP Operators

Operators in PHP are special symbols or keywords that perform specific actions on variables and values. There are several types of operators in PHP, including:

Arithmetic Operators

  1. Arithmetic Operators: Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, modulus, and exponentiation.

Arithmetic operators are used to perform mathematical operations on numeric values. The following are the arithmetic operators in PHP:

  • Addition operator +: adds two values together.
  • Subtraction operator -: subtracts one value from another.
  • Multiplication operator *: multiplies two values together.
  • Division operator /: divides one value by another.
  • Modulus operator % returns the remainder of a division operation.
  • Exponentiation operator **: raises a value to a power.
Operator Name Operations output
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product 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

For example, the expression $x + $y adds the value of $x to the value of $y.

Examples:

$x = 10;
$y = 5;

$sum = $x + $y; // Addition operator
$difference = $x - $y; // Subtraction operator
$product = $x * $y; // Multiplication operator
$quotient = $x / $y; // Division operator
$remainder = $x % $y; // Modulus operator
$power = $x ** $y; // Exponentiation operator

Comparison Operators

  1. Comparison Operators: Comparison operators are used to compare two values and return a boolean value (true or false).

Comparison operators are used to compare two values and return a boolean value. The following are the comparison operators in PHP:

  • Greater than operator >: returns true if the left operand is greater than the right operand.
  • Less than operator <: returns true if the left operand is less than the right operand.
  • Greater than or equal to operator (>=): returns true if the left operand is greater than or equal to the right operand.
  • Less than or equal to operator <=: returns true if the left operand is less than or equal to the right operand.
  • Equality operator ==: returns true if the values of both operands are equal.
  • Inequality operator !=: returns true if the values of both operands are not equal.
  • Identity operator ===: returns true if both the values and types of both operands are equal.
  • Non-identity operator !==: returns true if either the values or types of both operands are not equal.
Operator Name Operation Result
== 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.

For example, the expression $x > $y returns true if $x is greater than $y.

Examples:

$x = 10;
$y = 5;

$greater_than = $x > $y; // Greater than operator
$less_than = $x < $y; // Less than operator
$greater_than_or_equal = $x >= $y; // Greater than or equal to operator
$less_than_or_equal = $x <= $y; // Less than or equal to operator
$equal_to = $x == $y; // Equality operator (checks for value equality)
$not_equal_to = $x != $y; // Inequality operator (checks for value inequality)
$identical = $x === $y; // Identity operator (checks for value and type equality)
$not_identical = $x !== $y; // Non-identity operator (checks for value and type inequality)

Logical Operators

  1. Logical Operators: Logical operators are used to combine two or more conditions and return a boolean value.

Logical operators are used to combine two or more conditions and return a boolean value. The following are the logical operators in PHP:

  • AND operator &&: returns true if both the left and right operands are true.
  • OR operator ||: returns true if either the left or right operands are true.
  • NOT operator !: returns true if the operand is false and false if the operand is true.
Operator Name Operation Result
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

For example, the expression ($x > 0 && $y < 0) returns true if $x is greater than zero and $y is less than zero.

Examples:

$x = 10;
$y = 5;

$and = ($x > 0 && $y > 0); // AND operator
$or = ($x > 0 || $y > 0); // OR operator
$not = !($x > 0); // NOT operator

Assignment Operators

  1. Assignment Operators: Assignment operators are used to assign a value to a variable.

Assignment operators are used to assign a value to a variable. The following are the assignment operators in PHP:

  • Addition assignment operator +=: adds a value to a variable.
  • Subtraction assignment operator -=: subtracts a value from a variable.
  • Multiplication assignment operator *=: multiplies a variable by a value.
  • Division assignment operator /=: divides a variable by a value.
  • Modulus assignment operator %=: sets a variable to the remainder of dividing it by a value.
Operators Equivalent Description
x = y x = y The Left operand gets the value of the expression on Right 
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

For example, the expression $x += $y is equivalent to $x = $x + $y.

Examples:

$x = 10;
$y = 5;

$x += $y; // Addition assignment operator (equivalent to $x = $x + $y)
$x -= $y; // Subtraction assignment operator (equivalent to $x = $x - $y)
$x *= $y; // Multiplication assignment operator (equivalent to $x = $x * $y)
$x /= $y; // Division assignment operator (equivalent to $x = $x / $y)
$x %= $y; // Modulus assignment operator (equivalent to $x = $x % $y)

String Operators

  1. String Operators: String operators are used to concatenate strings.

String operators are used to concatenate strings. The following is the string operator in PHP:

  • Concatenation operator . dot : joins two strings together.

For example, the expression $name . " " . $surname concatenates the value of $name, a space, and the value of $surname together.

Examples:

$name = "John";
$surname = "Doe";

$full_name = $name . " " . $surname; // Concatenation operator

These are some of the most commonly used operators in PHP. There are many other operators available in PHP that can be used for various purposes.

Unary Operator in php

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

there are four unary operators in PHP:

  1. Unary Plus Operator +:

The unary plus operator is used to indicate that a value is positive. It can also be used to convert a string or boolean value to a numeric value. For example:

$a = 10;
$b = +$a; // $b is now equal to 10
$c = +"20"; // $c is now equal to 20
$d = +"hello"; // $d is now equal to 0

In the last example, the string "hello" cannot be converted to a numeric value, so it is converted to 0.

  1. Unary Minus Operator -:

The unary minus operator is used to indicate that a value is negative. It can also be used to negate a numeric value. For example:

$a = 10;
$b = -$a; // $b is now equal to -10
$c = -20; // $c is now equal to -20
$d = -(-10); // $d is now equal to 10
  1. Increment Operator ++:

The increment operator is used to add 1 to a variable's value. It can be used either as a pre-increment operator (the variable is incremented before the value is used) or as a post-increment operator (the variable is incremented after the value is used). For example:

$a = 10;
$b = ++$a; // $a is now equal to 11, $b is also equal to 11
$c = $a++; // $a is now equal to 12, $c is equal to 11
  1. Decrement Operator --:

The decrement operator is used to subtract 1 from a variable's value. It can be used either as a pre-decrement operator (the variable is decremented before the value is used) or as a post-decrement operator (the variable is decremented after the value is used). For example:

$a = 10;
$b = --$a; // $a is now equal to 9, $b is also equal to 9
$c = $a--; // $a is now equal to 8, $c is equal to 9

These are the four unary operators in PHP and how they can be used to modify or convert the value of a variable.

Array Operators in PHP

Operator Name Operation Output
+ 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

there are several array operators in PHP.

  1. Union Operator + :

The union operator combines two arrays into a new array that contains all the elements from both arrays. If an element exists in both arrays, the element from the left-hand array will be used. For example:

$a = [1, 2, 3];
$b = [3, 4, 5];
$c = $a + $b; // $c contains [1, 2, 3, 4, 5]
  1. Equality Operator  == :

The equality operator compares two arrays to see if they have the same key/value pairs, regardless of their order. For example:

$a = [1, 2, 3];
$b = [2, 3, 1];
if ($a == $b) {
    echo "Arrays are equal";
} else {
    echo "Arrays are not equal";
}
// Output: Arrays are not equal

In this example, $a and $b contain the same elements, but in a different order. However, since the order of the elements is not considered, the arrays are considered unequal.

  1. Identity Operator  === :

The identity operator compares two arrays to see if they have the same key/value pairs in the same order. For example:

$a = [1, 2, 3];
$b = [2, 3, 1];
if ($a === $b) {
    echo "Arrays are equal";
} else {
    echo "Arrays are not equal";
}
// Output: Arrays are not equal

In this example, $a and $b contain the same elements, but in a different order. Since the order of the elements is considered, the arrays are considered unequal.

  1. Inequality Operator  != or <> :

The inequality operator compares two arrays to see if they do not have the same key/value pairs. For example:

$a = [1, 2, 3];
$b = [2, 3, 4];
if ($a != $b) {
    echo "Arrays are not equal";
} else {
    echo "Arrays are equal";
}
// Output: Arrays are not equal
  1. Non-Identity Operator  !== :

The non-identity operator compares two arrays to see if they do not have the same key/value pairs in the same order. For example:

$a = [1, 2, 3];
$b = [2, 3, 1];
if ($a !== $b) {
    echo "Arrays are not equal";
} else {
    echo "Arrays are equal";
}
// Output: Arrays are not equal
  1. Intersection Operator == :

The intersection operator returns an array containing only the elements that exist in both arrays. The returned array does not preserve the keys of the original arrays. For example:

$a = [1, 2, 3];
$b = [2, 3, 4];
$c = $a == $b; // $c contains [2, 3]

These are the six array operators in PHP and how they can be used to compare and combine arrays.

Ternary Operators in PHP

The conditional assignment operator in PHP is also known as the "ternary operator". It is a shorthand way of writing an if-else statement.

The syntax for the conditional assignment operator is as follows:

$variable = (condition) ? value_if_true : value_if_false;

In the above syntax, $variable is the variable to be assigned, condition is the condition to be evaluated, value_if_true is the value to be assigned to $variable if condition is true, and value_if_false is the value to be assigned to $variable if condition is false.

Here's an example to illustrate the use of the conditional assignment operator:

$score = 75;
$result = ($score >= 60) ? 'pass' : 'fail';
echo $result;

In this example, the condition being evaluated is whether $score is greater than or equal to 60. If this condition is true, the value 'pass' is assigned to $result. If the condition is false, the value 'fail' is assigned to $result. In this case, since $score is 75 and therefore greater than or equal to 60, the output of this code would be pass.

The conditional assignment operator can be particularly useful when assigning default values to variables. For example:

$name = $_POST['name'] ?? 'Guest';

In this example, $name is assigned the value of $_POST['name'] if it exists, otherwise the value 'Guest' is assigned to $name. This is a concise way of writing an if-else statement to handle cases where a variable may or may not be set.