PHP: Types - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<Koment��eBooleans>
view the version of this page
Last updated: Thu, 15 Jul 2004

Kapitola 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.

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

Pozn�mka: 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
Trizor of www.freedom-uplink.org
30-Jun-2004 01:14
The differance of float and double dates back to a FORTRAN standard. In FORTRAN Variables aren't as loosly written as in PHP and you had to define variable types(OH NOES!). FLOAT or REAL*4 (For all you VAX people out there) defined the variable as a standard precision floating point, with 4 bytes of memory allocated to it. DOUBLE PRECISION or REAL*8 (Again for the VAX) was identical to FLOAT or REAL*4, but with an 8 byte allocation of memory instead of a 4 byte allocation.

In fact most modern variable types date back to FORTRAN, except a string was called a CHARACHTER*N and you had to specify the length, or CHARACHTER*(*) for a variable length string. Boolean was LOGICAL, and there weren't yet objects, and there was support for complex numbers(a+bi).

Of course, most people reading this are web programmers and could care less about the mathematical background of programming.

NOTE: Object support was added to FORTRAN in the FORTRAN90 spec, and expanded with the FORTRAN94 spec, but by then C was the powerful force on the block, and most people who still use FORTRAN use the FORTRAN77.
26-Dec-2003 03:51
You can use parentheses to separate a variable name.
e.g.) print "$(var_name)is nice";

Without parentheses, PHP will be failed to parse the variable name.
e.g.) print "$var_nameis nice"; (--> parse fails)
Bryan
31-Oct-2003 12:03
To explain further: 'hello'==0 is TRUE because the types in this case are converted to integers, not to booleans. Any non-numeric string resolves to zero. To illustrate:

(boolean) 'hello' == (boolean) 0  is FALSE.

Also, '12' == 12 is TRUE.
03-Jul-2002 02:22
But 'hello' === 0 is not, === makes sure that they are equivilent and of the same type.
kajism at yahoo dot com
22-Mar-2002 04: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 07: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.


<Koment��eBooleans>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page