How to Create Dynamic Variable Using PHP Examples

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. 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 which contains some value. A normal variable is a variable that contains an assigned value.

Normal Variable

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

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 variable 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.


Notice: Undefined variable: Hello

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.


My World

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 type of variable in PHP, read our post to create a variable in PHP.

Using Single Dollar($) Sign Before Another Variable

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

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

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

Also, check out more how-to posts based on PHP in PHP faq to increase your knowledgebase.

Hope, you like this post of how to create a dynamic variable in PHP. If you have any query regarding the tutorial, please comment below.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.