Wednesday, January 14, 2009
Data Types in PHP
11:58 PM |
Posted by
Hari Kiran |
Edit Post
PHP supports eight primitive types.
Four scalar types
Two compound types
We introduce some pseudo-types for readability:
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().
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:
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.
Four scalar types
- boolean
- integer
- float (floating-point number, or 'double')
- string
Two compound types
- Array
- object
- resource
- NULL
We introduce some pseudo-types for readability:
- mixed
- number
- callback
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().
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
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool); // prints: boolean
echo gettype($a_str); // prints: string
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool); // prints: boolean
echo gettype($a_str); // prints: string
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_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.
Labels:
PHP Basics
Subscribe to:
Post Comments (Atom)
Blog Archive
-
►
2013
(1)
- ► September 2013 (1)
-
►
2011
(7)
- ► April 2011 (7)
-
►
2010
(1)
- ► April 2010 (1)
-
▼
2009
(11)
- ► December 2009 (2)
- ► February 2009 (3)
-
►
2007
(3)
- ► October 2007 (3)
Categories
- Ajax (6)
- Buddha (2)
- Buddhism (2)
- Favorite Movie (1)
- First (1)
- Four Noble Truths (1)
- Ganesha (1)
- Hindu Deities (2)
- Hinduism (3)
- Krishna (1)
- Meditation (1)
- Namasthe (1)
- PHP Basics (3)
- power ful (1)
- respecting others (1)
- salutation (1)
- siva (1)
- siva tandava stotram (1)
- Speed up windows (1)
- Spiritual (1)
- Start up Programs (1)
- stotram (1)
- Windows Services (1)
- XP optimization (2)
- XP Services (1)
Powered by Blogger.
0 comments:
Post a Comment