TOC

The community is working on translating this tutorial into Turkish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Data types:

Data types

As mentioned in the previous chapter, PHP is a loosely typed language. You don't have to tell the interpreter which type a certain variable is, you just have to assign a value to it, and PHP will know which type to treat it as. In the perfect world, you would never have to care about the type of a variable, but as we all know, the world is far from perfect. There will be many situations where controlling the type of a variable will make sense, and therefore, PHP does expose functions to detect and manipulate the type of a variable. First, a little bit of information about the various types in PHP.

PHP consists of 4 basic data types:

boolean - a boolean is actually much like an integer, but with only two possible values: 0 or 1, which is either false or true.

integer - a number with no decimals, e.g 3 or 42.

float (sometimes referred to as double) - a number that may include decimals, e.g. 42.3 or 10.9.

string - A number of characters, which combined makes a text string.

Besides that, there are a couple of more complex datatypes:

array - holds an array of items, e.g. several strings or several integers. An array may contain variables which are arrays which are arrays and so on.

object - a reference to an instance of a class. This is related to Object Oriented programming, which we will talk more about later on in this tutorial.

There are also a couple of special types:

resource - holds a reference to a special external resource. It may be a file resource, or perhaps an open database connection.

NULL - a value of null is nothing. It's not the same as 0 (zero), because that's actually a value. Null is truly nothing. Variables which have not yet been assigned a value, or which you have used the unset() method on, will carry the NULL value. This is useful if you wish to check whether or not a variable contains any value - you may compare it against the NULL constant.

In the next couple of chapters, we will look into working with both strings and numbers (integers and floats), and later on, we will look into both arrays and objects.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!