PHP/Data Type/Data Type

Материал из Web эксперт
Версия от 10:03, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create Object and check its type

   <source lang="html4strict">

<?php

 class employee {
     var $emp_code;
     var $name;
     var $address;
     var $department;
     var $sex;
     var $date_of_birth;
     var $salary;
 }
 $dave = new employee;
 $alice = new employee;
 echo(is_object($dave)); 
 ?>
          
      </source>
   
  


Create Object and get its type

   <source lang="html4strict">

<?php

 class employee {
     var $emp_code;
     var $name;
     var $address;
     var $department;
     var $sex;
     var $date_of_birth;
     var $salary;
 }
 $dave = new employee;
 $alice = new employee;
 echo gettype ($dave)," \n";
 echo gettype ($alice), " \n";
 ?>
          
      </source>
   
  


gettype() and settype() Data Types

   <source lang="html4strict">


Data Type Returned by gettype Set by settype


Array Array Array Boolean Boolean Boolean Double Double Double Integer Integer Integer Null Null N/A Object Object Object Resource Resource N/A String String String Unknown Unknown type N/A

      </source>
   
  


is_numeric in action

   <source lang="html4strict">

<?php

  $item = 43;
  echo "The variable \$item is numeric: ".is_numeric($item)."
";

?>


      </source>
   
  


PHP Data Types

   <source lang="html4strict">

Type Description Boolean Possible values are True and False. Float Floating-point values. Integer Integer values. String Any series of ASCII characters 0?255. PHP strings are binary safe. Array An indexed list of other values. All data types are allowed as values. Object A class instance. Resource A handle to an internal data structure. This can be a database connection or a result set.

 </source>
   
  


Returns true if $a is a floating-point number

   <source lang="html4strict">

<? $a = 0.1; echo( is_double($a) ); ?>

      </source>
   
  


Returns true if $a is an array

   <source lang="html4strict">

<?

$a=array(0=>"zzzz", 
         "a"=>"aaa", 
         "b"=>"bbb", 
         "c"=>"ccc");
echo(is_array($a));

?>


      </source>
   
  


Returns true if $a is an integer

   <source lang="html4strict">

<? $a = 0; echo(is_integer($a)); ?>

      </source>
   
  


Returns true if $a is a string

   <source lang="html4strict">

<? $a = "aa"; echo(is_string($a)); ?>

      </source>