PHP DevCenter
oreilly.comSafari Books Online.Conferences.


PHP Foundations

Variable Manipulation and Output


This article will conclude our discussion of variables in PHP by presenting the numerous ways that atomic PHP variables can be manipulated and accessed within PHP scripts. We'll also look at the echo command and use it to create our first PHP script that truly outputs dynamic content to the user.

Basic manipulations

Now that we have a better idea of the types of variables in PHP and are familiar with how to use them, we will begin discussing how these variables can interact and be manipulated. As with our introduction to the types of variables in PHP, we will begin with the mathematical operators.

Basic mathematics in PHP
To begin our introduction into variable manipulation, we will start with the basic mathematical abilities of PHP using standard integer variables. For the most part, the syntax and logic in this section is straightforward and will only be covered briefly to clarify any confusion that may exist. Consider the following:

     $number = 10;
     $foo = 2;

     $answer = $number + $foo;

This is an example of a basic mathematical operation between two integers in PHP. As expected, the variable $number is added to the variable $foo with the sum of these two numbers (10 + 2 = 12) being stored in the variable $answer. Now, what if we wanted to subtract $answer by another number such as 5 (assume $answer is 12)?

<?    // Assume $answer exists and is equal to
      // $number + $foo (10 + 2 = 12)

      $answer = $answer - 5;

In this example, we are taking our previous value $answer, and subtracting it by the constant value 5 and then storing the resulting number back into the variable $answer. For those who have no programming experience, this example can be quite confusing! How can you take a variable, subtract it by another number and store the answer to that variable into the same variable you just used in your subtraction? The answer is that PHP will always evaluate a statement before any assignments are made -- that is, PHP will use the old value of $answer to do the mathematics before replacing it with the new value. Therefore, the value of answer should be 12 - 5 = 7, and it is!

Simplified syntax
The situation presented above -- where a variable is used in a mathematical statement and the result of the statement is stored back into the variable used originally -- is very common. Usually, the mathematical tasks performed in an application are very simple and, to make such tasks easier, the following statements are all allowed in PHP:


     // Add 1 to the value of $answer
     // Subtract 1 from the value of $answer

     // Add 5 to the value of $answer
     $answer += 5;


PHP also allows the use of parentheses within mathematical statements. An example:


     // Performing Multiple Operations with Parentheses

     $answer = (5*(4+2))/2

     // Answer =  ((4 + 2) * 5)/2 = 30
     //        =  30/2 = 15


Floating-point variables
As mentioned in my previous article, floating-point numbers may not always store and return the values as expected. For example, a floating-point statement that evaluates to the value 7.99999999 may be perceived as the value 7 on some systems when converted to an integer rather than the expected value of 8. For more information regarding the particulars of this on your specific system consult the PHP manual. Beyond the special consideration that must be taken when dealing with floating-point numbers, they conform to all of the same mathematical syntax as their integer counterparts.

String manipulation

When dealing with strings, it is often necessary to manipulate them in a number of ways. PHP provides a small army of string manipulation functions that provide tools for nearly every circumstance. (A list of these functions can be found online at the PHP web site.) Although the majority of string manipulation is done through function calls, there is a syntax that is worth discussion. When dealing with strings, it is sometimes useful if not necessary to take two strings and combine them head-to-tail into a single string. To do this, we use the period operator (".") to combine strings just as we used a math operator (such as addition). Example:


     // Assign $foo a string value
     $foo = "Hello, my name is: ";
     // Assign $name another String Value
     $name = "John";

     // Attach $name to the end of $foo and store in 
     // the variable $message
     $message = $foo . $name;


Also note that the period operator can be used in a syntax similar to the one illustrated in the integer examples:

     // Assign $bar a value
     $bar = "Hello,";

     // Add on to $bar
     $bar .= " my ";
     $bar .= " name ";
     $bar .= " is ";
     $bar .= " John ";


As expected, the resulting value of $bar will be "Hello, my name is John".

Pages: 1, 2

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career


Enroll today!

Sponsored by: