Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.

PHP Variables

It's difficult to explain what a variable is... but I suppose the simplest answer would be "it's a temporary storage area" or "it's a place to store data in temporarily". Programming languages like PHP, or JavaScript, would be virtually useless without a way to store certain bits of information, and that's where variables come in.

In PHP, variables start with a "$" sign, so "$hello" is a variable called "hello". To store data in that variable, we would simply say:
$hello = "Hi, my name is Simon";

What we've done there, is to say that the value of the variable $hello equals (=) whatever is inside the quote marks. The semicolon (;) at the end signifies the end of the variable and MUST NOT be missed off, otherwise you'll get an error message!

Once you have something stored in a variable, you can use it anywhere in your script:
$hello = "Hi, my name is Simon";
echo $hello;
<td><?PHP echo $hello; ?>

Here we've assigned some text to the variable $hello, and then we've written it out twice, once inside the original PHP script, and then breaking out of PHP to write a html table, and then going back into PHP to write out the value of $hello again, and then back out of PHP to finish off the table.
This would be the result:

Hi, my name is Simon

Hi, my name is Simon

The purpose of this example is to try and demonstrate that a PHP variable can be accessed, referenced, and used as many times as you like... for the life of that script. Once your script ends, and the html page has been delivered to the browser... any data stored in a variable is lost, unless the value of that variable is re-assigned on the start of each script.

PHP variables that contain a string of text (letters & numbers) are known as String Variables (see PHP Strings for more). There are other types of variables that look the same (still starting with $) but contain more complex information. See the section on PHP Arrays for more information.