Free Programming E-Books
Free download ebooks on computer and programming

Free Ebook "PHP 5 Recipes: A Problem-Solution Approach" Sample Chapter

PHP 5 Recipes:A Problem-Solution
Free Download Chapter 3: Performing Math Operations
Download chapter

What you hold in your hands is the answer to all your PHP 5 needs. We have written PHP 5 Recipes with a number of purposes in mind.

First, this book is a source of instant solutions, including countless pieces of useful code that you can just copy and paste into your own applications, giving you answers fast and saving you hours of coding time.

Second, this book is a useful reference to the most important aspects of the PHP 5 language, including the vital functions you know and love from previous versions of PHP, as well as the functions introduced in PHP 5.

Finally, this book explains the new PHP 5 functionality in detail, including the vastly improved object-oriented capabilities and the new MySQLi database extension.

We are confident PHP 5 Recipes will be a useful and welcome companion throughout your PHP journey, keeping you on the cutting edge of PHP development, ahead of the competition, and giving you all the answers you need, when you need them.

< < prev next > >

Performing Math Operations

Math is one of the fundamental elements of most programming languages. Math allows the programmer to perform anything from simple additions to advanced calculations. Even though PHP was designed to create dynamic Hypertext Markup Language (HTML) documents, it has evolved to a general-purpose programming language that includes a strong and flexible math implementation.

The implementation of math in PHP looks very much like the implementation in C. In fact, many of the functions are implemented as simple wrappers around the math functions found in C libraries.

3-1.Numeric Data Types

Working with numbers or numeric data and math functions in PHP is simple. Basically, you have two data types to work with, floating point and integer. The internal representations for these values are the C data types double and int, and these data types follow the same set of rules as in C.

We've designed most the samples in this chapter to work with the command-line interface (CLI) version of PHP. If you use the samples with a web server and view the results in a browser, you may see different formatting than the results shown in this chapter. This is especially true if a recipe is using a variable-width font to present the data. In most cases, we show the generated output following the code sample. When the output generates HTML output, we will use a figure to display the result.

Note The minimum and maximum values for integer values depend on the system architecture where PHP is running. On a 32-bit operating system, an integer can be between -2,147,483,648 and 2,147,483,647.

PHP is a loosely typed scripting language where variables change the data type as needed by calculations. This allows the engine to perform type conversions on the fly. So, when numbers and strings are included in a calculation, the strings will be converted to a numeric value before the calculation is performed, and numeric values are converted to strings before they are concatenated with other strings. In the following example, a string and an integer value are added, and the result is an integer value.

The Code

<?php
// Example 3-1-1.php
$a="5";
$b= 7 + $a;
echo "7 + $a = $b";
?>


How It Works
The variable is assigned a string value of 5, and then the variable is assigned the value of the calculation of 7 plus the value of . The two values are of different types, so the engine will convert one of them so they are both the same type. The operator + indicates the addition of numeric values to the string, which is converted to a numeric value of 5 before the addition. The last line displays the calculation, and the result is as follows:

7 + 5 = 12

PHP will also convert the data types of one or more values in a calculation in order to perform the calculation correctly. In the following example, the float is converted to an integer before the binary and (&) operation is executed.