codescracker


php

PHP Operators



« Previous Tutorial Next Tutorial »


Operators in PHP, are simply used to perform operations on variables and values in PHP script.

PHP 5 Operators

There are following types of operators available in PHP:

PHP Arithmetic Operators

PHP arithmetic operators are used to perform arithmetical operations, like addition and subtraction etc. Here the following table lists the arithmetic operators available in PHP:

Operator Works
+ This operators adds the two operands
- This operator subtracts the second operand from the first operand
/ This operator divides the numerator by denominator
* This operator multiplies both the operands
% This is a modulus operator, used to find the remainder of an integer division
++ This is an increment operator, increases the value of the integer by one
-- This ia a decrement operator, decreases the value of the integer by one

Example

Here is an example, uses arithmetic operators in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
	$num1 = 30;
	$num2 = 50;
    
	$res = $num1 + $num2;
	echo "Addition : $res <br/>";
	$res = $num1 - $num2;
	echo "Subtraction : $res <br/>";
	$res = $num1 * $num2;
	echo "Multiplication : $res <br/>";
	$res = $num1 / $num2;
	echo "Division : $res <br/>";
	$res = $num1 % $num2;
	echo "Modulus : $res <br/>";
	$res = $num1++; 
	echo "Increment : $res <br/>";
	$res = $num1--; 
	echo "Decrement : $res <br/>";
?>

</body>
</html>

Here is the output produced by the above PHP script:

PHP Arithmetic Operators

PHP Assignment Operators

The assignment operators in PHP, are used with numeric values to write the value to a variable. And the basic arithmetic operator in PHP (which is "="), is used to assign the value of the right to the left operand. Here, the following table lists the assignment operators available in PHP:

Operator Name Works
= Simple Assignment Operator This operator assigns the values from the right side operands to the left side operand
+= Add and Assignment Operator This operator is used to add the right operand to the left operand and then assign the result to the left operand
-= Subtract and Assignment Operator This operator subtracts the right operand from the left operand and then assign the result to the left operand
/= Divide and Assignment Operator This operator divides the left operand with the right operand and then assign the result to the left operand
*= Multiply and Assignment Operator This operator multiplies the right operand with the left operand and then assign the result to the left operand
%= Modulus and Assignment Operator This operator takes the modulus using both the operands and then assign the result to the left operand

Example

Here is an example, uses assignment operators available in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
	$num1 = 42;
	$num2 = 20;
   
	$res = $num1 + $num2; 
	echo "Addition Operation Result: $res <br/>";
	$res += $num1;
	echo "Add AND Assignment Operation Result: $res <br/>";
	$res -= $num1;
	echo "Subtract AND Assignment Operation Result: $res <br/>";
	$res *= $num1;
	echo "Multiply AND Assignment Operation Result: $res <br/>";
	$res /= $num1;
	echo "Division AND Assignment Operation Result: $res <br/>";
	$res %= $num1;
	echo "Modulus AND Assignment Operation Result: $res <br/>";
?>

</body>
</html>

It will display the following result in the browser:

PHP Assignment Operators

PHP Comparison Operators

PHP comparison operators are used to compare two values. Here the following table lists the comparison operators available in PHP:

Operator Name Operation Result When
== Equal $num1 == $num2 Returns true if $num1 is equal to $num2
=== Identical $num1 === $num2 Returns true If $num1 is equal to $num2, and they are of the same type
!= Not equal $num1 != $num2 Returns true If $num1 is not equal to $num2
!== Not identical $num1 !== $num2 Returns true If $num1 is not equal to $num2, or they aren't of the same type
< Less than $num1 < $num2 Returns true If $num1 is less than $num2
> Greater than $num1 > $num2 Returns true If $num1 is greater than $num2
<> Not equal $num1 <> $num2 Returns true If $num1 isn't equal to $num2
<= Less than or equal to $num1 <= $num2 Returns true If $num1 is less than or equal to $num2
<= Greater than or equal to $num1 <= $num2 Returns true If $num1 is greater than or equal to $num2

Example

Here is an example, uses comparison operators available in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
	$num1 = 42;
	$num2 = 20;
	
	if($num1 == $num2)
	{
		echo "Check1 : num1 is equal to num2<br/>";
	}
	else
	{
		echo "Check1 : num1 is not equal to num2<br/>";
	}
	if($num1 > $num2)
	{
		echo "Check2 : num1 is greater than  num2<br/>";
	}
	else
	{
		echo "Check2 : num1 is not greater than num2<br/>";
	}
	if($num1 < $num2)
	{
		echo "Check3 : num1 is less than  num2<br/>";
	}
	else
	{
		echo "Check3 : num1 is not less than num2<br/>";
	}
	if($num1 != $num2)
	{
		echo "Check4 : num1 is not equal to num2<br/>";
	}
	else
	{
		echo "Check4 : num1 is equal to num2<br/>";
	}
	if($num1 >= $num2)
	{
		echo "Check5 : num1 is either grater than or equal to num2<br/>";
	}
	else
	{
		echo "Check5 : num1 is neither greater than nor equal to num2<br/>";
	}
	if($num1 <= $num2)
	{
		echo "Check6 : num1 is either less than or equal to num2<br/>";
	}
	else
	{
		echo "Check6 : num1 is neither less than nor equal to num2<br/>";
	}
?>

</body>
</html>

Here is the output produced by this PHP script:

PHP Comparison Operators

PHP Increment/Decrement Operators

PHP increment/decrement operators are used to increment/decrement value of the variable. Here, the following table lists the increment/decrement operators available in PHP:

Operator Name Meaning
$num1++ Post-increment Returns $num1, then increments $num1 by one
++$num1 Pre-increment Increments $num1 by one, then returns $num1
$num1-- Post-decrement Returns $num1, then decrements $num1 by one
--$num1 Pre-decrement Decrements $num1 by one, then returns $num1

Example

Here is an example, uses increment/decrement operators available in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
	$num1 = 0;
	$num2 = 0;
	
	echo "$num1"."<br/>";
	
	$num2 = ++$num1;
	echo "$num2"."<br/>";
	
	$num2 = $num1++;
	echo "$num2"."<br/>";
	
	$num2 = --$num1;  
	echo "$num2"."<br/>";
	
	$num2 = $num1--;
	echo "$num2"."<br/>";
?>

</body>
</html>

The above PHP script produce the following output:

PHP Increment/Decrement Operators

PHP Logical Operators

PHP logical operators are used to combile conditional statements. Here, the following table lists the logical operators available in PHP:

Operator Name Operation Result When
or Or $num1 or $num2 True If either $num1 or $num2 is true
and And $num1 and $num2 True If both $num1 and $num2 are true
xor Xor $num1 xor $num2 True If either $num1 or $num2 is true, but not both
|| Or $num1 || $num2 True If either $num1 or $num2 is true
&& And $num1 && $num2 True If both $num1 and $num2 are true
! Not !$num1 True If $num1 is not true

Example

Here is an example, uses logical operators available in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
    $num1 = 42;
    $num2 = 0;
    
	if($num1 && $num2)
	{
		echo "Check1 : Both num1 and num2 are true<br/>";
	}
	else
	{
		echo "Check1 : Either num1 or num2 is false<br/>";
	}
	if($num1 and $num2)
	{
		echo "Check2 : Both num1 and num2 are true<br/>";
	}
	else
	{
		echo "Check2 : Either num1 or num2 is false<br/>";
	}
	if($num1 || $num2)
	{
		echo "Check3 : Either num1 or num2 is true<br/>";
	}
	else
	{
		echo "Check3 : Both num1 and num2 are false<br/>";
	}
	if($num1 or $num2)
	{
		echo "Check4 : Either num1 or num2 is true<br/>";
	}
	else
	{
		echo "Check4 : Both num1 and num2 are false<br/>";
	}
	
	$num1 = 10;
	$num2 = 20;
	
	if($num1)
	{
		echo "Check5 : num1 is true <br/>";
	}
	else
	{
		echo "Check5 : num1  is false<br/>";
	}
	if($num2)
	{
		echo "Check6 : num2 is true <br/>";
	}
	else
	{
		echo "Check6 : num2  is false<br/>";
	}
	if(!$num1)
	{
		echo "Check7 : num1 is true <br/>";
	}
	else
	{
		echo "Check7 : num1  is false<br/>";
	}
	if(!$num2)
	{
		echo "Check8 : num2 is true <br/>";
	}
	else
	{
		echo "Check8 : num2  is false<br/>";
	}
?>

</body>
</html>

Here is the output produced by the above PHP script:

PHP Logical Operators

PHP Conditional Operator

PHP conditional operator is an important operator available in any programming language. The conditional operator is just evaluates like if-else operator. Here is the general form of the conditional operator in PHP:

var = expression ? value1 : value2;

Here, expression is any valid expression in PHP. If the expression evaluates to true, then value1 is assigned to the variable (var), otherwise, value2 is assigned to the variable (var). Here is an example, uses conditional operator in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
	$num1 = 10;
	$num2 = 20;
   
	$result = ($num1 > $num2 ) ? $num1 : $num2;
	echo "Check1 : Value of result is $result<br/>";
	
	$result = ($num1 < $num2 ) ? $num1 : $num2;
	echo "Check2 : Value of result is $result<br/>";
?>

</body>
</html>

It will display the following result:

PHP Conditional Operator

PHP Operators Precedence

Higher
! ++ --
+ -
* / %
== !=
< <= > <=
||
&&
= += -= *= /= %=
?:
Lower

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test