A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such.
Another important thing in PHP is that all the statements must end with a semicolon ";". In PHP we needn't have to specify the variable type, as it takes the data type of the assigned value. The contents of a variable can be changed at any time, and so can its type. To declare a variable, you must include it in your script. You can declare a variable and assign it a value in the same statement.
Here is some code creating and assigning values to a couple of variables:
$myText = "Have a nice day!";
$myNum = 5;
echo "My favourite number is ". $myNum
The output is: Have a nice day! My favourite number is 5.
One thing that causes many problems and take hours of finding mistakes is case sensitivity. PHP is case sensitive. Have a look at the following code:
$myVar = "WebCheatSheet";
$myvar = "PHP tutorial";
echo "$myVar - $myvar";
PHP 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.
One important thing to note if you are coming from another programming language there is no size limit for variables.
PHP also allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable. A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand. The following code snippet outputs 'Have a nice day!' twice:
$firstVar = 'nice day!';
$secondVar = &$firstVar;
$secondVar = "Have a $secondVar";
Beyond the variables you declare in your code, PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code. All of these environment variables are stored by PHP as arrays. Some you can address directly by using the name of the index position as a variable name. Other can only be accessed through their arrays.
Some of the environment variables include:
||Contains information about the server and the HTTP connection. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).|
||Contains any cookie data sent back to the server from the client. Indexed by cookie name. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).|
||Contains any information sent to the server as a search string as part of the URL. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).|
||Contains any information sent to the server as a POST style posting from a client form. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).|
||Contains information about any uploaded files. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated).|
||Contains information about environmental variables on the server. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).|
The code to use the environment variables will be as follows:
$newVar = $_COOKIE["myFirstCookie"];
$newVar = $HTTP_COOKIE_VARS["myFirstCookie"];