Monday, 10 August 2015

PHP Operators :- 
  • Operators are used to operate on values.
  • Consider an example, "1+3=4". Here, "1 & 3" are called as operands and "+" is called as operators.
  • Types of Operators :-
      1. Arithmetic Operators.
      2. Comparison Operators.
      3. Logical(or Relational) Operators.
      4. Assignment Operators.
      5. Conditional(or ternary) Operators.
  • It can be illustrated further with suitable examples :-
 arithematic operators.php :-
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
    $x = 62;
    $y = 40;
    
    $z = $x + $y;
    echo "Addtion Operation Result: <b> $z </b> <br/> <br/>";
    
    $z = $x - $y;
    echo "Substraction Operation Result: <b> $z </b> <br/> <br/>";
    
    $z = $x * $y;
    echo "Multiplication Operation Result: <b> $z </b> <br/> <br/>";
    
    $z = $x / $y;
    echo "Division Operation Result: <b> $z </b> <br/> <br/>";
    
    $z = $x % $y;
    echo "Modulus Operation Result: <b> $z </b> <br/> <br/>";
    
    $z = $x++; 
    echo "Increment Operation Result: <b> $z </b> <br/> <br/>";
    
    $z = $x--; 
    echo "Decrement Operation Result: <b> $z </b> <br/> <br/>";
?>
</body>
</html>

Screenshot:-

comparison operators.php :-
<html>
<head><title>Comparision Operators</title><head>
<body>
<?php
    $x = 62;
    $y = 40;
    

    echo "<b> x =$x </b>"; echo "<br/>";
    echo "<b> y =$y </b>"; echo "<br/> <br/>";


    if( $x == $y ){
       echo "x is equal to y<br/><br/>";
    }else{
       echo "x is not equal to y<br/><br/>";
    }

    if( $x > $y ){
       echo "x is greater than  y<br/><br/>";
    }else{
       echo "x is not greater than y<br/><br/>";
    }
    if( $x < $y ){
       echo "x is less than  y<br/><br/>";
    }else{
       echo "x is not less than y<br/><br/>";
    }
    if( $x != $y ){
       echo "x is not equal to y<br/><br/>";
    }else{
       echo "x is equal to y<br/><br/>";
    }
    if( $x >= $y ){
       echo "x is either grater than or equal to y<br/><br/>";
    }else{
       echo "x is neither greater than nor equal to y<br/><br/>";
    }
    if( $x <= $y ){
       echo "x is either less than or equal to y<br/><br/>";
    }else{
       echo "x is neither less than nor equal to y<br/><br/>";
    }
?>
</body>
</html>

Screenshot:-


logical operators.php:-
<html>
<head><title>Logical Operators</title><head>
<body>
<?php
    $a = 42;
    $b = 21;

    echo "<b> a =$a </b>"; echo "<br/>";
    echo "<b> b =$b </b>"; echo "<br/> <br/>";

    if( $a && $b ){
       echo "Both a and b are true<br/>";
    }else{
       echo "Either a or b is false<br/>";
    }
    if( $a and $b ){
       echo "Both a and b are true<br/>";
    }else{
       echo "Either a or b is false<br/>";
    }
    if( $a || $b ){
       echo "Either a or b is true<br/>";
    }else{
       echo "Both a and b are false<br/>";
    }
    if( $a or $b ){
       echo "Either a or b is true<br/>";
    }else{
       echo "Both a and b are false<br/>";
    }
?>
</body>
</html>

Screenshot:-


assignment operators.php :-
<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
    $a = 42;
    $b = 20;
    
    echo "<b> a =$a </b>"; echo "<br/>";
    echo "<b> b =$b </b>"; echo "<br/> <br/>";

    $c = $a + $b;
    echo "c = a + b<br/>";   
    echo "Addtion Operation Result: $c(c's value) <br/><br/>";

    $c += $a; 
    echo "c = c + a<br/>"; 
    echo "Add AND Assigment Operation Result: $c(c value was 42 + 20 = 62) <br/><br/>";

    $c -= $a; 
    echo "c = c - a<br/>";
    echo "Subtract AND Assignment Operation Result: $c(c value was 42 + 20 + 42 = 104) <br/><br/>";

    $c *= $a; 
    echo "c = c * a<br/>";
    echo "Multiply AND Assignment Operation Result: $c(c value was 104 - 42 = 62) <br/><br/>";

    $c /= $a; 
    echo "c = c / a<br/>"; 
    echo "Division AND Assignment Operation Result: $c(c value was 62 * 42 = 2604) <br/><br>";

    $c %= $a; 
    echo "c = c % a<br/>";
    echo "Modulus AND Assignment Operation Result: $c(c value was 2604/42 = 62) <br/><br/>";
?>
</body>
</html>

Screenshot:-


conditional operators.php :-
<html>
<head><title>Conditional Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;
    
    echo "<b> a =$a </b>"; echo "<br/>";
    echo "<b> b =$b </b>"; echo "<br/> <br/>";

    echo "result = (a > b) ? a : b<br/>";
    $result = ($a > $b ) ? $a :$b;
    echo "Value of result is <b> $result </b> <br/><br/>";

    echo "result = (a < b) ? a : b<br/>";
    $result = ($a < $b ) ? $a :$b;
    echo "Value of result is <b> $result </b> <br/><br/>";
?>
</body>
</html>

Screenshot:-




No comments:

Post a Comment