How to PHP Scripting- PHP operators

PHP scripting language provides different kinds of operators to work with variables. PHP operators are used to performing operations on variables and values. Operators are divided into the following groups:

1. Arithmetic operators
2.Assignment operators
3.Comparison operators
4.Logical operators
5.String operators
6.Array operators

 

1.Arithmetic operators

OperatorDescription
+Adds two operands
-Subtracts second operand from the first
*Multiply both operands
/Divide numerator by de-numerator
%Modulus Operator and remainder of after an integer division
++Increment operator, increases integer value by one
--Decrement operator, decreases integer value by one

 

Arithmetic Operators
	<?php
	  $addition = 2 + 4; 
	  $subtraction = 6 - 2; 
	  $multiplication = 5 * 3; 
	  $division = 15 / 3; 
	  $modulus = 5 % 2; 
	  echo "Addition: 2 + 4 = ".$addition."<br />"; 
	  echo "Subtraction: 6 - 2 = ".$subtraction."<br />"; 
	  echo "Multiplication:  5 * 3 = ".$multiplication."<br />"; 
	  echo "Division: 15 / 3 = ".$division."<br />"; 
	  echo "Modulus: 5 % 2 = " . $modulus 
	?>

 

Output
	  Addition: 2 + 4 = 6 
	  Subtraction: 6 - 2 = 4 
	  Multiplication: 5 * 3 = 15 
	  Division: 15 / 3 = 5 
	  Modulus: 5 % 2 = 1

 

2.Assignment operators

OperatorDescription
=Simple assignment operator, Assigns values from right side operands to left side operand
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

 

Assignment Operators
	<?php
	 $a=12;
	 $b=$a;
	 echo $b; //prints 12
	 echo "<br>";
	 $a+=$b;
	 echo $a; //prints 24, same as a=a+b
	 echo "<br>";
	 $a-=$b;
	 echo $a; //prints 12, same as a=a-b=24-12
	 echo "<br>";
	 $a*=$b;
	 echo $a; //prints 144, same as a=a*b=12*12
	 echo "<br>";
	 $a/=$b;
	 echo $a; //prints 12, same as a=a/b=144/12
	 echo "<br>";
	 $a%=$b;
	 echo $a; //prints 0, same as a=a%b=12%12
	 echo "<br>";
	 $a.=$b;
	 echo $a; //prints 012, same as a=a.b=0.12=012
	?>

 

3.Comparison operators

OperatorDescription
$a == $bTRUE if $a is equal to $b.
$a === $bTRUE if $a is equal to $b, and they are of the same type.
$a != $bTRUE if $a is not equal to $b after type juggling.
$a <> $bTRUE if $a is not equal to $b after type juggling.
$a !== $bTRUE if $a is not equal to $b, or they are not of the same type.
$a < $bTRUE if $a is strictly less than $b.
$a > $bTRUE if $a is strictly greater than $b
$a <= $bTRUE if $a is less than or equal to $b.
$a >= $bTRUE if $a is greater than or equal to $b.
$a <=> $bAn integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.

 

Comparison Operators
	<?php
	  $a=10;
	  $b="10";
	 
	  //Equal example
	  var_dump($a==$b); //prints bool(true) 
	  echo "<br>";
	  //Identical example
	  var_dump($a===$b); //prints bool(false) 
	  echo "<br>";
	  //Not equal example
	  var_dump($a!=$b); //prints bool(false) 
	  echo "<br>";
	  var_dump($a<>$b); //prints bool(false) 
	  echo "<br>";
	  //Not identical example
	  var_dump($a!==$b); //prints bool(true) 
	  echo "<br>";
	  //Less than example
	  var_dump($a<$b); //prints bool(false) 
	  echo "<br>";
	  //Greater than example
	  var_dump($a>$b); //prints bool(false) 
	  echo "<br>";
	  //Less than or equal to example
	  var_dump($a<=$b); //prints bool(true) 
	  echo "<br>";
	  //Greater than or equal to example
	  var_dump($a>=$b); //prints bool(true) 
	  echo "<br>"; 
	?>

 

4.Logical operators

OperatorDescription
$a and $bTRUE if both $a and $b are TRUE.
$a or $bTRUE if either $a or $b is TRUE.
$a xor $bTRUE if either $a or $b is TRUE, but not both.
! $aTRUE if $a is not TRUE.
$a && $bTRUE if both $a and $b are TRUE.
$a || $bTRUE if either $a or $b is TRUE.

 

5.String operators

OperatorDeswcription
$txt1 . $txt2Concatenation of $txt1 and $txt2
$txt1 .= $txt2Appends $txt2 to $txt1 Show it ยป
String Operators
     <?php
        $a_string = "Hello";
        $another_string = " Billy";
        $new_string = $a_string . $another_string;
        echo $new_string . "!";
     ?>

 

6.Array operators

OperatorDescription
$a + $bUnion of $a and $b.
$a == $bTRUE if $a and $b have the same key/value pairs.
$a === $bTRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $bTRUE if $a is not equal to $b.
$a <> $bTRUE if $a is not equal to $b.
$a !== $bTRUE if $a is not identical to $b.

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...