PHP Operators Tutorial

– PHP operators are one type of symbol which used to perform some operation or task on the variable or value.

– The value or variable are known as the operands.

– For example, 4 + 6 = 10. Here, sign “+” is the operator and “4” as well as “6” are the operands.

– There are different types of operators provides into PHP. See below :

  1. Arithmetic Operator
  2. Logical Operator
  3. String Operator
  4. Assignment Operator
  5. Conditional Operator
  6. Increment/Decrement Operator
  7. Comparison Operator
  8. Array Operator

Now let us learn all the above kinds of operators in detail.

ARITHMETIC OPERATOR :

– Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

– Below is the list of arithmetic operators and their along with its description.

php operators

– Here, $a and $b are two different values.

– Below source code performs different arithmetic operations on them using the operators.

<!DOCTYPE html>
<html>
<body>

<?php
$a = 20;  
$b = 5;

// addition
$ans = $a + $b;
echo "Addition : " . $ans;
echo "</br>";

// subtraction
$ans= $a - $b;
echo "Subtraction : " . $ans;
echo "</br>";

//multiplication
$ans= $a * $b;
echo "Multiplication : " . $ans;
echo "</br>";

//division
$ans= $a / $b;
echo "Division : " . $ans;
echo "</br>";

//modulus
$ans= $a % $b;
echo "Modulus : " . $ans;
echo "</br>";

//exponentiation
$x = 10;
$y = 2;
$ans= $x ** $y;
echo "Exponentiation : " . $ans;
echo "</br>";
?>  

</body>
</html>

Below is the output of all the arithmetic operators :

Addition : 25
Subtraction : 15
Multiplication : 100
Division : 4
Modulus : 0
Exponentiation : 100

LOGICAL OPERATOR :

– Logical operators are mainly used to perform bit-level operations on operands.

– Logical operators are used to perform conditional statements and expression.

– Conditional statements are based on different conditions.

– Logical operators are also called as Relational operators.

– The condition can be match or can not be match so the result of conditional statement is either true or false.

– Below is the list of arithmetic operators and their along with its description.

php operators

– Here we perform different logical operators using PHP code.

<!DOCTYPE html>
<html>
<body>

<?php
// using AND operator
$a = true;  
$b = true;

if ($a and $b) {
    echo "Hello Demonuts!";
    echo "</br>";
    echo "</br>";
}

// using OR operator
$x = true;  
$y = 1;

if ($x == true or $y == 1) {
    echo "Hello Demonuts!";
    echo "</br>";
    echo "</br>";
}

// using XOR operator
$c = true;  
$d = 50;

if ($c == 100 xor $d == 20) {
    echo "Hello world!";
    echo "</br>";
    echo "</br>";
}

// using && operator
$m = true;  
$n = true;

if ($m && $n) {
    echo "Welcome to Demonuts!";
    echo "</br>";
    echo "</br>";
}

// using || operator
$A = true;  
$B = 1;

if ($A == true or $B == 1) {
    echo "PHP Operator Tutorial";
    echo "</br>";
    echo "</br>";
}

// using ! operator
$i = true;  

if ($i !== 10) {
    echo "We are here to help you!";
}

?>  

</body>
</html>

– Below is the output of all logical operators :

 Hello Demonuts!

Hello Demonuts!

Hello world!

Welcome to Demonuts!

PHP Operator Tutorial

We are here to help you!

STRING OPERATOR :

– PHP string operators are mainly used to perform operation on the strings.

– There are two types of string operator which are listed below :

php operators

– Here we have some string operator examples :

<!DOCTYPE html>
<html>
<body>

<?php

// Concatenation operator
$txt1 = "Hello";
$txt2 = " Demonuts!";
echo $txt1 . $txt2;
echo "</br></br></br>";

// Concatenation assignment
$text1 = "Welcome to";
$text2 = " Demonuts!";
$text1 .= $text2;
echo $text1;

?>  

</body>
</html>

– All the string operators are gives the output as below :

Hello Demonuts!


Welcome to Demonuts!

ASSIGNMENT OPERATORS :

– Assignment operators are used to assign different values to different variables in PHP.

– PHP assignment operators are used with numeric value to assign values to variables.

– The basic assignment operator in PHP is equal “=”.

It means that the left side operand get the value of assigned right side operand.

– Here we have assignment operator table along with it description.

php operators

– All the assignment operator are working as below program.

<!DOCTYPE html>
<html>
<body>

<?php

// a=b assignment operator
$a = 90;  
echo $a;
echo "</br></br>";

// a+=b assignment operator
$x = 10;  
$x += 300;
echo $x;
echo "</br></br>";

// a-=b assignment operator
$y = 100;
$y -= 40;
echo $y;
echo "</br></br>";

// a*=b assignment operator
$i = 15;  
$i *= 3;
echo $i;
echo "</br></br>";

// a/=b assignment operator
$j = 27;
$j /= 3;
echo $j;
echo "</br></br>";

// a%=b assignment operator
$m = 105;
$m %= 4;
echo $m;
echo "</br></br>";
?>  

</body>
</html>

– output of all assignment operators.

 90

310

60

45

9

1

CONDITIONAL OPERATOR

– Conditional operators are used to evaluate value depends on given condition in PHP.

– This conditional operator first check a statement for true or false value and then run one of the two statement base on the result of the evaluation.

– Conditional operators are also called as the Ternary operator.

– There is two type of conditional statement.

conditional

– Now we perform conditional operators.

<!DOCTYPE html>
<html>
<body>

<?php

// ? ternary operator

   // if empty($user) = TRUE, set $status = "demonuts"
   echo $status = (empty($user)) ? "demonuts" : "logged in";
   echo("<br>");

   $user = "clerk";
   // if empty($user) = FALSE, set $status = "logged in"
   echo $status = (empty($user)) ? "demonuts" : "logged in";
   echo "</br></br>";
   
// Null coalescing operator

	// variable $user is the value of $_GET['user']
   // and 'demonuts' if it does not exist
   echo $user = $_GET["user"] ?? "demonuts";
   echo("<br>");
  
   // variable $color is "red" if $color does not exist or is null
   echo $color = $color ?? "red";
?>  

</body>
</html>

– Output of all conditional operators.

demonuts
logged in

demonuts
red 

INCREMENT / DECREMENT OPERATOR

– Increment or decrement operators are use for increase or decrease the variable value.

– This operators are work on single operand.

– They are also called as unary operators.

– Here we have the list of all increment/decrement operators.

increment-decrement

– Here we perform increment/decrement operators and know how it works.

<!DOCTYPE html>
<html>
<body>

<?php
  // ++$a operator
  $a = 10;  
  echo ++$a;
  echo "</br>";

  // $a++ operator
  $a = 10;
  echo $a++; 
  echo "</br>";

  // --$a operator
  $a = 10;
  echo --$a;  
  echo "</br>";

  // $a-- operator
  $a = 10;
  echo $a--; 
  echo "</br>";

?>  

</body>
</html>

– Below is the output of all increment/decrement operators.

11
10
9
10

COMPARISON OPERATOR

– Comparison operators are used to compare two values.

– The values are in form of number or string.

– These operators are gives output in the boolean form.

– Here we have the list of comparison operators which are PHP provide.

php operators

– We perform comparison operator as below :

<!DOCTYPE html>
<html>
<body>

<?php
  $a = 160;  
  $b = "160";

  var_dump($a == $b); // returns true because values are equal
  echo "</br>";
  
  var_dump($a === $b); // Outputs: boolean false
  echo "</br>";
  
  var_dump($a != $b);  // Outputs: boolean true
  echo "</br>";
  
  var_dump($a <> $b); // returns false because values are equal
  echo "</br>";
  
  var_dump($a !== $b); // Outputs: boolean true
  echo "</br>";
  
  var_dump($a > $b);   // Outputs: boolean false
  echo "</br>";
  
  var_dump($a < $b);   // Outputs: boolean true
  echo "</br>";
  
  var_dump($a >= $b);  // Outputs: boolean false
  echo "</br>";
  
  var_dump($a <= $b);  // Outputs: boolean true
  echo "</br>";

  // <=> operator
  var_dump ($a <=> $b); // returns 0 because values are equal
  echo "<br>";

  $x = 10;
  $y = 20;
  var_dump ($x <=> $y); // returns -1 because $x is less than $y
  echo "<br>";

  $m = 15;  
  $n = 10;

  var_dump ($m <=> $n); // returns +1 because $x is greater than $y

?>  

</body>
</html>

– Here we have the output for all comparison operators.

bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
int(0)
int(-1)
int(1) 

ARRAY OPERATOR

– The array operator exist if there in array is used.

– This operator is used for compare array.

– Here we have the list of array operators.

Array

– Now we perform the operation of array operator in PHP using the following code snippet.

<!DOCTYPE html>
<html>
<body>

<?php
  $a = array("a" => "red", "b" => "green");  
  $b = array("c" => "blue", "d" => "yellow");  

  print_r($a + $b); // union of $x and $y
  echo "</br></br>";

  var_dump($a == $b); // equality
  echo "</br></br>";

  var_dump($a === $b); // identity
  echo "</br></br>";

  var_dump($a != $b); // inequality
  echo "</br></br>";

  var_dump($a <> $b); // inequality
  echo "</br></br>";

  var_dump($a !== $b); // non-identity
  echo "</br></br>";
?>  

</body>
</html>

– Output of all array operators as below :

Array ( [a] => red [b] => green [c] => blue [d] => yellow )

bool(false)

bool(false)

bool(true)

bool(true)

bool(true)