Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.1     Types of Data

This 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.





<< 3 Simple variables and operators   3.2 Checking a variable is set: isset() >>
Table of Contents
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!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
patilvardhaman@rediff.com - 30 Aug 2008

hi i am vardhaman from pune, working as php developer.

A PHP User - 30 Aug 2008

SunOS petcsm03 5.9 Generic_117171-15 sun4u sparc sun4u


[root@petcsm03:/usr/local/www/tools/colin]# ./second.php
./second.php: ?php: No such file or directory
./second.php: =: command not found
./second.php: =: command not found
./second.php: print: command not found
./second.php: line 5: syntax error near unexpected token `?>'
./second.php: line 5: `?> '

stud1@law.uct.ac.za - 30 Aug 2008

i have never done programming before. is PHP the right place to start? i am determined to know how to program and create my own website.

amol_bhavsar1982@rediffmail.com - 30 Aug 2008

Hello,I live in Pune ,I m from Maharashtra.

amol_bhavsar1982pune@indiatimes.com - 30 Aug 2008

Hello!

Karsten - 30 Aug 2008

I think you mean weak (and strong) typing, not loosely.

mike - 30 Aug 2008

just spent a while putting (0). kept getting error.

Then brainwave and tried {0} ---yep curly brackets rule!
(must get my eyes tested)
Worth pointing this out though for us oldies

cooley.rusty@gmail.com - 30 Aug 2008

To BruceG

try like this :
$myinteger="20";

visit my site :
http://hyp3r.myboard.info

cooley.rusty@gmail.com - 30 Aug 2008

<?php
$mystring="12";
$myinteger=20;
print $mystring + $myinteger;
?>

This is working properly . Only thing you should do is to configure your apache or server you'r on .

And answer on this :

I just ran the test script from above :

<?php $mystring="12";
$myinteger=20;
print $mystring + $myinteger;
?>

on a mac (osx 10.4), and I get "Parse error: parse error, unexpected T_VARIABLE" referring to the first line. Not sure why??

MAN ARE YOU BLIND ??? T_VARIABLE" referring to the first line. >>>>>>>>>>> <?php $mystring="12"; it should be like :
<?php
$mystring="12";

Dvorak user - 30 Aug 2008

These QWERTY users are screwing up this board.

Bruce G - 30 Aug 2008

Parse error: parse error, unexpected T_VARIABLE in /hsphere/local/home/bruceg/inspired-evolution.com/PHP_Test.php on line 12

<?php
    $mystring = "12";
    $myinteger = 20;
    print $mystring + $myinteger;
?>
I am on a Mac also. Looks like another person had this problem. What is the fix???

webmaster@dezinepros.com - 30 Aug 2008

Ah...I got it. forgot a curly brace. !oo!

webmaster@dezinepros.com - 30 Aug 2008

I just ran the test script from above :

<?php $mystring="12";
$myinteger=20;
print $mystring + $myinteger;
?>

on a mac (osx 10.4), and I get "Parse error: parse error, unexpected T_VARIABLE" referring to the first line. Not sure why??

A PHP User - 30 Aug 2008

asdfasdfradasdfsdfaasdfaasdasdfadsf

A PHP User - 30 Aug 2008

asdfadsf

sugaralien@gmail.com - 30 Aug 2008

If you do not want the string to be changed into an integer, then you would put quotation mark around the string as follows:

<?php
$mystring = "12";
$myinteger = 20;
print "$mystring" + $myinteger;
?>

A PHP User - 30 Aug 2008

,mnn;l;lkj;

A PHP User - 30 Aug 2008

"How would you handle it if you wanted to print the string and then the integer without having PHP change the string to an integer?"

I dunno im a newb too, perhaps like in visual basic and use the & instead

A PHP User - 30 Aug 2008

Replace the plus sign with a period. That would print them one right after the other with no formatting and no spaces.

A PHP User - 30 Aug 2008

How would you handle it if you wanted to print the string and then the integer without having PHP change the string to an integer?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow