PHP Data Types
PHP variables store different types of data, and different data types can do different things.
PHP supports the following data types:
- String
- Integer
- Float
- Boolean
- Array
- Object
- NULL
- Resource
PHP Strings
A string is a sequence of characters, like "Hello world!".
You can place any text in single quotes and double quotes:
PHP Integers
An integer is a number without decimals.
Integer rules:
- An integer must have at least one digit (0-9)
- An integer cannot contain commas or spaces
- An integer has no decimal point
- An integer can be positive or negative
- Integers can be specified in three formats: decimal, hexadecimal (prefixed with 0x) or octal (prefixed with 0).
In the following examples we will test different numbers.
PHP var_dump() The function returns the data type and value of a variable:
Example
Try it Yourself »
PHP Float
A floating point number is a number with a decimal part, or in exponential form.
In the following examples we will test different numbers. The PHP var_dump() function returns the data type and value of a variable:
Example
Try it Yourself »
PHP Boolean
A Boolean can be TRUE or FALSE.
$y=false;
Boolean is often used for conditional testing. You will learn more about conditional control in the next chapters.
PHP Arrays
An array can store multiple values in one variable.
In the following example, an array is created, and then the PHP var_dump() function is used to return the data type and value of the array:
You will learn more about arrays in the next chapters.
PHP Objects
The object data type can also be used to store data.
In PHP, an object must be declared.
First, you must declare a class object using the class keyword. A class is a structure that can contain properties and methods.
Then we define the data type in the class, and then use the data type in the instantiated class:
Example
Try it Yourself »
In the above example, the PHP keyword this is a pointer that points to the current object instance, and does not point to any other object or class.
You will learn more about objects in the next chapters.
PHP NULL Value
The NULL value indicates that a variable has no value. NULL is a value with data type NULL.
The NULL value indicates whether a variable is empty. It can also be used to distinguish between an empty data value and a NULL value.
You can clear the variable data by setting the variable value to NULL:
PHP Resource Type
PHP resource is a special variable that holds a reference to an external resource.
Common resource data types include open files, database connections, graphic canvas areas, etc.
Because resource type variables hold special handles for open files, database connections, graphic canvas areas, etc., it is meaningless to convert values of other types to resources.
Useget_resource_type()The function can return the resource type:
get_resource_type(resource $handle): string
This function returns a string that represents the type of the resource passed to it. If the parameter is not a valid resource, an error will be generated.
The following example:
Example
$c = mysql_connect();
echo get_resource_type($c)."\n";
// prints: mysql link
$fp = fopen("foo","w");
echo get_resource_type($fp)."\n";
// prints: file
$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc)."\n";
// prints: domxml document
?>