PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<CommentsBooleans>
view the version of this page
Last updated: Thu, 21 Aug 2003

Chapter 6. Types

Introduction

PHP supports eight primitive types.

Four scalar types:

Two compound types:

And finally two special types:

This manual also introduces some pseudo-types for readability reasons:

You may also find some references to the type "double". Consider double the same as float, the two names exist only for historic reasons.

The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.

Note: If you want to check out the type and value of a certain expression, use var_dump().

Note: If you simply want a human-readable representation of the type for debugging, use gettype(). To check for a certain type, do not use gettype(), but use the is_type functions. Some examples:

<?php
$bool = TRUE;   // a boolean
$str  = "foo";  // a string
$int  = 12;     // an integer

echo gettype($bool); // prints out "boolean"
echo gettype($str);  // prints out "string"

// If this is an integer, increment it by four
if (is_int($int)) {
    $int += 4;
}

// If $bool is a string, print it out
// (does not print out anything)
if (is_string($bool)) {
    echo "String: $bool";
}
?>

If you would like to force a variable to be converted to a certain type, you may either cast the variable or use the settype() function on it.

Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on Type Juggling. Also, you may be interested in viewing the type comparison tables, as they show examples of various type related comparisons.



add a note add a note User Contributed Notes
Types
fabrice at vignals dot nospam dot com
12-Jun-2003 05:36
Compare with print_r(); :

<?php
$a 
= array(12,"1","2", array("a""b""c") );
var_export ($a);
print_r($a);

/* var_export ($a); output:
array (
  0 => 1,
  1 => 2,
  2 => '1',
  3 => '2',
  4 =>
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)
*/

/* print_r($a); output :
Array
(
    [0] => 1
    [1] => 2
    [2] => 1
    [3] => 2
    [4] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)
*/
?>
With var_export you can easily(visibly) know if a number is a string or an integer. <=> representation is valid PHP code.
With print_r, you can only manipulate its return as a string.

See also : http://www.php.net/manual/function.print-r.php
03-Jul-2002 09:22
But 'hello' === 0 is not, === makes sure that they are equivilent and of the same type.
kajism at yahoo dot com
23-Mar-2002 12:39
Be carefull! Comparison of any string with integer zero returns TRUE. For example 'hello'==0 is TRUE.
philip at cornado dot com
06-Jun-2001 02:02
To see if something is numeric (a number) then use is_numeric().  All form data is returned as strings so checking a form value as an integer will return false.

http://www.php.net/is_numeric

<CommentsBooleans>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST