PHP Variables

If you have used any programming or scripting language before then you know exactly what variables are. For those of you who haven't, variables are basically labels for the address locations where the values to be used by the program are stored. Each of the values you work with in your program are stored somewhere in the computer's memory and the variable name gives you a way to reference that location.

PHP uses a very strict rule on how variables are named which makes it really easy for PHP to tell what parts of your code are variables. All variable names start with a $ followed by letters and numbers and underscores. The variable name extends to the first character found that is not one of these characters (such as a space).

You assign values to variables in PHP using the = operator. (Note that all PHP statements including assignments must end with a ; character identifying where the statement ends - this is different from JavaScript where the ; is optional). For example:

$i = 1;
$sub = 'base';

PHP is a loosely typed language and so the type of content that a variable contains is usually implied by what is assigned to it. With our examples $i is a number because that is what was assigned to it while $sub is a text string. Note that PHP supports two different sorts of text strings. The first type is surrounded by apostrophes where the content is assumed to be plain text and the only character that needs to be escaped by preceding it by a slosh character is the apostrophe itself (which is therefore entered as \'). The second type is surrounded by quotes and these strings allow for variables to be substituted directly into the string as well as allowing additional escape characters such as \r for carriage return \n for linefeed, \t for tab, and \" fr quote. So 'A $sub\'s away.\n' will evaluate exactly as shown (except for losing the \ before the ' in the middle of the string) while "A $sub's away.\n" will substitute the variable into the string and also treat the \n as a linefeed rather than literally as \n.

There are a couple of special situations that you need to know about when naming and using PHP variables. The first of these is that a PHP variable name is also allowed to be enclosed within {}. This allows you to specify a variable name in the middle of text and distinguish exactly where the variable name ends even though the following character is one that could be a part of the variable name. So if $sub has a value of "base" and you substitute it into a string using "A {$sub}ment" then the string evaluates as "A basement". This also means that if you really want the variable to be enclosed in {} you need to specify them twice "{{$sub}}" evaluates as "{base}".

The second special situation is that PHP allows you to define variable variables. What this means is that $sub$i does not evaluate as 'base1' as you might expect but is instead a reference to an undefined variable $sub1. If in fact we do want to have two variables follow straight after one another rather than the value of the second becoming a part of the name of the first then we need to specify it as {$sub}$i instead.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate