PHP: Variables

This entry is part 4 of 54 in the series PHP Tutorial

Sit back and relax.

Let’s not hurry and move to the more complex example. ┬áSometimes, it is good to just run code written by others.

We now look at how variables are declared in PHP.


$myText = "Happy New Year! "; 
$myNum = 2014;

echo $myText;
echo "The year is ". $myNum;
Try The Code

In PHP, a variable is preceded by a $ sign and declaration is actually not needed.

As in the example above, if you have $myNum = 2014, the PHP parser will interprete $myNum as an integer directly.

The $myText is a string variable as the text Happy New Year! is enclosed in a double quote.

Notice in line 8, there is a .(dot) in the line.  The . in PHP is a string concatenation. It joins a string and an integer or a string to a string together.