PHP Operators


Operators are symbols that are used to perform operations on operands. For example, lets look at a simple expression
 $num = 5 + 10;
At the above expression the + (addition) symbol is used to add two numbers, where Here 5 and 10 are called operands and ‘+’ is called the operator. PHP has both binary and unary operators, and one special ternary operator, the conditional operator. A unary operator requires a single operand, either before or after the operator :
 <operator> <operand>
Or
 <operand> <operator>
For example x++ or ++x.

A binary operator requires two operands, one before the operator and one after the operator:
 <operand1> <operator> <operand2>
For example 3 + 4, x * y.

PHP has the following type of Operator :

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Conditional Operators
  • String Operators
  • Array Operators
  • Increment/Decrement operator

1. Arithmetic Operators :

Arithmetic operators are used to perform arithmetic operations numbers. PHP supports the following arithmetic operators :

+ ( Addition ) :  Add two numbers.
 x + y
- ( Subtraction ) : Subtract two numbers.
 x - y
* ( Multiplication ) : Multiply two numbers.
 x * y
/ ( Division ) : Divide two numbers
 y / x
% ( Modulus ) : Outputs the remainder of an integer division
 y % x
For example :
<?php
  $x = 10;
  $y = 5;
  echo "x = " . $x . ", y = " . $y;
  echo "<br/>";
  echo "Addition : " . ($x + $y);
  echo "<br/>";
  echo "Subtraction : " . ($x - $y);
  echo "<br/>";
  echo "Multiplication : " . ($x * $y);
  echo "<br/>";
  echo "Division : " . ($x / $y);
  echo "<br/>";
  echo "Modulus : " . ($x % $y);
  echo "<br/>";
?>
Output :

x = 10, y = 5
Addition : 15
Subtraction : 5
Multiplication : 50
Division : 2
Modulus : 0


2. Comparison Operators :

A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values. PHP supports the following comparison operators :

= = (Equal) : Checks if the value of two operands are equal or not, if yes, then the condition becomes true and it return 1 otherwise, returns 0.
 x == y
!= (Not equal) : Checks if the value of two operands are equal or not, if the values are not equal, then the condition becomes true and it return 1 otherwise, returns 0.
 x != y
> (Greater than) : Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true and it return 1 otherwise, returns 0.
 x > y
< (Less than) : Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true and it return 1 otherwise, returns 0.
 x < y
>= (Greater than or Equal to) : Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true and it return 1 otherwise, returns 0.
 x >= y
<= (Less than or Equal to) : Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true and it return 1 otherwise, returns 0.
 x <= y
Example :
<?php
  $x = 5;
  $y = 10;
  echo "x = " . $x . ", y = " . $y;
  echo "<br/>"; 
  echo "Equal ( x == y ) : " . ($x == $y);
  echo "<br/>"; 
  echo "Not Equal ( x != y ) : " . ($x != $y);
  echo "<br/>";
  echo "Greater then ( x > y ) : " . ($x > $y);
  echo "<br/>"; 
  echo "Less then ( x < y ) : " . ($x < $y);
  echo "<br/>"; 
  echo "Greater then or Equal to ( x >= y ) : " . ($x >= $y);
  echo "<br/>"; 
  echo "Less then or Equal to ( x <= y ) : " . ($x <= $y);
  echo "<br/>"; 
?>
Output :

x = 5, y = 10
Equal ( x == y ) :
Not Equal ( x != y ) : 1
Greater then ( x > y ) :
Less then ( x < y ) : 1
Greater then or Equal to ( x >= y ) :
Less then or Equal to ( x <= y ) : 1


3. Logical Operators :

PHP logical operators return boolean result based on operands. Logical operators are used to determine the logic between variables or values. PHP supports the following logical operators :

AND : The and logical operator returns true if both the operands are true.
 true and true = true
OR  : The or logical operator returns true if both or one of the operand are true.
 false or true = true
&& (Logical AND) : If both the operands are non-zero, then the condition becomes true.
 x && y
|| (Logical OR) : If any of the two operands are non-zero, then the condition becomes true.
 x || y
! (Logical NOT) : Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.
 x ! y
Example :
<?php
  $a = true;
  $b = true;
  echo "\$a and \$b = " . ($a and $b);
  echo "<br/>";
  echo "\$a or \$b = " . ($a or $b);
  echo "<br/><br/>";

  $x = 0;
  $y = 5;
  echo "x = " . $x . ", y = " . $y;
  echo "<br/>";

  echo "( x && y ) : " . ($x && $y);
  echo "<br/>";

  echo "( x || y ) : " . ($x || $y);
  echo "<br/>";

  echo "!( x && y ) : " . !($x && $y);
?>
Output :

$a and $b = 1
$a or $b = 1

x = 0, y = 5
( x && y ) :
( x || y ) : 1
!( x && y ) : 1


4. Assignment Operators :

The assignment operator assign values to its left operand based on the value of its right operand. PHP supports the following assignment operators :

= (Simple Assignment ) : Assigns values from the right side operand to the left side operand.
 z = x + y
+= (Add and Assign) : It adds the right operand to the left operand and assigns the result to the left operand.
 x += y 
The above expression is equivalent to x = x + y.

*= (Multiply and Assign) : It multiplies the right operand with the left operand and assigns the result to the left operand.
 x *= y
The above expression is equivalent to x = x * y.

/= (Divide and Assign) : It divides the left operand with the right operand and assigns the result to the left operand.
 x /= y
The above expression is equivalent to x = x / y.

%= (Modules and Assign) : It takes modulus using two operands and assigns the result to the left operand.
 x %= y
The above expression is equivalent to x = x % y.


5. Conditional Operators :

The conditional operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.
 condition ? value1 : value2
If Condition is true ? Then value X : Otherwise value Y

If condition is true, the operator has the value of val1. Otherwise it has the value of val2. You can use the conditional operator anywhere you would use a standard operator. For example :
 $status = ( age >= 18 ) ? "Adult" : "minor";
Example :
<?php
  $age = 25;
  $status = ( $age >= 18 ) ? "Adult" : "minor";
  echo $status;
?>
Output :

Adult


6. String Operators :

PHP has two operators that are specially designed for strings operations. These are :

Concatenation ( . ) : Used to concatenate the two string variables. Example :
 echo $str1 . str2;
Concatenation Assignment ( .= ) : Used to appends one string variable in another. Example :
 $str1 .= $str2;
Example :
<?php
  $str1 = "This is String One.";
  $str2 = "This is String Two.";

  echo $str1 . $str2; // Concatenation operator
  echo "<br/>";
  $str1 .= $str2; // Concatenation Assignment operator
  echo $str1;
?>
Output :

This is String One.This is String Two.
This is String One.This is String Two.


7. Array Operators

The array operators are used to compare arrays. These operators are as follows :

Union ( + ) : Union of two variables.
 $x + $y
Equality ( == ) : Returns true if $x and $y have the same key/value pairs.
 $x == $y
Identity ( === ) : Return true if $x and $y have the same key/value pairs in the same order and of the same types.
 $x === $y
Inequality ( != ) : Return true if $x is not equal to $y.
 $x != $y
Inequality ( <> ) : Return true if $x is not equal to $y.
 $x <> $y
Non-identity ( !== ) : Return if $x is identical to $y.
 $x !== $y
Example :
<?php
  $arr1 = array("FE1"=>"HTML", "FE2"=>"CSS", "FE3"=>"JavaScript");
  $arr2 = array("BE1"=>"PHP", "BE2"=>"MySQL", "BE3"=>"JSP");
 
  $new_arr = $arr1 + $arr2;
  var_dump($new_arr);
  echo "<br/><br/>";
  var_dump($arr1 == $arr2);
  echo "<br/>";
  var_dump($arr1 === $arr2);
  echo "<br/>";
  var_dump($arr1 != $arr2);
  echo "<br/>";
  var_dump($arr1 <> $arr2);
  echo "<br/>";
  var_dump($arr1 !== $arr2);
?>
Output :

array(6) { ["FE1"]=> string(4) "HTML" ["FE2"]=> string(3) "CSS" ["FE3"]=> string(10) "JavaScript" ["BE1"]=> string(3) "PHP" ["BE2"]=> string(5) "MySQL" ["BE3"]=> string(3) "JSP" }

bool(false)
bool(false)
bool(true)
bool(true)
bool(true)


8. Increment/Decrement operator

The PHP increment operator is used to increment a variable's value and decrement operator is used to decrement a variable's value. These operators are as follows :

Pre-increment ( ++$x ) : The Pre-increment operator increments $x by one, then returns $x.
Post-increment ( $x++ ) : The Post-increment returns $x then increments $x by one.
Pre-decrement ( --$x ) : The Pre-decrement operator decrements $x by one, then returns $x.
Post-decrement ( $x-- ) : The Post-decrement returns $x then decrements $x by one.

Example :
<?php
  $x = 10;
  echo ++$x;
  echo "<br/>";
  echo $x;
  echo "<br/>";
  echo "<br/>";

  $x = 10;
  echo $x++;
  echo "<br/>";
  echo $x;
  echo "<br/>";
  echo "<br/>";

  $x = 10;
  echo --$x;
  echo "<br/>";
  echo $x;
  echo "<br/>";
  echo "<br/>";

  $x = 10;
  echo $x--;
  echo "<br/>";
  echo $x;
?>
Output :

11
11

10
11

9
9

10
9


Next Topic :

No comments:

Post a Comment