PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters
PHP Foundations

Variable Manipulation and Output

03/22/2001

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:

<?php
	
     $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:

<?php

     // Add 1 to the value of $answer
     $answer++; 	
		
     // Subtract 1 from the value of $answer
     $answer--;

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

?>

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

<?php

     // 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:

<?php

     // 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:

<?php
	
     // 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




Recommended for You

  1. Cover of Mastering Regular Expressions
    Mastering Regular Expressions
    Print: $44.99
    Ebook: $35.99
  2. Cover of Programming PHP
    Programming PHP
    Print: $39.95
  3. Cover of PHP Black Book
    PHP Black Book
    Print: $59.99
  4. Cover of Upgrading to PHP 5
    Upgrading to PHP 5
    Print: $29.95

Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com