PHP 7 Operators

There are three types of operators in PHP: Unary Operators, Binary Operators and Ternary Operator

Unary Operators

Unary Operators take only one value.

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.

<?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

Binary operators work on two operands. PHP has the following types of binary operators.

Arithmetic Operators

Arithmetic operators work on only two numeric operands, if the operands are not numeric then it automatically convert to numeric.

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

<?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.

Operator Example Description
= $a = 2; It assigns the right operand value to the left operand.
+= $a += 2; // same as $a = $a+2; It add both operands and assigns the result to the first operand.
-= $a -= 2; // same as $a = $a-2; It subtract 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.

<?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

This operators are used to perform logical operations. It returns the result in always a boolean value.

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. Its result is always a boolean value.

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.

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.

Syntax of Ternary Operator

(expression)? statement1 : statement2;

In the above syntax, if the expresion 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

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