PHP Variables and strings
A variable is a means of storing a value, such as a text string or a number. In PHP you do not have to declare your variable, as it's automatically declared when you set it. Since you do not need to declare the variable, you do not have to specify what kind of data it contains either.
In PHP you define a variable with the following form:
$variable_name = "value";
The variable name comes first, then an assignment operator, which in this case is an equal sign, and then the variable data. We have now assigned the rather drearily name variable $variable_name with a text string holding a single word. A variable is always prefixed with a dollar sign, unlike for instance javascript. If you do not prefix the variable name with a dollar sign, PHP will generate an error and your code will not execute. You will also notice that we have enclose the text string in quotes and ended the sentence with a semicolon. All code lines in PHP need to end with a semicolon (Please see this page for more information regarding the PHP syntax).
Permalink for this article http://mirror.magicode.org/content/PHP_Tutorial__Variables_
You can set the variable name without quotes if you're assigning a number to the variable. The following examples will both work:
$variable_name=10;
$variable_name="10";
Both will set $variable_name with the value of 10. However, this will not work with text strings, which you always must enclose with quotes.
This text was originally written for http://blog.magicode.org
Note: variable names are case-sensitive, so you must make sure you use the same capitalization when using a variable. The variables $variable_name and $Variable_name are different variables in the eyes of PHP. If you see this notice on any site other than magicode.org, it's probably been lifted without consent
You may use single quotes instead of double quotes. The following pieces of code are both valid:
$variable_name="value";
$variable_name='value';
But you may not mix single quotes and double quotes.
A note on single and double quotes
At this point you may think that using double quotes and single quotes is interchangable and that it doesn't matter which one you use. This may lead to some unexpected results, because there is a very important difference between the two methods that you need to know.
Variables assigned within double quotes are parsed by PHP. Variables assigned within single quotes are not. This means that if you put a variable within a double quote, PHP will insert the variable value, while in single quotes PHP will print the exact string including the dollar sign, as in this example:
$variable_one=1;
Note: Numbers can be assigned to variables without using quotes.
echo "$variable_one + 1 = 2";
This will print 1 + 1 = 2 to the screen.
echo '$variable_one + 1 = 2';
This will print $variable_one + 1 = 2 to the screen.
It is therefore important to distinguish between these two methods to avoid unnecessary debugging sessions later on.
Heredocs and nowdocs
PHP has two additional ways of specifing a string, although they are very little used compared to the regular ways of using single or double quotes.
A heredoc is specified like this:
$variable_name = <<<HEREDOCDEMO
This is a simple test
message using heredoc.
HEREDOCDEMO;
The variable $variable_name now contains the text between the <<< operator and the start identifier and the end identifier (these must be the same, and in the example above, this is HEREDOCDEMO). Variable within a heredoc will be parsed.
Note on heredoc. The end identifier must not be indented (ie. no spaces or tabulators before the identifer), or PHP will return an error.
A nowdoc are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. Nowdocs are specified like this:
$variable_name = <<<'NOWDOCDEMO'
This is a simple test
message using nowdoc.
'NOWDOCDEMO';
The difference is as you can see that the identifer is specified enclosed in single quotes. Variable inside nowdocs are not parsed.
Note on nowdoc. Support for nowdoc was included from PHP version 5.3.0. Using nowdocs with older versions of PHP will not work.
