All data stored in PHP variables fall into one of eight basic categories, known as data types . A variable ’ s data type determines what operations can be carried out on the variable ’ s data, as well as the amount of memory needed to hold the data.
PHP supports four scalar data types. Scalar data means data that contains only a single value. Here ’ s a list of them, including examples:
|Scalar Data Type||Description||Example|
|Integer||A whole number||15|
|Float||A floating – point number||8.23|
|String||A series of characters||“Hello world!”|
|Boolean||Represents either true or false||true|
As well as the four scalar types, PHP supports two compound types. Compound data is data that can contain more than one value. The following table describes PHP ’ s compound types:
|Compound Data Type||Description|
|Array||An ordered map (contains names or numbers mapped to values)|
|Object||A type that may contain properties and methods|
Finally, PHP supports two special data types, so called because they don ’ t contain scalar or compound data as such, but have a specific meaning:
|Special Data Type||Description|
|Resource||Contains a reference to an external resource such as a file or database|
|Null||May only contain null as a value meaning the variable explicitly does not contain any value|
Resource types are often returned by functions that deal with external applications or files. The type NULL is reserved for variables that have not been initialized(that is, variables that have not yet had a value assigned to them).
You can use PHP’s built-in function gettype() to test the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type. In Below assigns five different data types to a single variable, testing it with gettype() each time. The commentsin the code show you where the script is in the process.
<html> <head> <title>Listing 4.1 Testing the type of a variable</title> </head> <body> <?php $testing; // declare without assigning echo gettype($testing); // null echo “<br>”; $testing = 5; echo gettype($testing); // integer echo “<br>”; $testing = “five”; echo gettype($testing); // string echo “<br>”; $testing = 5.0; echo gettype($testing); // double echo “<br>”; $testing = true; echo gettype($testing); // boolean echo “<br>”; ?> </body> </html>
- Boolean are the simplest of all PHP data types. Like a switch that has only two states, on and off, it consist of a single value that may be set to either 1 (true) or 0( False). In this listing, $validUser is a Boolean variable set to tru.
- PHP also support two numeric data types: integers and floating-point values. Floating point value(also known as floats or doubles) are decimal or fractional numbers, while integers are round numbers. Both may be less than, greater than, or equal to zero. In this listing, $size holds an integer value, while $temp holds a floating value.
- For non- numeric data, PHP offers the string data type , which can hold letters, number, and special characters. String values must be enclosed in either single quotes or double quotes. In the previous listing, $cat is string variable containing the vale ‘Siamese’.
- You may also encounter NULL data type , which is a “special“ data type first introduced in PHP4. NULLs are used to represent “empty” variable in PHP; a variable of type NULL is a variable without any data. In the preceding listing, $here is NULL.