Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Operators

Operators in PHP Language can be classified as a character(s) used mostly between two operands. For example

3 + 2 = 5

In the above example 3 and 2 are operands and + and = sign are operators. But there are more types of operators in PHP language which are classified as:

Topics to be covered

We will cover the following topics in this lesson

  • Logical Operators
  • Arithmetic Operators
  • Conditional Operators
  • Comparison Operators
  • Assignment Operators
  • Summary

Logical Operators

Function of Logical operators is to convert their operands to boolean (i.e. check if it's a logical 1 or 0) then it performs the respective comparison

The following table shows

S/NFirst ConditionSecond ConditionOperatorResult
01TRUETRUEAND (&&)TRUE
02TRUEFALSEAND (&&)FALSE
03FALSETRUEAND (&&)FALSE
04FALSEFALSEAND (&&)FALSE
05TRUETRUEOR (||)TRUE
06TRUEFALSEOR (||)TRUE
07FALSETRUEOR (||)TRUE
08FALSEFALSEOR (||)FALSE
09TRUE EQUAL (==)TRUE
10FALSE EQUAL (==)FALSE
11TRUE NOT (!=)FALSE
12FALSE NOT (!=)TRUE

Example

Let's suppose variable Model holds 2020 and Variable Version holds 4 then: -

<?php
$model = 2020;
$version = 4;

The following examples show various comparisons

Example 1: AND OPERATOR

(Model AND Version) is true

echo ($model AND $version);

We used AND operator to see if BOTH operands are true

Outputs the value 1

Example 2: OROPERATOR

(Model OR Version) is true

echo ($model OR $version);

We used OR operator to see if any of the operands are not null then the condition will be true

Outputs the value 1

Example 3: && OPERATOR

(Model && Version) is true

echo ($model OR $version);

We used && operator to see if both operands are not null only then the condition will be true

Outputs the value 1

Example 4: || OPERATOR

(Model || Version) is true

echo ($model || $version);

We used OR operator to see if any of the two operands are not null then the condition will be true

Outputs the value 1

Example 5: ! OPERATOR

!(Mode && Version) is false

echo !($model && $version);

We used NOT operator to reverse the logical states of its operand. If the condition is true then the NOT operator becomes false

Outputs the value 0

Try to change the value of $version to 0 then test the above code again to see what you will get.

Arithmetic Operators

As the name suggests, Arithmetic operators are used with numeric values in order to perform arithmetical operations such as adding, subtracting, multiplying, dividing, etc.

Examples

<?php
$model = 2020;
$version = 4;

Example 1: Addition (+) Operator

echo $model + $version;

By using + operator between Model and Version we will be adding them together: $model + $version = 2024

Example 2: Subtraction (-) Operator

echo $model - $version;

By using - operator between Model and Version we will be performing the operator of subtraction: $model - $version = 2016

Example 3: Multiplication (*) Operator

echo $model * $version;

By using * operator between Model and Version we will be multiplying both operands: $model * $version; = 8080

Example 4: Division (/) Operator

echo $model / $version;

By using / operator we will be dividing numerator by de-numerator, in our example: $model / $version = 505

Example 5: Modulus (%) Operator

echo $model % $version;

By using % (modulus) operator and remainder of after an integer has been divided we get: $model % $version = 0

Example 6: Increment (++) Operator

echo ++$model;

By using the ++ (increment) operator we will be increasing the integer value by one: ++$model outputs 2021

Example 7: Decrement (--) Operator

echo --$model;

By using the -- (decrement) operator we will be decreasing the integer value by one: --$model outputs 2019

Conditional Operators

Conditional Operator's job is the firstly evaluate an expression under the boolean rule to conclude it whether true or false and then execute one of the two statements given depending on the result after the evaluation.

For example: If condition of Model (or Version) is true ? then value is A: otherwise value is B

<?php
$model = 2020;
$version = 4;

if ($model || $version){
	echo 'A';
}else{
    echo 'B';
}  

Comparison Operators

Comparison operators work by taking simple values (either numeric or strings but NOT data types) as arguments and evaluate them whether they are TRUE or FALSE

Examples

<?php
$model = 2020;
$version = 4;

Example 1: Equal To (==) Operator

if ($model == $version){
    echo 'TRUE';
}else{
    echo 'FALSE';
}

If we use == operator it will check the value of two operands to see if they are equal or not, and if they are then the condition becomes true:($model <= $version) is not true

Example 2: Not Equal To (!=) Operator

if ($model != $version){
    echo 'TRUE';
}else{
    echo 'FALSE';
}

 

If we use != operator it will check the value of two operands to see if they are equal or not. If they are not equal then the condition becomes true: ($model <= $version) is true

Example 3: Greater Than (>) Operator

if ($model > $version){
    echo 'TRUE';
}else{
    echo 'FALSE';
}

if we use > operator it will check the value of the left operand to see if it is greater than the value of operand on the right side, if yes then the condition becomes true: ($model <= $version) is true

Example 4: Less Than (<) Operator

if ($model < $version){
    echo 'TRUE';
}else{
    echo 'FALSE';
}

If we use < operator it will check the value of the right operand to see if it is greater than the value of operand on the left side, if yes than the condition becomes true: ($model <= $version) is false

Example 5: Greater Than or Equal To (>=) Operator

if ($model >= $version){
    echo 'TRUE';
}else{
    echo 'FALSE';
}

If we use >= operator it will check the value of the left operand to see if either it is equal to the operand on right or even if it is greater, if either of it is true then the condition becomes true: ($model <= $version) is true

Example 6: Less Than Equal To (<=) Operator

if ($model <= $version){
    echo 'TRUE';
}else{
    echo 'FALSE';
}

if we use <= operator it will check the value of the right operand to see if either it is equal to the operand on the left or even if it is greater, if either of it is true then the condition becomes true: ($model <= $version) is false

Assignment Operators

Assignment operators are used with all sorts of numeric values to write a value to a variable. For this we will use = sign. The operand on the left of = sign gets assigned with the value of the expression written on the right.

Examples

<?php

$x = 1;
$y = 2;
$z = 3;

Example 1: Expression Assignment

$x = $y + $z;
echo $x;

If we type X = Y + Z then the value of Y + Z will be assigned in to X

Example 2: Increment Assignment

$y += $y;
echo $y;

If we type X += Y then it will mean that X = X + Y

Example 3: Decrement Assignment

$y -= $y;
echo $y;

If we type X -= Y then it will mean X = X - Y

Example 4: Multiplication Assignment

$y *= $y;
echo $y;

If we type X *= Y then it will mean X = X * Y

Example 5: Division Assignment

$y /= $y;
echo $y;

If we type X /= Y then it will mean X = X / Y

Example 6: Modulus Assignment

$y %= $y;
echo $y;

If we type X %= Y then it will mean X = X % A

Summary

Operators in PHP allow us to do various tasks such as write arithemetic expressions like 1 + 1, assign values to variables like $value_added_tax = $cost * $vat_rate, compare values like $x == $y etc. Operators in PHP can be categorized as arithmetic, comparison, logical, and assignment.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP 7 Scalar Type Declarations

PHP 7 New Operators

Leave A Reply