Working With Data
PHP Data Types
A typical PHP script works on various types of data such as strings, integers, and Booleans. This classification is used to represent how that data can be manipulated. PHP has native support for both scalar data-types such as integers, strings, and Boolean and compound data-types like arrays and objects. PHP also supports two special data-types called resource and null, we’ve mentioned them here for the sake of completeness, but we’ll cover these data-types along with the array and object types in there respective tutorials.
Integers and Floats
Scalar data-types are the simplest type to implement since they can only contain and represent a single entity of data. The integer, float, string, and Boolean data-types are classified as scalar. Integers represent literal whole numbers which can be positive or negative that do not contain a decimal point. Floating-point or double data-types on the other hand are used to represent real numbers with decimal points. Lets look at some examples:
<?php /* Examples of integers */ $a = 4; $b = 1212; $c = -53; /* Examples of floats */ $total = 29.99; $avg = 4.1245; $num = 9.5E+5; //$num = 950000 ?>
The minimum and maximum ranges for integer and floating-point values are machine dependent, thus the range can differ from one platform to another. Floating-point data-types can also be expressed in scientific notation, as in the example above.
Boolean and Strings
The Boolean data-type is used to represent logic within a script. A Boolean variable can only store either the TRUE or FALSE literal values. A Boolean data-type by itself is insignificant, but when used in conditional statements or expressions, they can add decision-making functionality to your scripts.
A string is simply a series of characters of any length enclosed by either single or double quotation marks. You’ll spend the majority of your time manipulating strings in a PHP script, so it is essential that you have a basic understanding on how strings are handled in PHP. Single (‘) and double (“) quotes are most commonly used to delimit and group strings, but PHP also allows a third method of delimiting strings using the here-doc syntax, we’ll explain this in detail in the strings tutorial.
Below are some examples of Boolean and string data-types:
<?php /* Examples of Booleans */ $start = TRUE; $is_ready = FALSE; $run = 1; //same as true $run = 0; //same as false /* Examples of strings */ $name = "Cookie Monster"; $snack = 'cookies!!!'; $char = "h3110-<>0r\d"; ?>
Data-type Conversions
PHP has an internal mechanism for implicitly converting variable data-types depending on the context of how that variable is used in an expression, called type juggling. This can be time-saving in some circumstances, but it should be avoided to prevent confusion.Lets expand on this in the following examples:
<?php /* example 1 */ $num = "2"; $total = 5 + $num; //$total = 7 /* example 2 */ $a = "3 cookies"; $b = $a - 1; //$b = 2 /*example 3 */ $sum = "2.5e2"; $var = $sum - 5; //$var = 245 ?>
In the first example the value from the expression 5 + $num is assigned to the $total variable. Even though $num is of a string data-type, PHP automatically converts it to an integer to satisfy the arithmetic expression. PHP operators have different operand requirements to functional properly, but PHP follows some basic rules to convert between data-types when necessary.
If a string begins with a numerical value, that value is used in the conversion. Note in example 2, the $a variable begins with the digit 3 and PHP retains this value after the conversion. A string will be evaluated as a float if it contains a period (.), or upper -lowercase e as in the third example.
In addition to type juggling, you can explicitly convert the value of a variable from one data-type to another, known as type casting. Simply prepend the data-type in parentheses before the variable that you want to convert. Below is a table that shows the casts allowed.
| Type Casting | |
|---|---|
| (string) | String |
| (array) | Array |
| (bool) or (boolean) | Boolean |
| (int) or (integer) | Integer |
| (float), (real), or (double) | Float |
| (object) | Object |
Explicitly type casting your variables will allow you or someone else reading your code to logically follow your scripts without the overhead of trying to figure out your intentions.
