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 put any text inside single or double quotes:
Example
" ; $x = ' Hello world! ' ; echo $x ; ?>Try it »
PHP Integers
An integer is a number without a fractional part.
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 examples below we will test different numbers.
PHP var_dump() The function returns the data type and value of a variable:
Example
" ; $x = - 345 ; // negative var_dump ( $x ) ; echo "" ; $x = 0x8C ; // hexadecimal number var_dump ( $x ) ; echo "
" ; $x = 047 ; // octal number var_dump ( $x ) ; ?>
Try it »
PHP Float
A float is a number with a decimal point or in exponential form.
In the examples below we will test different numbers. The PHP var_dump() function returns the data type and value of a variable:
Example
" ; $x = 2 .4 e3 ; var_dump ( $x ) ; echo "" ; $x = 8 E - 5 ; var_dump ( $x ) ; ?>
Try it »
PHP Boolean
A boolean can be TRUE or FALSE.
$x=true;
$y=false;
Booleans are typically used in conditional testing. You will learn more about control structures in the coming chapters.
PHP Arrays
An array can store multiple values in a single variable.
In the example below an array is created, then the PHP var_dump() function is used to return the data type and value of the array:
Example
Try it »
You will learn more about arrays in the coming chapters.
PHP Objects
The object data type can also be used to store data.
In PHP, objects 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 types within the class, and use them in the instantiated class:
Example
color = $color ; } function what_color ( ) { return $this -> color ; } } ?>Try it »
In the example above, the PHP keyword $this is a reference to the current object instance, not to any other object or class.
You will learn more about objects in the coming chapters.
PHP NULL Value
A NULL value indicates that a variable has no value. NULL is the value of the NULL data type.
A NULL value indicates whether a variable is empty. It can also be used to distinguish between an empty value and a NULL value.
You can clear a variable's data by setting its value to NULL:
Example
Try it »
PHP Resource Type
A PHP resource is a special variable that holds a reference to an external resource.
Common resource data types include open files, database connections, and graphic canvas areas.
Because a resource variable holds special handles to open files, database connections, graphic canvas areas, etc., it makes no sense to convert values of other types to resources.
Use get_resource_type() A function can return a resource type:
get_resource_type(resource $handle): string
This function returns a string representing the type of the resource passed to it. If the argument is not a valid resource, an error will be generated.
The following example: