Basics

Variables in PHP are represented by a dollar sign followed by the
name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A
valid variable name starts with a letter or underscore, followed
by any number of letters, numbers, or underscores. As a regular
expression, it would be expressed thus:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Notă:
For our purposes here, a letter is a-z, A-Z, and the bytes
from 127 through 255 (0x7f-0xff).

By default, variables are always assigned by value. That is to say,
when you assign an expression to a variable, the entire value of
the original expression is copied into the destination
variable. This means, for instance, that after assigning one
variable's value to another, changing one of those variables will
have no effect on the other. For more information on this kind of
assignment, see the chapter on Expressions.

PHP also offers another way to assign values to variables:
assign by reference.
This means that the new variable simply references (in other words,
"becomes an alias for" or "points to") the original variable.
Changes to the new variable affect the original, and vice versa.

To assign by reference, simply prepend an ampersand (&) to the
beginning of the variable which is being assigned (the source
variable). For instance, the following code snippet outputs 'My
name is Bob' twice:

It is not necessary to initialize variables in PHP however it is a very
good practice. Uninitialized variables have a default value of their type depending on the context in which they are used
- booleans default to FALSE, integers and floats default to zero, strings (e.g. used in echo) are
set as an empty string and arrays become to an empty array.

Relying on the default value of an uninitialized variable is problematic
in the case of including one file into another which uses the same
variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of
working with uninitialized variables, however not in the case of appending
elements to the uninitialized array. isset() language
construct can be used to detect if a variable has been already initialized.

User Contributed Notes 7 notes

Before a variable is used, it has no existence. It is unset. It is possible to check if a variable doesn't exist by using isset(). This returns true provided the variable exists and isn't set to null. With the exception of null, the value a variable holds plays no part in determining whether a variable is set.

Setting an existing variable to null is a way of unsetting a variable. Another way is variables may be destroyed by using the unset() construct.

The first time that a variable is used in a scope, it's automatically created. After this isset is true. At the point at which it is created it also receives a type according to the context.

<?php$a_bool = true; // a boolean$a_str = 'foo'; // a string?>

If it is used without having been given a value then it is uninitalized and it receives the default value for the type. The default values are the _empty_ values. E.g Booleans default to FALSE, integers and floats default to zero, strings to the empty string '', arrays to the empty array.

A variable can be tested for emptiness using empty();

<?php$a = 0; //This isset, but is empty?>

Unset variables are also empty.

<?phpempty($vessel); // returns true. Also $vessel is unset.?>

Everything above applies to array elements too.

<?php$item = array(); //Now isset($item) returns true. But isset($item['unicorn']) is false.//empty($item) is true, and so is empty($item['unicorn']

$item['unicorn'] = '';//Now isset($item['unicorn']) is true. And empty($item) is false. //But empty($item['unicorn']) is still true;

$item['unicorn'] = 'Pink unicorn';//isset($item['unicorn']) is still true. And empty($item) is still false. //But now empty($item['unicorn']) is false;?>

For arrays, this is important because accessing a non-existent array item can trigger errors; you may want to test arrays and array items for existence with isset before using them.

I highly recommend to use an editor that can list all variable names in a separate window.

The reason are typing errors in variable names.

<?php$somename = "nobody";// Now we want to use $somename somewhereecho $somemane ; ?>And wonder why it doesn't print "nobody".The reason is simple, we have a typing error in $somename and $somemane is a new variable.

In this example it might be easy to find. But if you use variables to calculate some things, you might hardly find it and ask yourself why your calculation is always wrong.With an editor that list all variable names in a separate window such "double" variables but with wrong typing can be easily found.

BTW:It would have been better, if the PHP language would require to use some sort of keyword to define a variable the first time.

If you need to check user entered value for a proper PHP variable naming convention you need to add ^ to the above regular expression so that the regular expression should be '^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'.

So PHP doesn't rely on $this's value itself, but rather on its name and the syntax you're accessing instance's properties and calling its methods. Also it doesn't pass any hidden parameter $this to the instance's methods nor it automatically returns $this from the __construct()or, like often being described in books which tries to teach OOP.

$this is simply an undefined variable with special meaning of its name.

From the "Properties" page, about initialising properties:"This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. "

So if you define a variable in a class (as a member variable/property), things like public <?php $test = 2+3; ?> are invalid, because logic has to be performed on the right hand side. It HAS to be either a constant, or a scalar value (int/string/bool/float). Not even another variable which is a constant (so <?php public $test = "test"; public $invalid = $test; ?> would not work.

Just thought this should be mentioned here for all those like me who get here before getting to the "Properties" page.