g
Printer Friendly Version

editor  
BellaOnline's PHP Editor
 

The PHP Variable

The PHP variable is the work horse of the PHP programming language. It works like one of those cardboard storage boxes that you can buy from the office supply store. The storage box is empty (or null) until you put something inside. That something is the information or data that you wish to store inside the variable. The information or data stays there, nice and cozy, until you change it or remove it.

You can think of a variable like an equation in algebra. Remember algebra? You were told that x = 1. That meant that the value of x was equal to 1. You can think of x as the PHP variable and the data being stored in this x variable is 1.

x = 1 // In algebra this means that x is equal to 1

Variables in the PHP programming language work somewhat like the algebra equation. But there are a few differences.
  • In PHP you can change the data that is stored in the x variable by manipulating it. Let's add 2 to the variable x.

    $x = 1;
    // This sets the value of the variable to 1

    $x = $x + 2;
    // This sets the value of the variable to 3

    Note that in PHP programming, you put a dollar sign before the variable name. And you finish with a semicolon.

  • When you assign a numeric value to a PHP variable, as in our example above, you do not enclose the value within quotation marks. However, when you assign a character string value to a PHP variable you do enclose the character string within quotation marks.

    $x = 1;
    // This sets the value of the variable to 1

    $x = "one";
    // This sets the value of the variable to the
    // character string one

  • You can also remove any value being stored in the variable by setting the variable to null.

    $x = ""; // x is now null or empty

    This is different from setting the value of the variable to 0. In programming, 0 is a value just like 1, 2, or 3. So x = 0; means that the value of the variable x is 0 -- but it is not empty. To make the variable empty you must set the value to null as we did in the example above. x = ""; Now there is nothing inside the storage box.

  • It is a good practice to set the value of your variables to null before you use them in your program.

    $x = ""; // x is null or empty
    $x = 1; // the value of x is set to 1
    $x = $x + 3; // the value of x is now set to 4

  • You create a PHP variable by assigning a value to the variable, as in the examples above. But you cannot uncreate the variable by just assigning a null value to it--the variable still exists. To uncreate a variable you must use the unset statement.

    unset($x);

For those who hate math, I promise that this is the entire math that you need to know to learn the basics of programming in PHP.




This site needs an editor - click to learn more!

PHP Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2023 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor