Chapter 9. Learning PHP Number Handling
If you need to do serious numerical, scientific, or statistical computation, a web-scripting language is probably not where you want to be doing it. With that said, however, PHP does offer a generous array of functions that nicely cover most of the mathematical tasks that arise in web scripting. It also offers some more advanced capabilities such as arbitrary-precision arithmetic and access to hashing and cryptographic libraries.
The PHP designers have, quite sensibly, not tried to reinvent any wheels in this department. Instead, they found about 18 perfectly good wheels by the side of the road and built a lightweight fiberglass chassis to connect them all together. Many of the more basic math functions in PHP are simple wrappers around their C counterparts (for more on this, see the sidebar "A Glimpse behind the Curtain" in Chapter 27, which will cover PHP's mathematics capabilities in greater detail).
Numerical Types
PHP has only two numerical types: integer (also known as long), and double (aka float), which correspond to the largest numerical types in the C language. PHP does automatic conversion of numerical types, so they can be freely intermixed in numerical expressions, and the "right thing" will typically happen. PHP also converts strings to numbers where necessary.
In situations where you want a value to be interpreted as a particular numerical type, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access