Variable ManipulationNow that you have been introduced to the basic PHP data types, let's explore how these data types can be manipulated to perform calculations and more using PHP. As you would expect, PHP supports all the basic mathematical operations of any programming language, including addition and multiplication as well as a wide range of trigonometric and logarithmic functions. Beyond mathematical manipulations, PHP supports an even greater amount of string manipulation functions. This chapter will cover only the more fundamental variable manipulations used in both strings and mathematics. Performing mathematical operations in PHP is a fairly intuitive task. PHP supports all the common mathematical standards for operator precedence, groupings, and so on for both integers and floating-point numbers. For instance, performing simple mathematics in PHP can be accomplished as follows (see Listing 1.8):
Listing 1.8. Simple Mathematics with PHP Variables<?php $answer = 5 + 4; /* $answer now equals 9 */ $answer = $answer - 5; /* $answer now equals 4 */ $answer = $answer / 2; /* $answer now equals 2 */ $answer = 1/3; /* $answer is now 0.333333 */ $answer = ((5 + 4)*2) % 7; /* $answer now equals 4 */ ?> An important consideration that must be made when dealing with floating point numbers is how they are handled when PHP converts them to an integer value. For instance, a value of 0.999999, when converted to an integer, may translate to 0, whereas on other systems it may translate to 1 as you would expect. This difference in behavior is the result of the system that PHP is running on, not PHP itself. For more information on this subject, including a description of how your particular system is affected, consult the PHP documentation. As with most other C-style programming languages, PHP also supports a form of shorthand. In the preceding example, consider the second and third lines in which the result of a mathematical operation was stored in the same variable the operation was performed on. Instead of using the preceding syntax, you can save time by placing the desired operation next to the equal sign in the following fashion, as shown in Listing 1.9: Listing 1.9. Shorthand Mathematics in PHP<?php $answer = 5; /* Assign Original value */ $answer += 2; /* Equivalent to: $answer = $answer + 2; */ $answer *= 2; /* Answer is now 14 */ $answer %= 5; /* Answer is now 4 */ ?> To make your life even simpler, you can increment or decrement a variable by 1 by following the example shown in Listing 1.10: Listing 1.10. Shorthand Incrementing/Decrementing of Variables<?php $answer++; /* Increment $answer by 1 */ $answer--; /* Decrement $answer by 1 */ ++$answer; /* Increment by 1, (see note) */ ?> Along with simple mathematics, PHP also supports trigonometric and logarithmic operations for advanced calculations in the following fashion: <?php $cos = cos(2 * M_PI); /* cos of 2*PI is 1 */ ?>
As stated earlier, the majority of string-manipulation techniques available to PHP are discussed later in this chapter. However, one string-specific operation does exist in PHP that should be discussed nowthe string concatenation operator. This operator is represented by a period (.) character and is used to combine two separate variables (usually strings) into a single string, as shown next: <?php $string = "Thank you for buying "; $newstring = $string . "my book!"; ?> $newstring now contains the string "Thank you for buying my book!". To save time, this operator can also be used in its shorthand form like the mathematical operators described earlier. |