PHP supports many different data types, but they are generally divided in two categories:
scalar and composite.
A scalar value contains only one value at a time. PHP supports four scalar types:
boolean A value that can only either be true or false int A signed numeric integer value float A signed floating-point value string A collection of binary data
PHP recognizes two types of numbers, integers and floating-point values. The int data type is used to represent signed integers (meaning that both positive and negative numbers can be expressed with it). Numbers can be declared using several different notations:
Decimal 10; -11; 1452 Standard decimal notation. Note that no thousand separator is needed—or, indeed, allowed.
Octal 0666, 0100 Octal notation—identified by its leading zero and used mainly to express UNIX-style access permissions.
Hexadecimal 0x123; 0XFF; -0x100 Base-16 notation; note that the hexadecimal digits and the leading 0x prefix are both case-insensitive.
Floating-point numbers (also called floats and, sometimes, doubles) are numbers that have a fractional component; like integers, they are also signed. PHP supports two different notations for expressing them:
Decimal 0.12; 1234.43; -.123 Traditional decimal notation.
Exponential 2E7, 1.2e2 Exponential notation—a set of significant digits (also called the mantissa), followed by the case-insensitive letter E and by an exponent. The resulting number is expressed multiplied by ten to the power of the exponent—for example, 1e2 equals 100.
There are a few important gotchas that you need to be aware of when dealing with numbers. First of all, the precision and range of both types varies depending on the platform on which your scripts run.
For example, 64-bit platforms may, depending on how PHP was compiled, be capable of representing a wider range of integer numbers than 32-bit platforms. What’s worse, PHP doesn’t track overflows, so that the result of a seemingly innocuous operation like an addition can have catastrophic consequences on the reliability of your application.
Most importantly, you need to be aware that the float data type is not always capable of representing numbers in the way you expect it to. Consider, for example this very simple statement:
echo (int) ((0.1 + 0.7) * 10);
You would expect that the expression ((0.1 + 0.7) * 10) would evaluate to 8 (and, in fact, if you print it out without the integer conversion, it does). However, the statement above outputs 7 instead. This happens because the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8; when the value is converted to int, PHP simply truncates away the fractional part, resulting in a rather significant error (12.5%, to be exact).
The lesson that you need to take home from all this is simple: know the limitations of your numeric data types, and plan around them. Whenever the precision of your calculation is a relevant factor to the proper functioning of your application, you should consider using a the arbitrary precision functions provided by the BCMath extension (you can search for it in your copy of the PHP manual) instead of PHP’s built-in data types.
In the minds of many programmers, strings are equivalent to text. While in some languages this is, indeed, the case, in many others (including PHP), this would be a very limiting—and, in some cases, incorrect—description of this data type. Strings are, in fact, ordered collections of binary data—this could be text, but it could also be the contents of an image file, a spreadsheet, or even a music recording.
PHP provides a vast array of functionality for dealing with strings.
A Boolean datum can only contain two values: true or false. Generally speaking, Booleans are used as the basis for logical operations.
When converting data to and from the Boolean type, several special rules apply:
• A number (either integer or floating-point) converted into a Boolean becomes false if the original value is zero, and true otherwise.
• A string is converted to false only if it is empty or if it contains the single character 0. If it contains any other data—even multiple zeros—it is converted to true.
• When converted to a number or a string, a Boolean becomes 1 if it is true, and 0 otherwise.
Compound Data Types
In addition to the scalar data type that we have just examined, PHP supports two compound data types—so called because they are essentially containers of other data:
• Arrays are containers of ordered data elements; an array can be used to store and retrieve any other data type, including numbers, Boolean values, strings, objects and even other arrays.
• Objects are containers of both data and code. They form the basis of Object-oriented Programming.
Other Data Types
In addition to the data types that we have seen so far, PHP defines a few additional types that are used in special situations:
• NULL indicates that a variable has no value. A variable is considered to be NULL if it has been assigned the special value NULL, or if it has not yet been assigned a value at all—although in the latter case PHP may output a warning if you attempt to use the variable in an expression.
• The resource data type is used to indicate external resources that are not used natively by PHP, but that have meaning in the context of a special operation— such as, for example, handling files or manipulating images.
Converting Between Data Types
As we mentioned, PHP takes care of converting between data types transparently when a datum is used in an expression. However, it is still possible to force the conversion of a value to a specific type using type conversion operators. These are simply the names of the data type you want to convert to enclosed in brackets and placed before an expression. For example:
<?php $x = 10.88; echo (int) $x; // Outputs 10 ?>