PHP Basics: Variables
What’s a Variable?
A variable is a symbolic representation of a value such as integer, string, or function result. But as its name suggest, it can change over time or vary so that it can be used many times within a script.
Variables in PHP
In PHP, variable names are going to start with a “$” sign, then they are going to be followed by a letter or an underscore “_”. A variable name is case-sensitive and can contain letters, numbers, underscores, or dashes (a-z, A-Z, 0-9, _, or -), but no spaces otherwise PHP will think that it is a new entity.
Possible variable names:
-
$variable = value; // lower case
-
$VARIABLE = value; // UPPER CASE
-
$Variable = value; // Start with a capital character
-
$myVariable = value; // camelCase also spelled "camel case"
-
$my_variable =value; // with underscore between words
-
$my-variable = value; // Using “-“. Not used to avoid confusion because the dash sign looks a lot like the minus sign.
-
$variable1 = value; // Using numbers
-
$_variable = value; // Single underscore at the beginning. Used also by PHP to define a certain type of variables
-
$__variable = value; // Multiple underscore at the beginning. Sometimes its hard to tell how many underscores have been used.
My variable naming preferences:
-
$my_variable = value; // I use this naming style for my variables and the camel case style for my functions.
-
$myVariable = value;
Using variables in PHP
- In PHP statements, A value can be assigned to a variable using the “=” operator and the statement must end with a semicolon “;” as shown above.
- A PHP variable does not need to be declared before being set. The variable is declared automatically when you use it.
- In PHP there is no need to specify the variable type. PHP automatically converts the variable to the correct data type, depending on the assigned value.
- Variables in PHP may be passed by reference. When a variable is passed by reference, the reference sign,”&” is used.
-
<?php function foo (&$bar) {} ?>
Using Variable Variables in PHP
A variable variable is a variable whose name is contained in another variable. this method is also called variable reflection. for instance, if I had a variable $desc with the value of “color”, I could set a variable $color to “red” by simply writing $$desc = “red”. This process calls the value of $desc and then creates a variable out of it.
-
<?php // The first variable is a standard assignment.
-
$desc = "color";
-
-
// The second variable is named by "reflecting" the value of the first.
-
$$desc = "red";
-
?>
Therefore $$desc would produce the same output as $color
Please notice the use of curly braces {} symbols around the variable variable in the first echo. Another thing you can do by using the curly braces is to make a variable from a string, doing so will allow you to pass a string from one page to another, and use that string to pull the information from a variable of the same name.
-
<?php
-
$uri = $_SERVER;
-
foreach ($uri as $key => $var)
-
{
-
$$key = $var
-
}
-
…..
-
?>
Also it is possible to create variables whose names do not follow the constraints listed above.
-
<?php /* 911 is your variable name, this would normally be invalid. */
-
$emergency_number = ’911’;
-
// Again, you assign a value
-
$$emergency_number = ’18009119110’;
-
// Finally, using curly braces you can output ’18009119110’
There are many applications for variable varaible in PHP depending on the developer preferences. It’s a very powerful tool, and should be used with an extreme care.
If you have any addition, please use the comments form bellow!
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


