PHP Data Types
PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
- string (text values)
- int (whole numbers)
- float (decimal numbers)
- bool (true or false)
- array (multiple values)
- object (stores data as objects)
- null (empty variable)
- resource (references external resources)
Use var_dump() to Get the Data Type
To get the data type and the value of a variable, use the
var_dump() function.
Example
The var_dump() function dumps the data type and the value:
$x = 5;
var_dump($x); // dumps int(5)
Try it Yourself »
PHP String Data Type
A string is a sequence of characters, like "Hello world!".
PHP Int Data Type
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
- An integer must have at least one digit
- An integer must not have a decimal point
- An integer can be either positive or negative
- Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation
PHP Float Data Type
A float (floating point number) is a number with a decimal point or a number in exponential form.
PHP Bool Data Type
A Boolean data type represents two possible states: TRUE or FALSE.
Booleans are often used in conditional testing.
You will learn more about conditional testing in the PHP If...Else chapter.
PHP Array Data Type
An array data type stores multiple values in one single variable.
In the following example $cars is an array:
You will learn more about arrays later in this tutorial.
PHP Object Data Type
An object data type holds an instance of a programmer-defined class.
Example
Check the data type of $myCar:
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("red", "Volvo");
var_dump($myCar);
Try it Yourself »Do not worry if you do not understand the PHP Object syntax, you will learn more about that in the PHP Classes/Objects chapter.
PHP NULL Data Type
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
Variables can also be emptied by setting the value to NULL:
Changing Data Type
If you assign an integer value to a variable, the data type will automatically be an integer.
If you assign a string to the same variable, the data type will change to a string:
If you want to change the data type of an existing variable, but not by changing the value, you can use casting.
Casting allows you to change data type on variables:
You will learn more about casting in the PHP Casting Chapter.
PHP Resource Data Type
The special resource data type is not an actual data type. It holds a reference to an external resource, such as a database connection or a file handler.
We will not talk about the resource data type here, since it is an advanced topic.