PHP Data Types
PHP variables store different types of data, and different data types can perform different tasks.
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 in single or double quotes:
Example
<?php
$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!';
echo $x;
?>
PHP Integers
An integer is a number without a decimal point.
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
- An integer can be specified in three formats: decimal, hexadecimal (prefixed with 0x), or octal (prefixed with 0)
In the following example, we will test different numbers. The PHP var_dump()
function returns the data type and value of the variable:
Example
<?php
$x = 5985;
var_dump($x);
echo "<br>";
$x = -345; // negative number
var_dump($x);
echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047; // octal number
var_dump($x);
?>
PHP Floats
A float is a number with a decimal point or in exponential form.
In the following example, we will test different numbers. The PHP var_dump()
function returns the data type and value of the variable:
Example
<?php
$x = 10.365;
var_dump($x);
echo "<br>";
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>
PHP Booleans
A boolean represents two possible states: TRUE or FALSE.
Booleans are often used in conditional testing. You will learn more about conditional statements in the upcoming chapters.
PHP Arrays
An array stores multiple values in one single variable.
In the following example, an array is created, and then the PHP var_dump()
function returns the data type and value of the array:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>
You will learn more about arrays in the upcoming 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 of 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 use the data type in an instantiated class:
Example
<?php
class Car
{
var $color;
function __construct($color="green") {
$this->color = $color;
}
function what_color() {
return $this->color;
}
}
?>
In the above example, this
is a pointer to the current object instance, not pointing to any other object or class.
You will learn more about objects in the upcoming chapters.
PHP NULL Values
A NULL value indicates that a variable has no value. NULL is the only value of the NULL type.
NULL values are used to indicate whether a variable is empty. It is also used to differentiate between empty values and NULL values.
You can clear a variable's data by setting its value to NULL:
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
PHP Resource Types
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 graphics canvas areas.
Since resource variables hold special handles for open files, database connections, graphics canvas areas, etc., it is not meaningful to convert other types of values to resources.
The get_resource_type()
function can return the type of a resource:
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 occur.
Here is an example:
Example
<?php
$c = mysql_connect();
echo get_resource_type($c)."\n";
?>
// Print: mysql link
$fp = fopen("foo","w");
echo get_resource_type($fp)."\n";
// Print: file
$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc)."\n";
// Print: domxml document
?>