What is Variable Variable Name in PHP?


What is Variable Variable Name in PHP?
A variable variable name is a variable name that is stored inside another variable. Normally, a variable name is a static string. But sometimes, you want to store a variable name inside another variable to be able to participate in operations.
PHP supports variable variable names with following basic rules:

If the name of the original variable is stored in a different variable, you can use the $$var notation to identify the original variable in your PHP source code.
For example, $varnm = “Abdul”; $$varnm represents the variable $Abdul.

If the name of the original variable can be calculated by an expression, you can use the ${exp} notation to identify the original variable in your PHP source code.
For example, $varnm = “Abdul”; ${$varnm.”_1”} represents the variable $Abdul_1.

Variables with variable names can be assigned with new data like normal variables.
For example, $varnm = “Abdul”; $$varnm = “PHP Script”; assigns “PHP Script” to $Abdul.

Variables with variable names can be used in any operations like normal variables.
For example, $varnm = “Abdul”; $$varnm = “PHP Script”; echo ($$varnm. “ is free”); prints “PHP Script is free”.

If you use an expression as the variable name, the result of the expression will be automatically converted to a string, which will be used as the variable name. With this automatic conversion, anything can be used as a variable name. Even an empty string can be used as a variable name, ${“”} is a perfect variable expression.

Now let’s take an example to confirm some rules mentioned above.
Click here to download example file.
What is Variable Variable Name in PHP? What is Variable Variable Name in PHP? Reviewed by Abdul iTech on Tuesday, March 05, 2019 Rating: 5

No comments:

Powered by Blogger.