PHP Variables

Written by Scott on Thursday, June 11th, 2009

I am going to start a new series on PHP programming 101. Check back often for new tutorials, ideas, facts and featured functions. If you have any suggestions or questions about anything PHP related don’t hesitate to leave a comment.

The concept of a variable is used to store information that can be used later on. PHP variables are very easy to setup and define; here is the syntax:

$nameOfVariable = Value;

where Value can be any string.

Examples:

$var = ‘Hello World’;
$data = ‘12345’;
$myString = ‘xyz’;

To print a variable out to the webpage, just use the ‘echo’ statement:

echo $var;

According to www.tizag.com, here are some important tips on variable naming conventions:

There are a few rules that you need to follow when choosing a name for your PHP variables.

PHP variables must start with a letter or underscore “_”.
PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
Variables with more than one word should be separated with underscores. $my_variable
Variables with more than one word can also be distinguished with capitalization. $myVariable

Comments are closed.