(click anywhere to close)

[PHP] Variables I

category: Website | course: PHP | difficulty:

Alright, so we’ve seen all the types of data PHP can handle. But, computers can’t just have data flying around – it needs to be (temporarily) saved somewhere, under a certain name. More importantly, by saving data under a name, we can find it and use it at other places in the code, together with other data. For this, we have variables.

A variable is a container for storing data

For example, we could perform a complex calculation, and then put the result inside a variable named “result”. And then, at some other point in the program, we could use this result again (in another calculation or statement) by calling it by its name. Variables are super-duper useful, and you’ll be working with them a lot, which is why I will devote two chapters to them.

Defining Variables

In PHP, variables aren’t necessarily created. A variable exists from the moment you assign a value to it. To define a variable, we use the dollar syntax ($) and assignment operator (=):

$some_variable_name = value;

This means that, unlike some other programming languages, a variable’s data type is not fixed. Any time you want, you can assign a new value to a variable (using the same syntax), even if that value is of completely different data type. Yes, PHP is that flexible.

Using Variables

To use a variable – which means accessing its value – simply call it by its name. And, don’t forget to put the dollar sign in front.

$save_result = 5*5*5;
echo $save_result; //Prints 125 in the browser

$username = "Juan";
echo "Welcome, " . $username; //Prints: Welcome, Juan

If you haven’t assigned a value to the variable you’re calling, the value will be null. You will also probably get a notice or warning by PHP that the variable is undefined.

Naming Variables

Now, I might have given the impression that you could name variables any way you like, but that’s not true. Some rules have been established to make sure PHP reads the code correctly as a variable (and not as a number, or string, or anything else). A variable name

  • Must start with a letter (lowercase or uppercase) or underscore ( _ )
  • May never start with a number
  • Can only contain alpha-numeric characters (that is, letters and figures) and underscores

It’s good practice to use descriptive names for your variables. It’s better to use long variable names that describe exactly what data they hold, instead of short and indecipherable names.

Additionally, PHP variables are usually written in snake_case. This means that you only use lowercase characters, and that you separate words by underscores. Why? Because all of PHP’s built-in functionality also follows this syntax. (For example, the function is_string(par) checks if its parameter has data type string.)

//This will give an error
$3tips = "1) Never buy a snake. 2) Don't burn down your home 3) Don't go to bed without your teddy bear";

//This is fine
$tips = "...";
$_3tips = "...";
$tips_for_you = "...";


In case you hadn’t figured it out already, we can assign any value to a variable, so we can also assign the value from another variable! For example, if you create variable $A that holds a string, you could say $B = $A, and now variable $B has the exact same string as value.

When you do this, PHP copies the value from one variable to another. With large values, though, this can add a huge amount of extra load for the server. This can be solved with references.

A reference simply points two variables at each other. They take on the same value, and if one of them changes, the other changes with it. A reference is created by putting an ampersand ( & ) in front of the variable value:

$somevar = &$othervar;
$some_value = true;
$other_value = &$some_value;

$other_value = false;
//Now both $some_value and $other_value are false.

Dynamic Variables

PHP has even more variable magic in store for us. Not only can we change the value of a variable, we can also change the variable name (and keep the value)! Variables like this are, quite logically, called variable variables.

How does PHP know we want it to use the other variable’s value, instead of the other variable’s name? Well, we can force PHP to evaluate an expression by placing it between curly braces ( { } ). In doing so, the syntax for creating variable variables becomes

${$someothervar}= value;

This isn’t restricted to other variables; we could place whole computations between those curly braces to be used as the variable name. If you know you’re only going to reference a variable, however, you can use the following shorthand syntax:

$$someothervar = value;

This isn’t even restricted to two dollar signs; you can place as many dollar signs after each other as you want, each referencing another variable (as illustrated in the example). Endless possibilities!

$a = "panda";
$panda = "awesome";
$awesome = "LEGO!";

echo $$$a; //Prints LEGO!

$panda1 = "awesome";
$panda2 = "awesomer";
$panda3 = "awesomest";

echo ${"panda" . (6/2)}; //Prints awesomest 

Case Sensitivity

When I discussed the data types, I mentioned for several types that case doesn’t matter. TRUE, True and true are all the same thing. This is true as well for all other built-in PHP functionality (although it’s highly recommended to stick to the snake case).

This is not true, however, for variables. If you declare a variable, you can only reference it by typing the exact same name. Thus, for example, $somevar and $someVar are not the same. Lots of errors are the result of people carelessly changing the case of their variables – don’t be that person, stick to the same syntax!

Any other user-defined things you’ll learn about later, such as classes, are also case sensitive.

Deleting Variables

Last but not least, how would you go about deleting a variable you don’t need anymore? There are two ways, but you should use the first one:

  • Set a variable to value null
  • Call unset($variable)

The second doesn’t just set a variable to null again, it actually completely removes the variable from the program. Usually, this is a much heavier process to execute, and you don’t really need it.

Once you use a variable that is not set, however, PHP will throw all sorts of errors and hell breaks loose. Therefore, in some cases where you’re unsure, it’s useful to check whether a variable is set or not. This is done with


When a variable is set to null, it’s also set. A variable is only not set if it has no defined value.

//If the username is set, which it is in this case, welcome the user
$username = "Ginty";

if(isset($username)) {
	echo "Welcome, " . $username;

//Now we remove the variable, and the code will NOT welcome the user a second time

if(isset($username)) {
	echo "Welcome, " . $username;
Do you like my tutorials?
To keep this site running, donate some motivational food!
Chocolate Milk