PHP uses different types of data in its many functions. For example, if you have a script that adds up two numbers, you need to provide two numbers. If a script searches a text within another text, you need to provide two series of characters. These are two simple types of data.
Different types of data take up different amounts of memory and may be treated differently when they are manipulated in a script – for example, a number between 0 and 255 uses only one byte of memory; the series of characters “Johnny” uses 6 bytes of memory – therefore some programming languages demand that the programmer declares in advance which type of data a variable will contain, so they will know how much memory to allocate. PHP doesn’t require you to declare the type of data a variable will use, and just interprets the data according to the context in which that data is used. For example, if you provide a function with a number instead of some characters, the function will treat the number as a set of characters. It will do this, but without changing the type of the data; instead, it will use its own interpretation of that data.
Some of the most common types in PHP are: boolean, integer, floating-point number and string. A boolean type expresses a truth value, so it can be either “TRUE” or “FALSE”. An integer is a whole or real number, meaning a number without a decimal point. On the other hand, a floating-point number (also called float, double, or real number) is a number that includes a decimal point. A string is a collection of characters; when you work with strings in your scripts, they should always be surrounded by double (") or single (') quotation marks. Arrays and objects are also important data types in PHP, but we’ll talk about those later.
You can use PHP’s built-in function gettype() to test the type of any data, and settype() to change the type of that data. You can also change the type by casting, which means that you place the type in brackets in front of the data, thus instructing PHP to create a copy of that data’s value converted to the type you specified. The main difference between settype() and a cast is the fact that casting produces a copy, leaving the original data untouched.
Another important type of data in PHP is “NULL”. If a data has no assigned value, or its value has been unset using the unset() function, then that data is null. NULL is the only possible value of type NULL.
Special functions require you to use special types of data called resources. These data hold references to an external application, for example handlers to opened files, database connections, images, so you cannot convert any value to a resource; it can only be manipulated by a limited number of functions.