PHP Variables

This article explains PHP variables, and shows how to create, change, and use variables, as well as how to name PHP variables.

Like most programming languages, PHP lets you create variables in your scripts. A variable is a storage container that holds a value. This value can change as the script runs. You can:

  • Assign any value you like to a variable
  • Access the value stored in a variable, and
  • Change a variable's value at any time.

Variables are useful because they let you write flexible scripts. For example, a script that can only add 3 and 4 together isn't very useful. A script that can add any two values together, though, is much more flexible.

Creating a variable

To create a new variable in PHP, you can just write the variable's name:


Notice the dollar ($) symbol before the variable name. All PHP variables have a dollar symbol in front.

This is known as declaring a variable. It's also a good idea to give the variable an initial value at the time you declare it — this is known as initializing the variable:

$myVariable = 23;

If you don't initialize a new variable then it takes on a value of null.

Changing a variable's value

You've just seen how to assign a value to a variable: you simply write the variable name, followed by an equals sign, followed by the value you want to assign.

To change a variable's value, simply assign the new value:

$myVariable = 23;
$myVariable = 45;
$myVariable = "hello";

The first line of code creates a new variable with a numeric value of 23, while the second line changes the variable's value to 45. The third line changes the value again — this time to a string of text, "hello".

PHP is a loosely-typed language, which means you can change the type of data that a variable holds whenever you like. In the above example, $myVariable starts off holding a number, and finishes by holding a string of text.

Using a variable's value

To use the value of a variable in your script, simply write the variable's name. For example, to display the value of $myVariable you'd use:

echo $myVariable;

To add the values of two variables $x and $y together and display the result, you could write:

echo $x + $y;

Variable names in PHP

Before leaving the topic of PHP variables, it's worth taking a look at how to name variables. All PHP variable names have to follow these rules:

  • They start with a $ (dollar) symbol.
  • The first character after the dollar must be either a letter or a _ (underscore) symbol.
  • The other characters may be letters, numbers, and/or underscores.

Variable names can be as long as you like, but it's a good idea to keep them relatively short (otherwise they can get quite unwieldy). It's also good to use meaningful variable names — for example, $numWidgets is much more useful than $nw.

PHP variable names are case-sensitive, which means that $myVariable and $MyVariable refer to different variables.

Now that you know how PHP variables work, you're well on your way to writing useful PHP scripts!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or PHP 5.3.

Follow Elated

Related articles

Responses to this article

4 responses (oldest first):

06-Sep-09 00:04
Nice article, thanks. Is there any way to find out the type of data that's stored in a variable?
06-Sep-09 00:43
06-Sep-09 02:19
Also if you want to test for specific types of data you can also use the is_* functions, described in "Finding out the data type of a value":
06-Sep-09 03:06
Perfect - thanks for your help chrishirst & matt

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page