Types of Data

PHP has seven types of variables, and all but one hold a specific class of information. The seven types are: strings, integers, floats, booleans, arrays, objects, and resources. You'll be using them all throughout this book, so it is worthwhile remembering what each do.

Strings hold characters (literally: a string of characters) such as "a", "abc", "Jack and Jill went up the hill to fetch a pail of water", etc. Strings can be as short or as long as you want - there's no limit to size.

Integers hold whole numbers, either positive or negative, such as 1, -20, 55028932, etc. There is a maximum limit to the size of integers - any numbers lower than -2147483647 and any numbers higher than 2147483647 are automatically converted to floats, which can hold a much larger range of values.

Floats hold fractional numbers as well as very high integer numbers, such as 4.2, 1.00000001, and 2147483647000.

Booleans simply hold true or false. Booleans are, in fact, just integers behind the scenes - PHP considers the number 0 to be false, and everything else to be true.

Arrays are a special variable type in that they hold multiple values. Arrays can be quite complicated, and so are covered in detail in their own chapter.

Objects are complex variables that have multiple values, but also their own functions. Objects are also very complicated, and, like arrays, are covered in their own chapter.

Resources are anything that is not PHP data - this might be picture data you have loaded from a file, the result of an SQL query, etc. Resources are used like any other variable, with the key difference that you should generally remember to free up your resources when you are finished with them.

Most data types are freely convertible to most other data types, which makes PHP loosely typed . Here's a piece of code that should illustrate the point nicely:

    $mystring = "12";
    $myinteger = 20;
    print $mystring + $myinteger;

That script will output 32, despite the fact that the first variable, $mystring, is a string, whereas the other is an integer. PHP will automatically attempt to convert the non-integer operand, $mystring, into an integer, and will find that it is in fact an integer inside a string. If it tries to convert a string like "wombat" to an integer, PHP will simply return the value 0.

Author's Note: "Operand" is one of those terms you learn at school and forget the minute you leave. An operand is something, in this case $mystring, upon which a mathematical function, in this case +, is being performed. So in $mystring + $myinteger, + is the operator, $mystring and $myinteger are the operands.

Strings have one special usage that set them apart a little bit from the other variable types, and that is {x} notation. As a string is just a collection of characters, it is sometimes possible that you may want to read or write just one character. Take a look at this example:

    $mystr = "Jello, world?";
    $mystr{0} = "H";
    $mystr{12} = "!";
    print $mystr;

Starting off with a broken string, we change the first character (letter 0) to H, then the twelfth character to an exclamation mark, forming "Hello, world!". Later on we'll look at a function, strlen(), that returns the size of a string - you can use this instead of numbers so that rather than 12 we could use this:

$mystr{strlen($mystr)-1} = "!";

The -1 is necessary because strlen() returns the length of the string, which is always one more than the position of the last digit of the string in PHP because the first character starts at 0. That is, a string of length 13, as above, will have its last character at position 12. More on that later.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Checking a variable is set >>

Previous chapter: Simple variables and operators

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.