One of the reasons why PHP is so easy to use is that it does not ask its user to specify the PHP data types of certain PHP variables manually. Instead, it understands it automatically according to the value that has been assigned to it.
PHP variables differ significantly in value. Because of that, PHP can classify variables into certain PHP data types. Nevertheless, it's useful to know and understand what main PHP data types are there, and how they differ from each other.
Basically, eight types can be separated into three bigger groups. There are scalar types - integers, strings, booleans, and floats. More complicated are the compound types - array and objects. Resource and NULL are classified as special types.
Contents
PHP Data Types: Main Tips
- Integers, strings, booleans and floats are the simplest data types.
- Arrays and objects are considered more complex, as they hold more than a single value. In contrast, NULL holds no value at all.
- Resource is technically not a data type, because it points to an outside reference.
Strings Explained
PHP strings are sequences of characters. It can consist of letters, numbers or special characters, make up words or sentences. You can learn more about them in a separate tutorial, too.
You must remember to encase the whole string in quotes, though there is no difference if you will use single or double ones:
<?php
$txt1 = "Hello world!";
$txt2 = 'Hello world!';
echo $txt1;
echo "<br>";
echo $txt2;
?>
Note: If you want to display quotes inside a string, you need to surround them with a different kind of quotes. For example, if you surround a string with single quotes, you can use double quotes inside, and vice versa.
What Integers Are
Intengers as a data type are used to hold numeric values. It can come in decimal (base 10), hexadecimal (base 16) and octal (base 8) forms. Since PHP 5.4+, binary form (base 2) has also been introduced.
Integers have to follow a couple of rules:
- They must contain at least a single digit.
- They have to be whole numbers (no decimal points).
- They can be positive or negative, but must stay between -2,147,483,648 and 2,147,483,647.
The example below has an integer $x
. A function called PHPvar_dump()
will return the data type and value of a variable:
Description of Floats
If you do use a decimal point or numbers in an exponential form in a variable, it will be classified as a float (floating point number). It can also be positive or negative.
In the example below you can see a float $x
. PHP var_dump()
function will return the data type and value of a variable:
Meaning of Booleans
A boolean in an extremely lightweight data type. It can have only one of two values: True or False. You can treat it as a sort of switch, which is useful for conditional statements.
Looking at the PHP class example, notice these values are not put in quotes: by doing that, you would produce a simple string.
<?php
$submit = true;
if ($submit !== false)
{
echo 'Submitted';
}
?>
Arrays: Lists of Values
Arrays are one of the more complex PHP data types. An array is a single variable that can store multiple values. Those are usually related and of the same type - for example, it can hold a list of your dream countries or all the books on your nightstand.
The example below has an array $x
. PHP var_dump()
function will return the data type and value of a variable:
Objects Defined
Objects are different from other PHP data types because they hold not only the data itself but also all the information on how that particular data can be processed. In addition to that, the PHP object has always to be declared.
Firstly, the class of the object has to be declared. To do this, you have to use a class
keyword. Classes are structures containing properties and methods.
Let's see an example:
<?php
class Book {
function page() {
$this->pageNumber = '2';
}
}
// create an object
$book = new Book();
// show object properties
echo $book->pageNumber;
?>
What NULL Means
It is a special type of data, which can only have a single value: NULL. NULL variables have no real value assigned to them.
Variable values can be cleared by setting them to NULL. Variables that are created without an assigned value are assigned the value of NULL by default.
Resources Identified
Resources technically shouldn't even be classified as one of PHP data types: it's more like storage of a reference to various resources outside of PHP. Examples of such external resources might be databases or opened files.
PHP Data Types: Summary
- There are eight PHP data types in total: integers, strings, booleans, floats, arrays, objects, NULL and resource.
- Arrays and objects hold more than a single value. NULL holds no value at all.
- Resource is used to direct the used to an outside reference.