3.1 Types of DataThis is NOT the latest copy of this book; click here for the latest version.
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.
Author's Note: You should be aware that floating-point numbers are complicated beasts, and are unpredictable. The classic example is the sum "(0.1+0.7)*10" converted to an integer. You might think, "that's easy - it's 8", but PHP will report 7. The reason for this is because inherent inconsistencies in floating-point numbers mean that the value will actually be 7.9999999999999991. Usually this is not a problem, because rounding that value even to 10 decimal places gives you 8, however it does mean that you should avoid comparing floating-point numbers wherever possible.
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:
<?php
$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 an object, 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:
<?php
$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 see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|