PHP MySQL tutorial

PHP Operators

Operators operate on variables and produce calculated results such as additions or multiplications or comparisons. In PHP, following are operators are available.

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators

Let’s discuss each operator one by one.

Arithmetic operators

Operator Description
+ Adds two operands
Subtracts right operand from the left operand
* Multiplies two operands
/ Divides left operand by right operand
% Modulus operation (remainder of left operand divided by right operand)
++ Increment operand by one integer value
Decrement operand by one integer value

<?php
	$KH_Operand1 = 55;
	$KH_Operand2 = 15;
	 
	$KH_Result = $KH_Operand1 + $KH_Operand2;
	echo "Operand1 + Operand2 = $KH_Result <br/>";
	 
	$KH_Result = $KH_Operand1 - $KH_Operand2;
	echo "Operand1 - Operand2 = $KH_Result <br/>";
	 
	$KH_Result = $KH_Operand1 * $KH_Operand2;
	echo "Operand1 * Operand2 = $KH_Result <br/>";
	 
	$KH_Result = $KH_Operand1 / $KH_Operand2;
	echo "Operand1 / Operand2 = $KH_Result <br/>";
	 
	$KH_Result = $KH_Operand1 % $KH_Operand2;
	echo "Operand1 % Operand2 = $KH_Result <br/>";
	 
	$KH_Result = ++$KH_Operand1; 
	echo "++ Opearrtor1 = $KH_Result <br/>";
	 
	$KH_Result = --$KH_Operand2; 
	echo "-- Operand2 = $KH_Result <br/>";
?>

Download the code Run the code

Comparison Operators

Operator Description
== Condition is true only when value of two operands are same
!= Condition is true only when value of both operands is different
< Condition is true only when value of first operand is less than second operand
> Condition is true only when value of first operand is greater than second operand
<= Condition is true only when value of first operand is less than or equal to second operand
>= Condition is true only when value of first operand is greater than or equal to second operand

<?php
	$KH_Operand1 = 55;
	$KH_Operand2 = 15;

	if ($KH_Operand1 == $KH_Operand2) 
	  echo "Operand1 is equal to Operand2 <br/>";
	else
	  echo "Operand1 is not equal to Operand2 <br/>";

	if ($KH_Operand1 < $KH_Operand2) 
	  echo "Operand1 is less than Operand2 <br/>";
	else
	  echo "Operand1 is not less than Operand2 <br/>";

	if ($KH_Operand1 > $KH_Operand2) 
	  echo "Operand1 is greater than Operand2 <br/>";
	else
	  echo "Operand1 is not greater than Operand2 <br/>";

	if ($KH_Operand1 <= $KH_Operand2) 
	  echo "Operand1 is less or equal to Operand2 <br/>";
	else
	  echo "Operand1 is not less or equal to Operand2 <br/>";

	if ($KH_Operand1 >= $KH_Operand2) 
	  echo "Operand1 is greater or equal to Operand2 <br/>";
	else
	  echo "Operand1 is not greater or equal to Operand2 <br/>";

	if ($KH_Operand1 != $KH_Operand2) 
	  echo "Operand1 is not equal to Operand2 <br/>";
	else
	  echo "Operand1 is equal to Operand2 <br/>";
?>

Download the code Run the code

Logical Operators

Operator Description
and Condition is true only if the value of both operands is true
or Condition is true only if the value of either operands is true
&& Condition is true only if the value of both operands is true
|| Condition is true only if the value of either operands is true
! Condition is true only if the value of operand is false

<?php

	$KH_Operand1 = 55;
	$KH_Operand2 = 0;
	 
	if ($KH_Operand1 == 55 && $KH_Operand2 == 15) 
	  echo "Both operands are true <br/>";
	else
	  echo "Either of operands is false <br/>";

	$KH_Operand2 = 15;

	if ($KH_Operand1 and $KH_Operand2)
	  echo "Both operands are true <br/>";
	else
	  echo "Either of operands is false <br/>";
	 
	$KH_Operand1 = 55;
	$KH_Operand2 = 0;

	if ($KH_Operand1 || $KH_Operand2)
	  echo "Either of operands is true <br/>";
	else
	  echo "Both operands are false <br/>";

	$KH_Operand1 = 0;
	$KH_Operand2 = 0;

	if ($KH_Operand1 or $KH_Operand2)
	  echo "Either of operands is true <br/>";
	else
	  echo "Both operands are false <br/>";

	$KH_Operand1 = 55;
					  
	if (!$KH_Operand1)
	  echo "Operand is false <br/>";
	else
	  echo "Operand is true <br/>";
	 
	if (!$KH_Operand2)
	  echo "Operand2 is false <br/>";
	else
	  echo "Operand2 is true <br/>";
?>

Download the code Run the code

Assignment Operators

Operator Description
= Assign right side operand value to left side operand
+= First add the value of right operand to left side operand and then result is assigned to left operand
-= First subtract the value of right operand from left side operand and then result is assigned to left operand
*= First multiply the value of right operand with left side operand and then result is assigned to left operand
/= First divide the value of left operand with right side operator and then result is assigned to left operand
%= First take the modulus of two operands and then result is assigned to left operand

<?php

	$KH_Operand1 = 55;
	$KH_Operand2 = 15;

	$KH_Result = $KH_Operand1 + $KH_Operand2;   
	echo "Addtion Operation Result: $KH_Result <br/>"; // Output: Addtion Operation Result: 70 

	$KH_Result += $KH_Operand1;
	echo "Add AND Assigment Operation Result: $KH_Result <br/>"; // Output: Add AND Assigment Operation Result: 125 

	$KH_Result -= $KH_Operand1; 
	echo "Subtract AND Assignment Operation Result: $KH_Result <br/>"; // Output: Subtract AND Assignment Operation Result: 70 

	$KH_Result *= $KH_Operand1; 
	echo "Multiply AND Assignment Operation Result: $KH_Result <br/>"; // Output: Multiply AND Assignment Operation Result: 3850 

	$KH_Result /= $KH_Operand1; 
	echo "Division AND Assignment Operation Result: $KH_Result <br/>"; // Output: Division AND Assignment Operation Result: 70 

	$KH_Result %= $KH_Operand1; 
	echo "Modulus AND Assignment Operation Result: $KH_Result <br/>"; // Output: Modulus AND Assignment Operation Result: 15 
?>

Download the code Run the code

<<< PHP ConstantsPHP if elseif else statements >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .