Thursday 14 May 2009

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.
Numbersprint 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;
Using Different Kinds of NumbersInternally, 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.
Arithmetic OperatorsDoing 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.
Math operationsprint 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

More Learning

Join Free Now : PHP Tutorial

No comments:

Post a Comment