PHP Data Types
A variable can hold many kinds of data, and what you can do with a value depends on its type. PHP works with these types: String, Integer, Float (also known as double), Boolean, Array, Object, NULL, Resource.
PHP String
Strings hold text: a series of characters such as "Hello world!". Wrap the text in single or double quotes, and PHP treats it as a string.
String example
These two variables hold the same text, written once with double quotes and once with single quotes:
Output: PHP script
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
Integers are whole numbers with no decimal part. On 32-bit systems they range from -2,147,483,648 to 2,147,483,647. When writing one, you need at least one digit and no decimal point, and the value may be positive or negative. You can write it as decimal (base 10), hexadecimal (base 16, starting with 0x), or octal (base 8, starting with 0).
To see a value's type alongside the value itself, use var_dump().
Integer example
Here var_dump() confirms that $x is an integer:
Output: PHP script
<?php
$x = 5985;
var_dump($x);
?>
PHP Float
Floats, short for floating point numbers, have a decimal point or are written in exponential form. Once again, var_dump() can show both the type and the value.
Float example
Here var_dump() confirms that $x is a float:
Output: PHP script
<?php
$x = 10.365;
var_dump($x);
?>
PHP Boolean
A Boolean has only two possible values, TRUE and FALSE. You will mostly meet them in conditions, where your code decides what to do next.
Boolean values
Here one variable gets each boolean value:
<?php
$x = true;
$y = false;
?>
PHP Array
With an array, a single variable can hold several values at once. As with the other types, var_dump() reveals the type and the contents.
Array example
The output lists the array's length and every element with its index, type and value:
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>
PHP Object
Object-oriented programming is built on two ideas: classes and objects. Think of a class as a template and an object as one instance made from it. Every object gets the properties and behaviors defined in the class, yet each can hold its own property values.
Give a class a __construct() function, and PHP calls it for you every time you create an object from that class.
Object example
The Car class serves here as the template for three objects, each with its own color and model:
Output: PHP script
<?php
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");
echo $myCar->message();
echo "<br>";
$myCar = new Car("green", "BMW");
echo $myCar->message();
echo "<br>";
$myCar = new Car("blue", "Toyota");
echo $myCar->message();
?>
PHP NULL Value
The Null data type is unusual because it has just one possible value: NULL. It means that a variable has no value assigned. PHP gives this value automatically to a variable created without one, and you can also set it yourself to empty a variable.
NULL example
$x starts with a string and is then emptied, so var_dump() reports NULL:
Output: PHP script
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Change Data Type
A PHP variable's type follows its value. Assign an integer and the variable is an integer; assign a string to that same variable later and it becomes a string. To change the type yourself, use PHP's casting feature or the settype() function.
Casting to change type
Here $x starts as an integer and a cast turns it into a string:
Output: PHP script
<?php
$x = 5;
$x = (string) $x;
var_dump($x);
?>