Searching...
Sunday 16 September 2012

Different Types Of PHP Opertators

2:58 pm


 Different Types Of PHP Opertators


Operators are used to perform some operations on data.

       php Operators are broadly classified into 2 types.

(a)    Unary operators : An operator that requires only one operand.

Ex : $a++

(b)   Binary operators : An opertator that requires two operands.

Ex: $a + $b

The different types of php opertators are :

1.       Arithmetic operators

+      -    *   /    %



<?php

Echo (10/3).”<br>”;

Echo (10%3).”<br>”;

Echo (-10%3).”<br>”;

Echo (10%-3).”<br>”;

Echo (-10%-3).”<br>”;

$a = “5”;

$b=”tech”;

$c= $a + $b;

Echo $c.”<br>”;

Echo “result =”.$c (10/0).”<br>”;

Echo “application completed”;

?>

2.       Assignment operators

=      +=    -=      *=      /=      %=    .=



$a+=Sb      same as  Sa = $a + $b



<?php

   $a = ($b=4)+5;

   Echo $a;

?>



Note :  an expression is evaluated from right to left.



<?php                                             $x*= $x+=10

  $x =10;                                                = $x = $x + 10

 $x*=$x+=10;                                       =  $x=15

Echo $x;                                          $x = $x * Sx

?>                                                           = 15*15= 225

3.       Increment and decrement operators :



++                       --

<?php

 Echo “<h1>”;

$x = 10;

Echo $x ++.”—“.++$x;

?>



4.       Relational operators :



<     >    <=     >=    ==     !=    <>   ===    !==

<?php

 $a = “11”;

 $b = 11;

If($a==$b)

 Echo “both are equal”;

  Else

Echo “both are not equal”;

?>



== will check only for the content where as === will check for the content and for the type.

<?php

 $a = “11”;

$b = 11;

If($a===$b)

 Echo “both are equal “;

  Else

Echo “both are not equal”;

?>

Output:   both are not equal



5.       Logical operators



Logical operators are used for framing compound operations.



&&(AND)            ||(or)

Note:  in the case of logical operators all the expansions will not be evaluated every time, it is based on the result of the first condition.

                 ($a>$b)&&($b>$c)      or       ($a>$b)||($b>$c)

6.       Negation operator

$a =10

$b=-$a

7.       Error control operator (@):

                           When prepended with an expression , any warning messages that is being generated will gets suppressed.

         A php programme is associated with 4 types of errors.

(a)    A notice

(b)   Parse error

(c)    A warning – if a warning occurs rest of the programe will gets executed.

(d)   A fatal error – if a fatal error occurs the rest of the programe will not gets executed.

<?php

$a = 10;

$b =0;

@annName();

@$c= $a/$b;

Echo = “result=”.$c.”<br>”;

Echo “application completed!!!”;

?>

8.       Bitwise Operators :

                  Bitwise AND   --   &

                  Bitwise OR     --    |

                  Bitwise  XOR  --     ^

In the case of bitwise operators both the operands will be converted to binary form and the corresponding bitwise operations will be performed.

                   Echo (10 & 15) = 10                   

                   Echo (10 | 15 ) = 15                   

                   Echo (10  ^  15) = 5    

Different Types Of PHP Opertators
              


1.       Shift Operators  :
Left shift        <<    à    multiplication
Right shift      >>   à     Division
In the case of shift operators the first operand will be converted to binary form and the corresponding shifts will happen beyond on the second number of operands.
     Echo (100<<2)   ;        400
     Echo (100>>2)  ;         25
    Echo  (100<<4)   ;         1600
    Echo (100>>3)     ;          12
2.       String / concatenation operator :

.                                   .=
3.       Conditional / terenary operator   :

      ?:
<?php
$x  = 10;
$y  = 12;
$lar = 0;
If( $x > $y)
$lar = $x ;
Else
$lar  = $y;
Echo “largest = “ .$lar;
?>
4.       Type operator :

Instanceof
5.       New operator
6.       Scope resolution operator  :             ::
7.       De-referencing  operator  :               -- >
<!--[if gte mso 9]> Untitled Document Operators are used to perform some operations on data. Operators are broadly classified into 2 types. (a) Unary operators : An operator that requires only one operand. Ex : $a++ (b) Binary operators : An opertator that requires two operands. Ex: $a + $b The different types of opertators are : 1. Arithmetic operators + - * / % ”; Echo (10%3).”
”; Echo (-10%3).”
”; Echo (10%-3).”
”; Echo (-10%-3).”
”; $a = “5”; $b=”tech”; $c= $a + $b; Echo $c.”
”; Echo “result =”.$c (10/0).”
”; Echo “application completed”; ?> 2. Assignment operators = += -= *= /= %= .= $a+=Sb same as Sa = $a + $b Note : an expression is evaluated from right to left. = 15*15= 225 3. Increment and decrement operator : ++ -- ”; $x = 10; Echo $x ++.”—“.++$x; ?> 4. Relational operators : < > <= >= == != <> === !== == will check only for the content where as === will check for the content and for the type. Output: both are not equal 5. Logical operators Logical operators are used for framing compound operations. &&(AND) ||(or) Note: in the case of logical operators all the expansions will not be evaluated every time, it is based on the result of the first condition. ($a>$b)&&($b>$c) or ($a>$b)||($b>$c) 6. Negation operator $a =10 $b=-$a 7. Error control operator (@): When prepended with an expression , any warning messages that is being generated will gets suppressed. A php programme is associated with 4 types of errors. (a) A notice (b) Parse error (c) A warning – if a warning occurs rest of the programe will gets executed. (d) A fatal error – if a fatal error occurs the rest of the programe will not gets executed. ”; Echo “application completed!!!”; ?> 8. Bitwise Operators : Bitwise AND -- & Bitwise OR -- | Bitwise XOR -- ^ In the case of bitwise operators both the operands will be converted to binary form and the corresponding bitwise operations will be performed. Echo (10 & 15) = 10 Echo (10 | 15 ) = 15 Echo (10 ^ 15) = 5

0 comments:

Post a Comment