3/23/15

PHP Operators


Variables are simply containers for information. In order to do anything useful with them, you need Operators .Operators are symbols that tell the PHP processor
to perform certain actions. For example, the addition(+) symbol is an Operators 
that tells PHP to add two variables or values, while the greater-than(>) symbol
is an Operators that tells PHP to compare two values.
PHP supports more than 50 such Operators , ranging from operators for arithmetical
operations to operators for logical comparison and bitwise calculations.
This section discusses the most commonly used Operators .

There are four type of operators generally used by us.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators

Arithmetic operators in PHP
PHP supports all standard arithmetic operations, as illustrated by the list of Operators .
Common Arithmetic Operators
OperatosDescription
+Add
-Subtract
*Multiply
/Divide and return quotient
%Divide and return modulus

Here’s example illustrating these operators in action

PHP Arithmetic operators are used to perform mathematical operation on more than one operands.
Some of the standard arithmetic operators are +,-,*,/,%.
The use of some common arithmetic operators is here illustrated by an example as follows:-
Eg i (arithmetic operators + for addition )
 <?php
  $x=10;
  $y=5;
  
  //addition
  $sum=$x+$y;
  echo "sum=".$sum."<br/>";
    ?> 
 Output : sum = 15 

Eg ii (arithmetic operators – for subtraction)
   <?php
  $x=10;
  $y=5;
                //subtraction
  $sub=$x-$y;
  echo "sub=".$sub."<br/>";
 ?> 
 Output : sub = 5 

Eg iii (arithmetic operators * for multiplication)
   <?php
                $x=10;
  $y=5;
                //Multiply
  $multiply=$x*$y;
  echo "Multiplication = ".$multiply."<br/>";
 ?> 
 Output : Multiplication = 50 

Eg iv (arithmetic operators – for quotient)
   <?php
  $x=10;
  $y=5;
              //quotient
  $div=$x/$y;
  echo "Div = ".$div."<br/>";
 ?> 
 Output : Div = 2 

Eg v (arithmetic operators % for remainder)
   <?php
  $x=10;
  $y=3;
               //remainder
  $rem=$x%$y;
  echo "remainder=".$rem."<br/>";
 ?> 
 Output : sub = 0 

$x and $y are two integer variables here
there are five blocks/modules in this example they are to preform addition, subtraction, multiplication, division and modulus respectively.
$x store the value 10,
$y store the value 5.
The output of first module is addition of two values 10 and 5 that is 15 ($x+$y=15).
The output of second module is subtraction of two values 10 and 5 that is 5 ($x-$y=5).
The output of third module is multiplication of two values 10 and 5 that is 50 ($x*$y=50).
The output of fourth module is division of two values 10 and 5 that is 2 ($x/$y=2).
The output of last module is modulus of two values 10 and 3 that is 1 ($x%$y=1).

PHP Assignment Operators

There are a few other Operators that tend to do some arithmetic operations
and store the result in same. for eg the addition-assignment operator, represented
by the symbol +=, lets you simultaneously add and assign a new value to a variable.
Common Assignment Operators
OperatosDescription
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign quotient
%=Divide and assign modulus
.=Concatenate and assign(its used only for sting)
and here’s example illustrating these operators in action.

Eg i (for add and assign)
 
   <?php  
 $x = 500;
 $x+= 500;
 echo "sum=".$x."<br/>";
  ?>
  
   Output : sum=1000
In the above example
Initialize variable ($x) with value = 500. If we want to add 500 to this value . we don’t need a second and third variable to store the sum of value($x+=500) it means ($x=$x+500 ) .
add 500 and re-assign new value(1000) back to same variable ($x).
Eg ii (for subtract and assign)
 
   <?php  
 $x = 1000;
 $x-= 500;
 echo "subtraction = ".$x."<br/>";
  ?>
  
   Output : subtraction = 500
In the above example
Create and initialize variable($x) hold value = 1000. now perform subtraction. 500 is subtract from 1000 using($x-=500) it means($x=$x – 500 ) .
Now assign new value back to the initialize variable($x). so the output will become:

Eg iii (multiply and assign)
 
   <?php  
 $x = 100;
 $x*= 10;
 echo "Multiplication = ".$x."<br/>";
  ?>
  
   Output : Multiplication= 1000
In the above example
Variable( $x) with value=100. now perform multiplication. ($x*=10) now value 10 multiply with previous value(100).
and the output will become:1000 and it re-assign back to the variable ($x)
so the output will become : 1000
Eg iv (Divide and assign quotient)
 
   <?php  
 $x = 1000;
 $x/= 500;
 echo "Quotient = ".$x."<br/>";
  ?>
  
   Output : Quotient = 2
In the above example.
Declare Variable( $x) with value=1000. now perform divide.($x/=500) now value 500 divide with previous value(1000).
and the output will become:2 and it re-assign value=2, back to the variable ($x).

Eg v (Divide and assign modulus)
 
   <?php  
 $x = 5;
 $x%= 2;
 echo "Remainder = ".$x."<br/>";
  ?>
  
   Output : Remainder= 1
In the above example.
Variable($x) with value =5. Now calculate the modulus using ($x%=2) .
it gives remainder value=”1″ and this remainder assign again to variable($x).
and the output will become : 1

Eg vi (Concatenate and assign)
 
   <?php  
 $str = "Welcome ";
 $str. = "to the world of truetech4";
 echo $str."<br/>";
  ?>
  
   Output : Welcome to the world of truetech4
In the above example.
Declare variable($str). With string value=”Welcome” .
Now concatenate this string to another string using the same variable
by performing ($str.=”to the world of php”).
It concatenate “welcome” with “to the world of php” and
The output will become this: Welcome to the world of truetech4

PHP Comparative Operators


PHP lets you Compare one variable or value with another via its wide range of comparison operators.

Common Comparison Operators
OperatosDescription
==Equal to
===Equal to and of the same type
!=Not equal to
!==Not equal to and of the same type
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Here’s example illustrating these operators in action

Eg i (== and ===)
    <?php
 $x=10;
 $y=10.0;
 echo ($x==$y);
  //it returns true because both the variable contains same value.
 
 echo ($x===$y);
 /*it returns false because === strongly compares.
 here both variable contain same value i.e 10 but different data
  type one is integer and another is float.*/ 
   ?>
in the above example.
Two variable $x , $y define
$x hold the value 10
$y hold value 10.0
Now perform several operation on this
First check ($x==$y)=>it returns true because the value for both is same
Second Check($x===$y)=>it returns false because now it also compare data-type. $y hold a float value.
Eg ii (== and ===)
    <?php 
 //another example
 $bool=(boolean)1;
 $int=(integer)1;
  
 //return true because both have same value.
 echo ($bool==$int);
  
 //return false because both have same value but diff data type
 echo ($bool===$int); 
    ?>  
$bool=(boolean)1
($bool==$int) it returns true because both have same value
$int= (integer)1
($bool===$int) its return false because both have different data type
Eg iii (>, <, >=, <=)
    <?php   
  $a=10;
  $b=11;
  
  echo $a>$b;
  //return false because $a is less than $b.
  
  
  echo $a<$b;
  //return true because $a is less than $b.
  
  echo $a>=$b;
  //return false because neighter $a is greater nor equal to $b
  
  
  echo $a<=$b;
 //return true because $a is than $b.
   
 ?>


$a hold the value 10
$b hold the value 11
check ($a>$b)=> returns false bacause $a less than $b.
check($a>=$b)=> returns true beacuse $a neighter grater nor equal to $b.

PHP Logical Operators


Logical operators really come into their own when combined with conditional tests.
following example illustrating these operators.
Logical Operators
OperatosDescription
&&and
||or
!not
  • and
Operator name and pass
Description If name and pass both are true then result true.
Explanation if name = = "alex" and pass = = "alex123" then it will redirect on truetech4 page, and if any one of these is not valid then it shows an error message(Invalid name or password).
Eg i
 <?php
  $name="alex";
  $pass="alex123";
  if($name=="alex" && $pass=="alex123")
  {
   header('location:http://truetech4.blogspot.in/');
  }
  else
  {
  echo "Invalid name or password"; 
  }
    
 ?>

Output : This program will redirect you on "http://truetech4.blogspot.in/" page

In the above example
Two variable $name and $pass with value(“alex”,”alex123″)
If both value are exist then it will redirect on truetech4.blogspot.in because of header( ). Otherwise invalid name or password.
Here both the condition are true so as output you will be redirected on “http://truetech4.blogspot.in/” page.
  • or
Operator name or pass
Description If name or pass are true then result true.
Explanation if name = = "alex" or pass = = "alex123" then it will redirect on truetech4 page, and if both are false then it shows an error message(Invalid name or password).
Eg ii
 <?php
  $name="alex";
  $pass="alex123";
  if($name=="alex" || $pass=="alex12345")
  {
   header('location:http://truetech4.blogspot.in/');
  }
  else
  {
  echo "Invalid name or password"; 
  }
    
 ?>

Output : This program will redirect you on "http://truetech4.blogspot.in/" page
in the above example
Two variable $name or $pass
$name hold the value=”alex”
$pass hold the value=”alex123″
if any of one condition is true then redirect you on “http://truetech4.blogspot.in/” page otherwise so invalid name or password.
Here one of the both condition is true so as output you will be redirected on “http://truetech4.blogspot.in/” page.
  • not
Operator not
Description reverse the logical test
Explanation check given number is odd or not. Here $num stores 11 and its modulus is 1. By example $num mudulus is not equal to 0 it is an odd number so 11 is an odd numder.
Eg iii
       <?php
  $num=11;
  if($num%2!=0)
  {
   echo $num." is odd number";
  }
  else
  {
   echo $num." is even number"; 
  }
    
 ?>

Output : 11 is odd number

In the above example
take a variable $num with value = 11
let we check number is odd or even. we give a condition($num%2!=0) inside if it will not true then number is odd. otherwise else statement is execute ( Number is even ).
Here number is not divided by 2 so the output display : given number is odd number

Create a Login page using && and || operator

Eg iv
  <?php
     if(isset($_GET['login']))
       {
        $eid=$_GET['e'];
        $pass=$_GET['p'];
 if($eid=="" || $pass=="")
 {
 echo "<font color='red'>Please fill your email and pass</font>";
 }
 else
 {
  if($eid=="xyz" && $pass=="xyz123")
  {
                echo "<font color='blue'>welcome xyz</font>";
  }
  else
  {
  echo "<font color='red'>wrong email or pass</font>";
  }
 }
 
}
    
 ?>

<form>
 Enter your email<input type="text" name="e"/><br/>
 Enter your pass<input type="password" name="p"/>
 <input type="submit" value="Signin" name="login"/>
</form>
       Output : wrong email or pass
             Enter your email
      Enter your pass
         

In the above example
Here we create a form of two field .By default the method of form is ‘GET’
First filed is for Email
Second filed is for password
A logic is define in PHP script. First it’s check the isset( ) function for existence, Enter the name and password in name and password field it store value in variables ($eid and $pass). if either $eid or $pass value is null then a message is shown “fill your email or password”.
Otherwise it check the $eid and $Pass value with the given existing value.
if match then message show “welcome xyz” else message show “wrong email or password.”

0 comments:

Post a Comment

FIND US ON FACEBOOK

FIND US ON Twitter