15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Declaring Variables 499<br />

Declaring Variables<br />

PHP variables can hold any type of data, so a variable might hold a string<br />

one moment and a number the next. To declare a variable in PHP, you must<br />

begin every variable name with the dollar symbol ($), such as<br />

Book V<br />

Chapter 4<br />

$VariableName = value;<br />

PHP<br />

VariableName can be any descriptive name, but PHP is a case-sensitive language<br />

so $MyAge is considered a completely different variable from $myage.<br />

Some programmers use uppercase letters to make variable names easier to<br />

find, and others use all lowercase.<br />

One unique feature of PHP is its ability to reference the same value. For<br />

example, consider the following code:<br />

$myage = 35;<br />

$yourage = $myage;<br />

$myage = 109;<br />

In this example, the $myage variable is initially set to 35 and the $yourage<br />

variable is set to the $myage variable, which means the $yourage variable<br />

also contains the value of 35. The third line stores the value 109 into the<br />

$myage variable, but the $yourage variable still holds the value of 35.<br />

By referencing variables with the ampersand symbol (&), PHP allows a variable<br />

to contain identical data without specifically assigning those values. For<br />

example:<br />

$myage = 35;<br />

$yourage = &$myage;<br />

$myage = 109;<br />

The second line in the preceding PHP code tells the computer that the $yourage<br />

variable references the $myage variable, so whatever value the $myage variable<br />

contains from now on will automatically get stored in the $yourage<br />

variable.<br />

After the third line, the $myage variable now contains the value of 109, so<br />

the $yourage variable contains 109 too.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!