PHP 8 Operators

An operator in a programming language is a symbol or phrase that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final output. It takes one or more arguments and produces a new value. By using operators, we can perform various operations on variables and constants.



Types of operators in PHP

There are three types of operators in PHP:

  • Unary Operators
  • Binary Operators
  • Ternary Operator


Unary Operators

Unary Operators take only one value. In mathematics, a unary operation is an operation with only one operand, i.e., a single value to produce a new value. Types of unary operators -

Operator Name Example
++ Increment $a++ // post increment $a
++$a // pre increment $a
-- Decrement $a-- // post decrement $a value
--$a // pre decrement $a value
! Logical Negation !$a // True if $a evaluates to False.

Examples of Unary operators
<?php
    $var = 10;
    echo $var++;
    echo '<br/>';
    echo ++$var;
    echo '<br/>';
    echo $var--;
    echo '<br/>';
    echo --$var;
    echo '<br/>';
    if(!isset($a)){
        echo 'variable $a is not set';
    }
?>




Binary Operators

A binary operator works on two operands and manipulates them to return an output. PHP has the following types of binary operators.

Arithmetic Operators

Arithmetic operators work on only two numeric operands to perform common arithmetical operations, such as addition, subtraction, multiplication etc. If the operands are not numeric, then it automatically converts to numeric.

Operator Name Example
+ Addition $a+$b
- Subtraction $a-$b
* Multiplication $a*$b
/ Division $a/$b
% Modulus $a%$b

Examples of arithmetic operators
<?php
    $a = 10; $b = 12;
    echo $a + $b.'<br/>';
    echo $a - $b.'<br/>';
    echo $a * $b.'<br/>';
    echo $a / $b.'<br/>';
    echo $a % $b.'<br/>';
?>




Assignment Operators

Assignment operators are used to assign a value to a variable, x = 10 is a simple assignment operator that assigns the value 10 on the right to the variable x on the left.

Operator Example Description
= $a = 2; It assigns the right operand value to the left operand.
+= $a += 2; // same as $a = $a+2; It adds both operands and assigns the result to the first operand.
-= $a -= 2; // same as $a = $a-2; It subtracts both operands and assigns the result to the first operand.
*= $a *= 2; // same as $a = $a*2; It multiply both operands and assigns the result to the first operand.
/= $a /= 2; // same as $a = $a/2; It divides left operand to the right and assigns the result to the first operand.
%= $a %= 2; // same as $a = $a%2; It takes modules and assigns the result to the first operand.

Examples of assignment operators
<?php
    $a = 10; 
    echo $a += 2;
    echo '<br/>';
    echo $a -= 2;
    echo '<br/>';
    echo $a *= 2;
    echo '<br/>';
    echo $a /= 2;
    echo '<br/>';
    echo $a %= 2;
?>

Logical Operators

These operators are used to perform logical operations. It returns the result in always a boolean value. It allows a program to make a decision based on multiple conditions.

Operator Name Example
&& AND $var && $var2; // Logical AND between $var and $var2
|| OR $var || $var4; // Logical OR between $var and $var4
^ XOR $var^$var1;
! NOT !($var && $var3);




Comparison Operators

Comparison operators are used for comparison. Comparison operators compare two values in an expression that resolves to a value of true or false. Its result is always a boolean value. In computer programming, it is generally used in conditional expressions to determine which block of code executes, thus controlling the program flow.

Operator Name Example
== Equal to $a == $b; // check the equality between both $a and $b.
=== Identical to $a === $b; // if value and datatype of both operands are equal then it returns true.
!= Not equal to $a != $b; //If the value of both operands are not equal, then it returns true, otherwise it returns false.
< Less than $a < $b; //If the value of left operand is less than the right operand than it returns true.
> Greater than $a > $b; //If the value of left operand is greater than the right operand than it returns true.
>= Greater than equal to $a >= $b; //If the value of left operand is greater than or equal to the right operand than it returns true.
<= Less than equal to $a <= $b; //If the value of left operand is less than or equal to the right operand than it returns true.

String Operators

String operators are performed on the string.

Operator Name Example
. Concatenation $a.$b; // It concatenates two strings.
.= Assign concatenation $a .= $b; // It concatenates $a and $b strings and assigns to the first string.

<?php
    $str1 = 'Hello John'; 
    $str2 = 'How are you?';
    echo $str1.$str2.'<br/>';; 
    echo $str1.=$str2;
?>

Array Operators

Array operators are used to compare arrays. Elements of arrays are equal for the comparison if they have the same key and value.

Operator Name Example
== Equal to $a == $b; // It returns true if both arrays have same key/value pair.
=== Identical to $a === $b; // It returns TRUE if both arrays have the same data types in the same order and same key/value pair.
!= Not equal to $a != $b; // It returns TRUE if array $a is not equal to array $b.
<> Inequality $a <> $b; // It returns TRUE if array $a is not equal to array $b.




Ternary Operator

Ternary Operator works on truth expression. It takes three operands - a condition, a result statement for true and a result statement for false. That's why it is called a ternary operator.

Syntax of Ternary Operator

(expression)? statement1 : statement2;

In the above syntax, if the expression is true, then statement1 is executed, otherwise statement2 is executed.

Example

<?php
    $day = 1;
    $message = ($day == 5)?'Today is Holiday' : 'Today is working day';
    echo $message;
?>

PHP 7 Null coalescing

In PHP 7, a new null coalescing operator (??) has been introduced. It returns the first value only if it exists and not set to null otherwise it returns the other value.

Syntax of Null coalescing

$x = statement1 ?? statement2;

In the above syntax, if the statement1 exists and is not null then it returns statement1 otherwise it returns statement2.

Example

$name = $_GET["name"] ?? "Priska";


Practice Exercises





Read more articles


General Knowledge



Learn Popular Language