Published on (
 See this if you're having trouble printing code examples

PHP Foundations

Variable Manipulation and Output


Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

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".

Outputting to the browser

One of the most fundamental aspects of web development in PHP is outputting to the browser. Although in a technical sense you are outputting to the browser simply by sending them the content of a web page, when we discuss output we will focus on sending variables that we construct and manipulate in PHP to the browser dynamically.

Basic output with echo
The first function we will discuss, echo, is the most basic output function available to PHP. With echo, you can send any data to the web browser (variable or constant). Its syntax follows:

echo <variable or constant>;

In example:

<title>My first PHP page</title>


     $my_msg = "This is my first PHP output!<br />";
     $my_var = "Hello, PHP!<br /><br />";
     $msg = $my_msg.$my_var;

     echo $msg;

     $my_var = "Goodbye, PHP!";

     echo $my_var;



This example pulls together a number of the concepts that we have been talking about in the past articles. Above, we have a PHP script embedded in a HTML document. In this PHP script, we are taking two variables ($my_msg and $my_var) and assigning them values. These two variables are then combined into a single variable and stored into $msg which is then output to the browser using the echo function. Then the value of $my_var is changed and a second output is made to the browser showing its new value. The PHP script then terminates, returning the server to outputting straight HTML. The resulting output is equivalent to the following static HTML page:

<title>My first PHP page</title>
This is my first PHP output!<br />
Hello, PHP!<br /><br />
Goodbye, PHP!

Which of course outputs to the browser as:

    This is my first PHP output!
    Hello, PHP!

    Goodbye, PHP!

Notice that in our PHP code to output the text we included the HTML tag <br /> where we wanted a line break within the PHP variable. Without such HTML tags within our variables, the output would have looked something like this:

This is my first PHP output!Hello, PHP!Goodbye, PHP!

Simplified variable output

Although the echo function is the most basic function used to output to the browser, there is an easier way to output single variables in PHP. The best way to illustrate this is by rewriting our original example:

<title>My first PHP page</title>
   $my_msg = "This is my first PHP output!<BR>";
   $my_var = "Hello, PHP!<br><br>";

<?=$my_msg?><br />
<?=$my_var?><br /><br />

<? $my_var = "Goodbye, PHP!"; ?>



Although the above example looks more complicated than the original, it illustrates an important element of PHP syntax -- the <?= ?> method of output. This example may be different in syntax, but the output to the browser is identical. Using this method is no more efficient or effective than using echo, but exists to save keystrokes for the developer needing to perform the very common task of outputting a variable.

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.

Return to the PHP DevCenter.

Copyright © 2009 O'Reilly Media, Inc.