Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.4     Brief introduction to variable types

This is NOT the latest copy of this book; click here for the latest version.

Variables in PHP can be of type integer (a whole number), floating-point (usually called "float"; a fractional number), string (a set of characters), array (a group of data), object (a complex mix of data and functionality), or a resource (any outside information, such as picture data). We will be looking at data types in more depth later on; for now, you only need to know what variables are and how they work.





<< 2.6.3 Heredoc   2.6.5 Code blocks >>
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
Safiya Dogara Bashir/sdbashir2001@yahoo.com - 07 Sep 2008

The floats are a subset of the rational numbers. A given rational number may or may not be exactly representable a float, depending on the base of the float, its length, and the rational number in question, so not all rational numbers are floats - in other words, the binary floats are a different set from the decimal floats, and so on. It is correct to refer to floats as fractional numbers, although slightly misleading since that description could be understood to mean "magnitude smaller than unity."

Examples:
1/5 can be represented exactly as a decimal fraction in only one digit [0.2], but it has no exact base 2 fraction representation of any length. It is a rational number, in base 2 [1/101(2)] or any other base, but it is a nonterminating fraction in base 2.

1/3 is a nonterminating fraction in base 2 or base 10 but it is 0.1(3).

10^(1/2), pi and e are irrational numbers and cannot be exactly represented by any finite-length number in any base.

Please note that in most languages floats (also called "reals" in some languages) are stored in base 2, not base 10. (BASIC and COBOL and perhaps others offer "currency" or "BCD" data types in which numbers are stored as either fixed or floating point decimals, however.) I haven't read far enough yet to know how PHP stores its floats, but if it is compatible with C then floats are stored in base 2 - this is the usual choice since decimal representations tend to inflict a considerable performance hit on commonly-available computer hardware.

A PHP learner - 07 Sep 2008

The floats are a subset of the rational numbers. A given rational number may or may not be exactly representable a float, depending on the base of the float, its length, and the rational number in question, so not all rational numbers are floats - in other words, the binary floats are a different set from the decimal floats, and so on. It is correct to refer to floats as fractional numbers, although slightly misleading since that description could be understood to mean "magnitude smaller than unity."

Examples:
1/5 can be represented exactly as a decimal fraction in only one digit [0.2], but it has no exact base 2 fraction representation of any length. It is a rational number, in base 2 [1/101(2)] or any other base, but it is a nonterminating fraction in base 2.

1/3 is a nonterminating fraction in base 2 or base 10 but it is 0.1(3).

10^(1/2), pi and e are irrational numbers and cannot be exactly represented by any finite-length number in any base.

Please note that in most languages floats (also called "reals" in some languages) are stored in base 2, not base 10. (BASIC and COBOL and perhaps others offer "currency" or "BCD" data types in which numbers are stored as either fixed or floating point decimals, however.) I haven't read far enough yet to know how PHP stores its floats, but if it is compatible with C then floats are stored in base 2 - this is the usual choice since decimal representations tend to inflict a considerable performance hit on commonly-available computer hardware.

Rik - 07 Sep 2008

This is wrt Kyle's comment. A rational number is one that could be expressed as p/q where p and q are integers. So, the two number representation is in fact more appropriate for rational numbers than for fractions in general.

Floats are rationals, because they are a number, divided by a power of the base. However, not all rationals are representable as floats. 1/3 is a rational number. It cannot be represented as a float, because it translates to an infinitely recurring decimal representation.

So, if you consider defining floats as fractions to be innacurate, then so defining them as rationals. Neither set is completely covered by floats.

Kyle - 07 Sep 2008

Technically, I'd consider a "float" to be more of a rational number, rather than a fractional number. It can have a floating point decimal in it, and it is stored as a large integer with axponential of ten, e.g. 314159265e-8. I'd imagine fractions as being two integers stored, and the computer would have to compute it each time it used it (5 and 8 make 5/8, which computes to .625). Fractional numbers cannot always be accurately converted into floats, though, as 1/3 does not have a displayable decimal equivallent.

A PHP User - 07 Sep 2008

What about the "boolean" / "bool" variable type?
Dirk



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 nine plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow