PHP 5.4.31 Released


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:

And the pseudo-variable $....

Some references to the type "double" may remain in the manual. Consider double the same as float; the two names exist only for historic reasons.

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

Note: To check the type and value of an expression, use the var_dump() function.

To get a human-readable representation of a type for debugging, use the gettype() function. To check for a certain type, do not use gettype(), but rather the is_type functions. Some examples:

TRUE;   // a boolean
$a_str  "foo";  // a string
$a_str2 'foo';  // a string
$an_int 12;     // an integer

echo gettype($a_bool); // prints out:  boolean
echo gettype($a_str);  // prints out:  string

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

// If $a_bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
"String: $a_bool";

To forcibly convert a variable to a certain type, 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. The type comparison tables may also be useful, as they show examples of various type-related comparisons.

add a note add a note

User Contributed Notes 3 notes

shojibur at gmail dot com
3 months ago

           echo ++
$b;     //Out put phq 

So, String also incrementing , cooll
office dot stojmenovic at gmail dot com
5 months ago
One small observation about setting type :

= "Hello World!";

// wrong way

$stringToInt = settype($string, "integer");  

// correct way

echo  settype($string, "integer");  // integer
echo gettype($stringToFloat) ; // boolean
Zeeshan Karamat Abbas
7 months ago
In languages like JAVA, we usually have to define the variable data types as well while declaring them but in PHP, we don't have to define the type of the variable while declaring it. Just give a variable any value. For example, we have variable $myFolks. You can set its value as under, say to a string:

$myFolks = "Hossayn opposed Tyranny of Yazid."

And then you can call it or print it and while doing so, you don't have to mention its data type. Afterwards, you can change data type as well:

$myFolks = 5+12+14
Or just $myFolks = TRUE
To Top