Operand,Operators and Expression in PHP

Operator is a mathematical symbol which used to tell compiler to do mathematical operations and logical operations. Operators operates on variables and constants. These variables or constants are called operands. Operators and Operator form an expression.

   

Following are type of operators in PHP.

Increment & Decrement operators:-

 These operators are used for increasing and decreasing values of variable.These operators need only one operand so they operators are also called Unary operators.

Some examples of unary operators.
 
a++,++a,--a,a--


<?php
   $a = 10;
   echo $a++;
   echo "<br/>";
   echo ++$a;
   echo "<br/>";   
   echo --$a;
   echo "<br/>";
   echo $a--;
?>


Arithmetic Operator:- 

These operators are used for addition, substration , multiplication and divisions operations.These operators use two operands there fore These operators are also called binary operators.

Some examples of Binary Operators.
 
 +,-,*,/,%

<?php
   $a = 10;
   $b = 3;
   $c = $a + $b;
   echo $c;
   echo "<br/>";
   $c = $a - $b;
   echo $c;
   echo "<br/>";
   $c = $a * $b;
   echo $c;
   echo "<br/>";
   $c = $a / $b;
   echo $c;
   $c = $a % $b;
   echo $c;

?>



Assignment Operators:-

 These operatos are used for assign values to variables.

' =  ' is an example of assignment operator.

<?php
 
 $a = 20;  // Assignment Operator assign 20 to variable a
 echo $a ;

?>




Arthmetic Assignment Operators:-

 These operators are used for both arthmetic and assignment operation.
Below are examples of Arthmatic Assignment operators.
 +=,-=,*=,/=

<?php

   $a = 10;
   $b = 3;
   $b +=$a;
   echo $b;
     echo "<br/>";
   $b -=$a;
   echo $b;
   echo "<br/>";
   $b *=$a;
   echo $b;
   echo "<br/>";
   $b /=$a;
   echo $b;
   echo "<br/>";
?>



Comparison operators:-

 These operators are used for comparing variable values.
= =,<,>,!=  are examples of comparison operators.



<?php

$a = 20;

$b =20;

if($a == $b){

   echo "Equal";

}
$a = 20;
$b = 2;
echo "<br/>";

if($a != $b){

   echo "Not Equal";

}
echo "<br/>";

if($a > $b){

   echo "a greater than b";
}
echo "<br/>";
$a = 2;
$b = 20;
if($a < $b){

   echo "a less than b";

}

echo "<br/>";

if($a != $b){

   echo "a not equal to b";
}

?>






Logical Operators:-

These operators are used for logical operations like true or false.

&& , || , ! are logical operators.


<?php
$a = 20;
$b =20;
$c = 20;
if($a == $b && $a == $c){
   echo "true";
}
echo "<br/>";
if($a == $b || $a == $c){

   echo "true";

}
echo "<br/>";
$a =2;
$b = 3;
if($a != $b){

   echo "true";

}
?>


Login Form and Authentication in PHP


This post is about the Simple login form and Authentication in PHP.This Authentication is very basically used for an little application.More about Secure and Advanced Authentication will be discussed in later posts.


This is Simple login form you can used in any application . This consists of user name and password field and submit button.

Below is html code of Login Form




index.php
------------

<html>
  <head>
       <title>Login</title>
   </head>
   <body>
   <fieldset style="margin-right:600px;">
   <legend>Login</legend>
   <form action="auth.php" name="login" method="post">
       <table>
        <?php
            if(isset($_GET['msg'])){
        ?>
        <tr>
            <td><?php echo $_GET['msg'];?>
            </td>
        </tr>
           
        <?php       
           
            }
        ?>
        <tr>
            <td>User Name</td>
            <td><input type="text" name="user_name"/></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password"/></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="submit"/></td>
        </tr>
   </form>
   </fieldset>
  </body>
  </html>

After putting login details this form will submit for authentication.Following is Authentication Code.

auth.php
-----------
 
<?php
 $username = $_POST['user_name'];
 $pass = $_POST['password'];
 if($username == "john" AND $pass == "abc"){
     header("location:welcome.php");

 }
 else{
     header("location:index.php?msg=Invalid user name or password");
 }
?> 


If user is authenticated successfully then it will direct to Welcome page otherwise it will redirect to index page with error message.

Welcome.php
--------------

<html>
  <head>
       <title>Login</title>
   </head>
   <body>
      <h1>Welcome !</h1>
  </body>
  </html>



 Thanks For Reading..........