2.0 Introduction


PHP is a loosely typed language. This means that a variable's type does not need to be declared at runtime and the variable's type can change if needed. This makes it much easier for you to do math with PHP than with a traditional language such as C, where you have to declare your variable's type at runtime, such as

 int i; 

This declaration would limit the value of i to an integer. So, if i was assigned 2.45 , it would be truncated down to 2 and in addition, the compiler would issue a nasty warning.

In PHP, however, numbers are automatically converted for you. Let us take the earlier example: If we had a value of a variable $k equal to 2.45 , then because the number 2.45 is a float, $k would be a float automatically! Or, if $k now equaled 15 , the type of $k would be converted to an integer automatically!

Although PHP does automatic type conversions for you, you can optionally cast a value to a certain type for added control. Here is a quick example:

 $k = (int)2.125; 

Here we force $k to be of type integer by using the ( type ) notation. You can also use settype() function to achieve the same goal.

This chapter will go over many of the important concepts related to PHP and numbers, such as

  • Working with arbitrary-precision numbers

  • Generating random numbers (very useful)

  • Working with trigonometry

  • Validating credit card numbers



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net