Online Test Banks
Score higher
See Online Test Banks
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Create and Assign Values to PHP Variables

PHP variables can hold numbers or strings of characters. You store information in variables with a single equal sign (=). For instance, the following four PHP statements assign information to variables:

$age = 12;
$price = 2.55;
$number = -2;
$name = "Little Bo Peep";

Notice that the character string is enclosed in quotes, but the numbers are not.

Whenever you put information into a variable that didn’t exist before, you create that variable. For instance, suppose you use the following PHP statement:

$firstname = "George";

If this statement is the first time that you’ve mentioned the variable $firstname, this statement creates the variable and sets it to “George”. If you have a previous statement setting $firstname to “Mary”, this statement changes the value of $firstname to “George”.

You can also remove information from a variable. You might do this in order to clear out information or to initialize the variable. For example, the following statement takes information out of the variable $age:

$age = "";

The variable $age exists but doesn’t contain a value. It doesn’t mean that $age is set to 0 (zero) because 0 is a value. It means that $age doesn’t store any information. It contains a string of length 0.

You can go even further and uncreate the variable by using this statement:


After this statement is executed, the variable $age no longer exists.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

Inside Sweepstakes

Win $500. Easy.