Wednesday, January 7, 2015

What are PHP Operators ?

Operations are performed in PHP using operators. These operators are used on variables and values.
There are seven operators in PHP:
·         Arithmetic Operators.
·         Assignment Operators.
·         Comparison Operators.
·         Increment/Decrement Operators.
·         Logical Operators.
·         String Operators.
·         Array Operators.


Details on operators:

1.       Arithmetic Operators: They are used to perform common arithmetical operations such as addition, subtractions etc. with numeric values.
Operator
Name
Example
Result
+
Addition
$x+$y
Sum of $x and $y
-
Subtraction
$x-$y
Difference of $x and $y
*
Multiplication
$x*$y
Product of $x and $y
/
Division
$x/$y
Quotient of $x and $y
%
Modulus
$x%$y
Remainder of $x divided by $y
**
Exponentiation
$x**$y
Result of raising $x to the $y’th power

Example:
PHP Script (arithmetic.php)
 <?php  
 //Example of arithmetic operator  
 $x = 20;  
 $y = 30;  
 echo $x + $y."<br/>";  
 echo $x-$y."<br/>";  
 echo $x*$y."<br/>";  
 echo $x/$y."<br/>";  
 echo $x%$y."<br/>";  
 echo $x**$y."<br/>";  
 ?>  


2.       Assignment Operators: They are used with numeric values to write a value to a variable.
Operator
Name
Description
x=y
x=y
The left operands gets set to the value of the expression on the right
x+=y
x=x+y
Addition
x-=y
x=x-y
Subtraction
x*=y
x=x*y
Multiplication
x/=y
x=x/y
Division
x%=y
x=x%y
Modulus

Example:
PHP Script (assignment.php)
 <?php  
 //Example of assignment operator  
 $x = 20;  
 $y = 30;  
 $x=$y;  
 echo $x."<br/>";  
 $x+=$y;  
 echo $x."<br/>";  
 echo $x-=$y;  
 echo"<br/>";  
 echo $x*=$y;  
 echo"<br/>";  
 echo $x/=$y;  
 echo"<br/>";  
 echo $x%=$y;  
 ?>  

3.       Comparison Operators: These operators are used to compare two values.
Operator
Name
Example
Result
==
Equal
$x==$y
Returns true if $x is equal to $y
===
Identical
$x===$y
Returns true if $x is equal to $y, and they are of the same type
!=
Not equal
$x!=$y
Returns true if $x is not equal to $y
<> 
Not Equal
$x<>$y
Returns true if $x is not equal to $y
!==
Not identical
$x!==$y
Returns true if $x is not equal to $y, or they are not of the same type
> 
Greater than
$x>$y
Returns true if $x is greater than $y
< 
Less than
$x<$y
Returns true if $x is smaller than $y
>=
Greater than or equals to
$x>=$y
Returns true if $x is greater than or equals to $y
<=
Less then or equals to
$x<=$y
Returns true if $x is less than or equals to $y

Example:

PHP Script (comparison.php)

<?php  
 //Example of comparison operator  
 $x = 20;  
 $y = 30;  
 var_dump($x == $y);echo "<br/>";//returns false because values are not equal  
 var_dump($x === $y);echo "<br/>";/*returns false because types are equal but value are not equal*/  
 var_dump($x != $y);echo "<br/>"; // returns true because values are not equal  
 var_dump($x <> $y);echo "<br/>"; // returns true because values are not equal  
 var_dump($x !== $y);echo "<br/>"; /*returns true because types are equal and values are not equal*/  
 var_dump($x > $y);echo "<br/>";// returns false because $x is not less than $y  
 var_dump($x < $y);echo "<br/>";// returns true because $x is less than $y  
 var_dump($x >= $y);echo "<br/>";// returns false because $x is less than $y  
 var_dump($x <= $y); // returns true because $x is less than $y  
 ?>  

4.       Increment/ Decrement Operators: They are used to either for increment or decrement a variable value.
Operator
Name
Description
++$X
Pre-increment
Increments $x by one, then returns $x
$X++
Pose-increment
Returns $x, then increments $x by one
--$X
Pre-decrement
Decrements $x by one, then returns $x
$X--
Pose-decrement
Returns $x, then decrements $x by one

Example:
PHP Script (incre-decre.php)
 <?php  
 $x = 20;  
 echo ++$x."<br/>";  
 echo $x++."<br/>";  
 echo --$x."<br/>";  
 echo $x--;  
 ?>  

5.       Logical Operators:Logical operators are used to combine conditional statements.
Operator
Name
Example
Result
and
And
$x and $y
True if both $x and $y are true
or
Or
$x or $y
True if either $x or $y is true
xor
Xor
$x xor $y
True if either $x or $y is true, but not both
&&
And
$x&&$y
True if both $x and $y are true
||
Or
$x || $y
True if either $x or $y is true
!
Not
!$x
True if $x is not true

Example:

PHP Script (logical.php)

 <?php  
 $x = 20;  
 $y = 30;  
 if ($y == $x and $y==30) {  
   echo "This Tutorial";  
 }  
 echo "<br/>";  
 //example of or operator  
 if ($y == $x or $y==30) {  
   echo "Workshop Tutorial";  
 }  
 echo "<br/>";  
 //example of xor operator  
 if ($x==20 xor $x==$y ) {  
   echo "Learn HTML";  
 }  
 echo "<br/>";  
 //example of && operator  
 if ($x==20 && $y==30 ) {  
   echo "Learn CSS";  
 }  
 echo "<br/>";  
 //example of || operator  
 if ($x==20 || $x==$y ) {  
   echo "Learn JavaScript";  
 }  
 echo "<br/>";  
 //example of ! operator  
 if ($x!=$y) {  
   echo "Learn PHP";  
 }  
 ?> 

6.       String Operator: They are specially designed for strings.
Operator
Name
Example
Result
.
Concatenation
$ft1.$ft2
Concatenation of $txt1 and $txt2
.=
Concatenation assignment
$ft1 .= $ft2
Appends $txt2 to $txt1


Example:

PHP Script (logical.php)

 <?php  
 $x = "Nirajan";  
 $y = "Workshop";  
 //Example of . operator  
 echo $x.$y."<br/>";  
 //Example of .= operator  
 echo $x.=$y;  
 ?>  


7.       Array Operator: They are used to compare array in PHP.
Operators
Name
Example
Result
+
Union
$x + $y
Union of $x and $y
==
Equality
$x == $y
Returns true if $x and $y have the same key/value pairs
===
Identity
$x === $y
Returns true if $x and $y have the same key/value pairs in the same order and of the same types

!=
Inequality
$x != $y
Returns true if $x is not equal to $y
<> 
Inequality
$x <> $y
Returns true if $x is not equal to $y
!==
Non-identity
$x !== $y


Example:

PHP Script (array.php)

 <?php  
 $x = array("H" => "HTML", "C" => "CSS");  
 $y = array("P" => "PHP");  
 //Example of array operator +  
 print_r($x + $y);echo"<br/>";  
 //Example of array operator ==  
 var_dump($x == $y);echo"<br/>";  
 //Example of array operator ===  
 var_dump($x === $y);echo"<br/>";  
 //Example of array operator !=  
 var_dump($x != $y);echo"<br/>";  
 //Example of array operator <>  
 var_dump($x <> $y);echo"<br/>";  
 //Example of array operator !==  
 var_dump($x !== $y);  
 ?>