Understanding PHP Data Types
Variables can store data of different types, and different types of data can do different things. For example, you can add variables whose values are numbers (1 + 2), but adding variables whose values are characters (a + b) doesn’t make much sense.
You can store the following simple types of data in PHP variables:
- Integer: A whole number (no fractions), such as –43, 0, 1, 27, or 5438. The range of integers that is allowed varies, depending on your operating system, but in general, you can usually use any number from –2 billion up to +2 billion.
- Floating point number: A number (usually not a whole number) that includes decimal places, such as 5.24 or 123.456789. This is often called a real number or a float.
- Character string: A series of single characters, such as hello. There is no practical limit on the length of a string.
- Boolean: A TRUE or FALSE value.
Boolean data types represent two possible states — TRUE or FALSE. Boolean values are used mainly to compare conditions for use in conditional statements. For example, PHP evaluates an expression, such as $a > $b, and the outcome is either TRUE or FALSE.
PHP considers the following values FALSE :
- The string FALSE (can be upper- or lowercase)
- The integer 0
- The float 0.0
- An empty string
- The one-character string 0
- The constant NULL
Any other values in a Boolean variable are considered TRUE. If you echo a Boolean variable, the value FALSE displays as a blank string; the value TRUE echoes as a 1. Functions often return a Boolean variable that you can test to see whether the function succeeded or failed.
Assigning data types
Most other languages require that you initialize the variable before using it, specifying what type of data it can hold, but PHP is more informal. You don’t need to tell PHP which data type is in a variable. PHP evaluates the data when you assign it to the variable and then stores it as the appropriate type. Generally, this is helpful. PHP guesses the data type pretty accurately.
PHP also converts data when it needs to be converted. For example, if you have the following statements, PHP converts the data types with no problem:
$firstNumber = 1; # PHP stores it as an integer
$secondNumber = 1.1; # PHP stores it as a float
$sum = $firstNumber + $secondNumber;
Technically, the third statement is not possible because the data to be added are different types. However, PHP converts the integer to a float so that the addition proceeds smoothly. This happens automatically and invisibly and is very helpful.
On a rare occasion, PHP guesses badly when it stores the data. You might need to do something with a variable, and PHP won’t let you because the data is the wrong type. In such a case, you can specify how you want PHP to store the data, rather than let PHP decide for itself. This is called type casting. To specify a particular type, use a statement like one of the following:
$newint = (int) $var1;
$newfloat = (float) $var1;
$newstring = (string) $var1;
The value in the variable on the right side of the equal sign is stored in the variable on the left side as the specified type. So the value in $var1 is stored in $newint as an integer, as specified by (int).
Be careful when doing type casts. Sometimes you can get unexpected results. For example, when you cast a float into an integer, it loses its decimal places. To do this, PHP rounds the float toward 0. For example, if $number = 1.8 and you cast it into an integer — $newnumber = (int) $number — $newnumber will equal 1.
You can find out the data type of a variable by using a statement like the following:
For example, the following statement checks the data type of $checkvar:
The output from this statement is int(27), which tells you that $checkvar contains the integer 27.