PHP Lesson 2: Data types

Today’s PHP lesson is about data types. Although PHP can work with many different data types, we divide them in two different categories: scalar data and composite data.

photo-2015-05-19-12-56-17_1896Scalar data

Scalar data can hold only one value at a time. PHP knows four different types of scalar data:
Boolean: possible values are TRUE or FALSE
int: can be any signed (so both positive and negative values are possible) numeric integer value
float: can be any signed (so both positive and negative values are possible) floating-point value
string: can be any collection of binary data

Numeric values
Numeric values can be both of the type integer as floating point, and can be annotated in decimal, octal (identified by leading zero) or hexadecimal (identified by leading 0x), and for floating-popint numbers, they can be annotated in decimal and exponential (2E7, 2.3E5).
A very important thing to note when working with numbers i s that the precision and the range of your data types depends on the machine you are working on. For instance, & 64-bit system will have a wider range than a 32-bit system. Also, PHP doesn’t track overflows. So stuffing your variable with too much data will NOT throw an error, but will result in a truncated value.
Another thing to keep in mind is that floats are not always storing their value the way you would think. For example, if you execute the statement

integervar (int) (0.2 + 0.4) * 10);
print integervar;

The printed result will not be 6 as you would expect, but it will be 5. This is because the result of 0.2 and 0.4 is not stored as 0.6 in the float, but as 0.59999999, and times ten, this gives 5.999999
When this float is conversed to an int, it simply truncates everyting behind the fraction, resulting in 5.
These things are not necessarily a disaster, but are limitations you should be aware of when doing calculations in your PHP code.

When talking about strings, most programmers immediately think of text. This is indeed the case. In some languages. Not in PHP, oh no. In PHP, a string is an ordered collection of binary data. This data can be text, but can also be an image, an office document, or a mp3-file.
Due to the extended character of strings, and all that is possible with them in PHP, we will get back on them later.

Booleans are the easiest of all variables, as they can only hold one of two values: true or false, and are therefor mostly used in logical operations.
There are some important things to know about booleans and conversions though:
When you convert any number (no matter if it is an integer or a float) to a boolean, the boolean will always be true, except when the value of the number is zero.
When you convert a string to a boolean, the boolean will always be true, except when the string is empty (null), or contains a single digit 0 (zero). When there are multiple zeros in the string (00000), the boolean will convert to true.
When you convert a boolean to a string, integer or float, the value will be 1 if the boolean was true, and 0 if the boolean is false.

Composite Data

PHP doesn’t only support the scalar data types we discussed above, but also supports two compound or composite datatypes. The name compound or somposite comes from the fact that they are in essence containers of other data.

Arrays contain data of other datatypes in an ordered way. Arrays can contain any of the abovementioned scalar data. We will digg deeper into arrays in a future lesson.

Objects contain both data and code. Objects are the cornerstones of Object Oriented Programming (OOP) (who would’ve thought, right?), and are also dugg into in a future lesson.

Special Data Types

Besides the two data types we mentioned above, there are also two very special data types:

Null indicates that a variable has no value. A variable can get the NULL value by getting it specifically assigned, or by not yet having been assigned any value at all.

Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)Resource
The resource data type is used when dealing with external resources that are not part of PHP, but are used inside your code, for example a file.

Converting between data types

PHP is very easy when it comes to converting data types, as it does all the work for you, for free! However, if you really want to convert a variable to another data type yourself, you can do this by putting the name of the data type you want to convert to between parentheses, and place it in front of the expression or variable you want to convert. Check it out, it was already there in the example we used before:

integervar (int) (0.2 + 0.4) * 10);

Leave a Comment

%d bloggers like this: