Data: Working with Numbers in PHP

Numbers in PHP are expressed using familiar notation, although you can’t use com­mas or any other characters to group thousands. You don’t have to do anything spe­cial to use a number with a decimal part as compared to an integer. Example 2-15 prints 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;

1. Using Different Kinds of Numbers

Internally, the PHP engine 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 dec­imal point can “float” around to represent different amounts of precision.

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

One distinction between the PHP engine’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. “Building Complicated Decisions” on page 43 explains comparisons and shows how to properly compare floating-point numbers.

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 173.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 sup­ports two asterisks (**) for exponentiation and the percent sign (%) for modulus divi­sion (returning 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 opera­tor is useful for printing rows whose CSS class names alternate in an HTML table, as shown in Example 4-13.

The exponentiation operator was introduced in PHP 5.6. If you’re using an older version of PHP, use the pow() function.

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 engine 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 engine 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. You can avoid the need to memorize or repeatedly refer to this table, however, with a healthy dose of parentheses. Grouping operations inside parentheses unambiguously tells the PHP engine 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 in 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 inte­ger that makes it larger than the maximum allowable integer or smaller than the min­imum possible integer, the PHP engine converts the result into a floating-point number so you get the proper result for your calculation.

Source: Sklar David (2020), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *