Showing posts with label php training. Show all posts
Showing posts with label php training. Show all posts

Monday, September 21, 2009

PHP - Type Juggling & Type Casting

 PHP - Type Juggling & Type Casting 


( Int *  float  =  float )
Ex :
php
              $a = 5;
              $b = 12.7;
              $c = $a+$b;
              echo $c;
             
?>
-----------------------------------------------------------------------
PHP - Type Juggling &Type Casting
It is used to convert one data type to another data type Except  Array and Object .
php
              $a = 10;
              $b = (string)$a;
              var_dump($b);
              echo '
';

              var_dump($a);
             
?>

Read  php Interview Questions and Answers by experts at www.tipsoninterview.com

For Php Training in Hyderabad call 9394799566

PHP - Variable functions

PHP -Variable functions

Gettype()
Settype()
Isset()
Empty()
Unset()
Is_...()
Intval()
Example for gettype()
It is used to get the data type
Var_dump() is used to get datatype,length and value but it return only datatype
php
            $a = 5;
            $b = gettype($a);
            echo $b;
           
?>
Example for settype()
1)It is used to set the data type to a variable
2) if it is set successful then it returns true (1) else it return false (null)
php
            $a = 5;
            $b = settype($a,'string');
            var_dump($b);
            echo gettype($a);
?>
Example for isset()
If variable is existed it returns true(1) else it return false (null)
php
            $a = 10;
            $b = isset($a);
            var_dump($b);
?>
Example for empty()
If variable is empty the it will return true (1) else it return false (null)
php
            $a = 0;
            $b = empty($a);
            var_dump($b);
?>

Empty means :
False
0
Empty string
Empty array
Empty object
Null
Not existed
Non empty means :
True
 -infinity to + infinity  ( Except 0 )
Non empty string
Non empty array
Non empty object
Example for unset()
  1. It is not return any value.
  2. It will remove allotted memory to the variable  (it deletes the variable)
 php
            $a = 10;
            unset($a);
            $b = empty($a);
            var_dump($b);
?>
Example for is_..()
it will return true (1) else it return false (null)
is_integer(var)
ex 1:
php
            $a = 10;
            $b = is_integer($a);
            var_dump($b);
?>
-----------------------------------------------------------------------
ex 2:
php
            $a = '10';
            $b = is_integer($a);
            var_dump($b);
?>
-----------------------------------------------------------------------
is_bool(var)
is_string(var)
is_array(var)
is_object(var)
Example for intval()
  1. when you performed arithmetic operation then “ string conversion method “ will work
  2. But with out performed arithmetic operation , if you want to convert a value into int then intval() is useful
 Ex 1 :
php
            $a = '12abc';
            $b = intval($a);
            echo $b;
?>
Ex 2 :
php
            $a = 'abc';
            $b = intval($a);
            echo $b;
?>


Read  php Interview Questions and Answers by experts at www.tipsoninterview.com

For Php Training in Hyderabad call 9394799566

php - Operators

php - Operators

Arithmetic
Relational
Logical
Concatenation
Assignment
Inc / Dec
Bitwise Operator
Ternary

Arithmetic
            +  Addition
            -   Subtraction
            *   Product
            /    Division
            %  Modules        (5%2 = 1)

      $a = $a + $b
    $a + = $b
      $a = $a - $b
    $a -  = $b
      $a = $a * $b
    $a * = $b
     $a = $a / $b
    $a / = $b
     $a = $a % $b
    $a % = $b

Relational
==     Equals to
===  Identical                       ( It will check value and data type )
!=      Not equals to
!==   Not identical
>       Greater then
<       Less then
>=     Greater then or Equals to
<=     Less then or Equals to
Example 1
php
                   $a = 10;
                   $b = '10abc';
                   if($a == $b)
                             echo 'Pass';
                   else
                             echo 'fail';
?>
Example 2
php
                   $a = 10;
                   $b = '10abc';
                   if($a === $b)
                             echo 'Pass';
                   else
                             echo 'fail';
?>
Logical
And            &&
Or              ||
Not             !
Example
php
                   $a = 5;
                   $b = 20;
                   if($a>2 && $b>15)
                             echo 'Pass';
                   else
                             echo 'fail';
?>
Concatenation
.  or  ,                  
Assignment
$a = 10 ;
$a = $b ;
Incrementation/ Decrementation
                      ++              incrementation
                      --                Decrementation
Ex :
                     
                      $a = 10 ;
                      $a++;   similar to ( $a + = 1)  that is    $a = $a + 1
                      $a-- ;    similar to ( $a - = 1)  that is    $a = $a - 1
            $a = 10 ;
  Post increment
            echo ($a++) ;     -        10;
            echo ($a) ;          -        11;
  Pre increment
            echo (++$a) ;     -        11;
            echo ($a) ;          -        11;
Note :
Unari Operator takes one Value or variable .   (  !  ,  ++  ,  --  )
Binary Operator takes two Values or variables .  
Bitwise Operator
 
And            &
Or              |
Xor            ^
Example1
php
                   $a = 12;
                   $b = 9;
                   echo $a & $b;
?>
                  
Output :  8
Note : 
binary value  -  1100
binary value  -  1001
12  &  9         -  1000   -> decimal value 8   ( 1*23  + 0*22  + 0*21 + 0*20 )
            
            T  &  T  = T                           T  |  T  = T             T  ^  T  = F  (both are same F)
            T  &  F  = F                           T  |  F  = T             T  ^  F  = T  (both are diffarent T)
            F  &  T  = F                           F  |  T  = T             F  ^  T  = T
            F  &  F  = F                           F  |  F  = F             F  ^  F  = F
--------------------------------------------------------------------
Example2
php
                   $a = 12;
                   $b = 9;
                   echo $a | $b;
?>
                     
Output :  13
--------------------------------------------------------------------
Example3
php
                   $a = 12;
                   $b = 9;
                   echo $a ^ $b;
?>
                     
Output :  5
--------------------------------------------------------------------
Ternary
Var = ( condition ) ?   val 1  :   val 2
Example
php
           $a = 10;
           $b = 20;
           $c = ( $a > $b ) ? $a : $b;
          
           echo ( $c );
?>

for php training in Hyderabad call 9394799566