PHP Types
PHP supports eight primitive types.
Four scalar types:
boolean (expresses a truth value. It can be either TRUE or FALSE.)
integer (is a number of the set Z = {..., -2, -1,
0, 1, 2, ...}. )
float (floating-point number, aka 'double', like
$a = 1.234;)
string (is series of characters)
Two compound types:
array
array is actually an ordered map. This type is
optimized in several ways, so you can use it as a real array, or
a list (vector), hashtable (which is an implementation of a map),
dictionary, collection, stack, queue and probably more. Because you
can have another PHP array as a value, you can also quite easily
simulate trees.
An array can be created by the array() language-construct. It takes
a certain number of comma-separated key => value pairs.
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
A key may be either an integer or a string.
object
To initialize an object, you use the new statement to instantiate
the object to a variable.
<?php
class foo
{
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
?>
two special types:
resource
A resource is a special variable, holding a reference
to an external resource. Resources are created and used by special
functions
NULL
NULL is
the only possible value of type NULL. Used like
$abc=null
some pseudo-types:
mixed
mixed indicates that a parameter may accept
multiple (but not necessarily all) types.
number
number indicates that a parameter can be either integer or float.
callback
Callback functions can not only be simple functions but also object
methods including static class methods.
|