Last Updated on December 27, 2020 by Roshan Parihar
In this tutorial, learn how to create a dynamic variable in PHP. The short answer is to use a dollar($
) sign at the start of a normal variable for more than a single time. Find other useful methods to create a dynamic variable with examples.
How to Create Dynamic Variable in PHP
A dynamic variable is a variable inside a variable in PHP. It is not a normal variable that contains some value. A normal variable is a variable that contains an assigned value.
Normal Variable
1 2 3 |
<?php $var = "Hello"; ?> |
The above example showing the normal variable with an assigned string value.
The normal variable contains the single dollar($
) sign. If you add another dollar($
) sign before the variable, we can call the result as a dynamic variable. Let’s create by using the example given below.
Dynamic Variable
1 2 3 |
<?php $$var ?> |
Add dollar($
) sign at the starting of the normal variable. The example of the normal variable contains the assigned value as “Hello”. The dynamic variable can also be written as $Hello
.
Print Dynamic Variable in PHP
You have to use the PHP echo statement to print the variable. But there are few things about dynamic variables you should know first. If you print the variable as given in the example below. You will get an error message which shows the variable is not defined.
1 2 3 4 |
<?php $var = "Hello"; echo $$var; ?> |
Output
The above example showing the error message in the output. The normal variable $var
contains the input string “Hello”. After you add a dollar($
) sign before the $var
variable. It becomes $Hello
. So, you also have to assign a value to the variable Hello
. Because of this, you will get print the value in the output.
1 2 3 4 5 |
<?php $var = "Hello"; $Hello = "My World"; echo $$var; ?> |
Output
The above example shows the output prints the value of $Hello
variable.
Different Ways of Writing Variable in PHP
There are different ways of writing the dynamic variable in PHP. Below are the useful methods to create only the dynamic variable. However, if you want to create more types of variables in PHP, read our post to create a variable in PHP.
Using a Single Dollar($
) Sign Before Another Variable
1 |
$$var |
You can easily create the variable using the above method. The method requires just adding a dollar($) sign before any of your created variables. Almost all the PHP developers using this method in their projects.
Embedding the Variable Within the Parenthesis
1 |
${$var} |
parenthesis can also be used to embed the variable after the dollar($) sign. This is another method you can use to create a variable.
Using More than Single Dollar($
) Sign
1 |
$$$var |
In addition to all the above methods, you can add more and more dollar($
) sign. However, using more dollar($
) sign makes it complex for the users to use in the project. So, prefer to use only a few dollar($
) signs for creating PHP dynamic variable.
You may also like to read
I hope you like this post on how to create a dynamic variable in PHP.