Previous Section  < Day Day Up >  Next Section

2.2 Numbers

Numbers in PHP are expressed using familiar notation, although you can't use commas or any other characters to group thousands. You don't have to do anything special to use a number with a decimal part as compared to an integer. Example 2-15 lists some valid numbers in PHP.

Example 2-15. Numbers
print 56;

print 56.3;

print 56.30;

print 0.774422;

print 16777.216;

print 0;

print -213;

print 1298317;

print -9912111;

print -12.52222;

print 0.00;

2.2.1 Using Different Kinds of Numbers

Internally, the PHP interpreter makes a distinction between numbers with a decimal part and those without one. The former are called floating-point numbers and the latter are called integers. Floating-point numbers take their name from the fact that the decimal point can "float" around to represent different amounts of precision.

The PHP interpreter uses the math facilities of your operating system to represent numbers so the largest and smallest numbers you can use, as well as the number of decimal places you can have in a floating-point number, vary on different systems.

One distinction between the PHP interpreter's internal representation of integers and floating-point numbers is the exactness of how they're stored. The integer 47 is stored as exactly 47. The floating-point number 46.3 could be stored as 46.2999999. This affects the correct technique of how to compare numbers. Section 3.3 explains comparisons and shows how to properly compare floating-point numbers.

2.2.2 Arithmetic Operators

Doing math in PHP is a lot like doing math in elementary school, except it's much faster. Some basic operations between numbers are shown in Example 2-16.

Example 2-16. Math operations
print 2 + 2;

print 17 - 3.5;

print 10 / 3;

print 6 * 9;

The output of Example 2-16 is:

4

13.5

3.3333333333333

54

In addition to the plus sign (+) for addition, the minus sign (-) for subtraction, the forward slash (/) for division, and the asterisk (*) for multiplication, PHP also supports the percent sign (%) for modulus division. This returns the remainder of a division operation:

print 17 % 3;

This prints:

2

Since 17 divided by 3 is 5 with a remainder of 2, 17 % 3 equals 2. The modulus operator is most useful for printing rows whose colors alternate in an HTML table, as shown in Example 4-12.

The arithmetic operators, as well as the other PHP operators that you'll meet later in the book, fit into a strict precedence of operations. This is how the PHP interpreter decides in what order to do calculations if they are written ambiguously. For example, "3 + 4 * 2" could mean "add 3 and 4 and then multiply the result by 2," which results in 14. Or, it could mean "add 3 to the product of 4 and 2," which results in 11. In PHP (as well as the math world in general), multiplication has a higher precedence than addition, so the second interpretation is correct. First, the PHP interpreter multiplies 4 and 2, and then it adds 3 to the result.

The precedence table of all PHP operators is part of the online PHP Manual at http://www.php.net/language.operators#language.operators.precedence. You can avoid memorizing or repeatedly referring to this table, however, with a healthy dose of parentheses. Grouping operations inside parentheses unambiguously tells the PHP interpreter to do what's inside the parentheses first. The expression "(3 + 4) * 2" means "add 3 and 4 and then multiply the result by 2." The expression "3 + (4 * 2)" means "multiply 4 and 2 and then add 3 to the result."

Like other modern programming languages, you don't have to do anything special to ensure that the results of your calculations are properly represented as integers or floating-point numbers. Dividing one integer by another produces a floating-point result if the two integers don't divide evenly. Similarly, if you do something to an integer that makes it larger than the maximum allowable integer or smaller than the minimum possible integer, the PHP interpreter converts the result into a floating-point number so you get the proper result for your calculation.

    Previous Section  < Day Day Up >  Next Section