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);  
 ?> 



Is PHP Case Sensitive ?

The functions, keywords, classes, and user-defined functions are NOT case-sensitive at all in PHP.

Example:
PHP Script (case-sensitive.php)

 <?php  
 ECHO "JoE World!!!<br/>";  
 echo "JoE World!!!<br/>";  
 EcHo "JoE World!!!<br/>";  
 /* variable names are case-sensitive*/  
 $food= "mo-mo";  
 echo "My favorite food is " . $food. "<br/>";  
 echo "My favorite food is " . $FOOD. "<br/>"; /*$food and $FOOD are different in PHP*/  
 ?>

Working with PHP Cookies

Cookies are text files stored in client computer for tracking and verifying purposes. They are also used for identification purpose by web server. Cookies are stored by browsers in local machine for future use and references.They are usually set in HTTP header and  they always have an expiry date, which means the cookie gets expired after some time limit.


PHP Script (create.php)

 <?php  
 $cookie_name = "login";  
 $cookie_value = "admin";  
 setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta charset="UTF-8"/>  
 <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <?php  
 if(!isset($_COOKIE[$cookie_name])) {  
   echo "Cookie named '" . $cookie_name . "' is not set!";  
 } else {  
   echo "Cookie '" . $cookie_name . "' is set!!!!!!<br>";  
   echo "Value is: " . $_COOKIE[$cookie_name];  
 }  
 ?>  
 </body>  
 </html> 

Working with PHP Session (Create, Modify) etc.

Session helps in creating a file in a temporary directory on the server or inside the database as per its configuration and definition. This helps in authentication and validation of users and tokens, using session we can limit the access or broaden it. Before creating a session,PHP first creates a unique identifier for that particular session where a random string of 32 hexadecimal numbers, after that a cookie called PHPSESSID is created for that particular session in user’s computer to store unique session identification string. After that a file is created in server in a temporary directory or in database which contains the name of the unique identifier and details of users IP(Internet Protocol) and etc. according to logic used for creating session.

Syntax:
 Session_start();  


Example:

PHP Script (session.php)

 <?php  
 // Start the session  
 session_start();  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8"/>  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Setting Session</h3>  
 <?php  
 // Set session variables  
 $_SESSION["session_var1"] = "PHP";  
 $_SESSION["session_var2"] = "Hello World";  
 echo "Session variables are set";  
 ?>  
 </body>  
 </html> 


Example of Get PHP Session Value:

PHP Script (session_get.php)

 <?php  
 // Start the session  
 session_start();  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8"/>  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Get Session value</h3>  
 <?php  
 // Getting session variable value  
 echo "Variable1= ".$_SESSION["session_var1"];  
 echo "<br/>";  
 echo "Variable2= ".$_SESSION["session_var2"];  
 ?>  
 </body>  
 </html> 

Example of Modify PHP Session value:

Syntax:

 $_SESSION["session_variable"] = "New_value";  


Example:

PHP Script (session_modify.php)



 <?php  
 // Start the session  
 session_start();  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8"/>  
   <title>PHP</title>  
 </head>  
 <body>  
 <h3>Example of Modify Session</h3>  
 <?php  
 // Modify session  
 $_SESSION["session_var1"] = "CSS";  
 $_SESSION["session_var2"] = "This is modified";  
 echo "Session value is modified.";  
 ?>  
 </body>  
 </html>