Monday, September 21, 2009

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

No comments:

Post a Comment