Using braces with dynamic variable names in PHP
Wrap them in {}
:
${"file" . $i} = file($filelist[$i]);
Working Example
Using ${}
is a way to create dynamic variables, simple example:
${'a' . 'b'} = 'hello there';
echo $ab; // hello there
Dynamic variable name php 7.4 and value assigment
This piece of code will work in the newest version of PHP as well.
$column = 'A';
${"total{$column}"} = 20;
echo ${"total{$column}"}; // 20
echo $totalA; // 20
Using Dynamic Variable Names to Call Static Variable in PHP
So the answer was to use a function for a constant lookup:
$handler = new StreamHandler('/var/log/php/php.log', constant("Monolog\Logger::" . $level));
from php 5.4 to php 7 , useing dynamic variables is confused
For dynamic variables like this, you need to wrap the string in curly braces. You almost had it there in your last line. However, remember that it's not the array $attributes
that you're using to indicate the dynamic variable, it's the string at $attributes[0]
.
So this should work:
$this->{$attributes[0]}
Dynamic variables names are powerful, but easy to get confused with. If you have the option, I'd recommend using just an array:
$this->attributes = [
"boy" => "eric",
"girl" => "lili",
"none" => "who"
];
echo $this->attributes["boy"];
When to wrap curly braces around a variable
What are PHP curly braces:
You know that a string can be specified in four different ways. Two of these ways are – double quote("") and heredoc syntax. You can define a variable in those 2 types of strings and PHP interpreter will parse or interpret that variable too, within the strings.
Now, there are two ways you can define a variable in a string – simple syntax which is the most used method of defining variables inside a string and complex syntax which uses curly braces to define variables.
Curly braces syntax:
To use a variable with curly braces is very easy. Just wrap the variable with { and } like:
{$variable_name}
Note: There must not be any gap between { and $. Else, PHP interpreter won't consider the string after $ as a variable.
Curly braces example:
<?php
$lang = "PHP";
echo "You are learning to use curly braces in {$lang}.";
?>
Output:
You are learning to use curly braces in PHP.
When to use curly braces:
When you are defining a variable inside a string, PHP might mix up the variable with other characters if using simple syntax to define a variable and this will produce an error. See the example below:
<?php
$var = "way";
echo "Two $vars to defining variable in a string.";
?>
Output:
Notice: Undefined variable: vars …
In the above example, PHP's interpreter considers $vars a variable, but, the variable is $var. To separate a variable name and the other characters inside a string, you can use curly braces. Now, see the above example using curly braces-
<?php
$var = "way";
echo "Two {$var}s to define a variable in a string.";
?>
Output:
Two ways to define a variable in a string.
Source: http://schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/
PHP, how to use a character as a variable name?
You should refer to PHP Variable variables.
A variable variable takes the value of a variable and treats that as the name of a variable.
Try one of these:
$string = "ABCD";
echo ${$string[0]}; // 10
echo $$string[0]; // 10
PHP Dynamic Variable Name
Two ways:
First, use a variable:
$property = 'field_' . $_taxVocabName;
$node->$property;
Second, use curly brackets:
$node->{'field_' . $_taxVocabName};
Related Topics
Mvc For Advanced PHP Developers
Simplest Two-Way Encryption Using PHP
Convert a Series of Parent-Child Relationships into a Hierarchical Tree
How to Remove "Index.PHP" in Codeigniter'S Path
How to Sort Files by Date in PHP
Reference: Why Are My "Special" Unicode Characters Encoded Weird Using Json_Encode
Difference Between Http_Host and Server_Name in PHP
How to Encrypt/Decrypt Data in PHP
Curl Error 60: Ssl Certificate: Unable to Get Local Issuer Certificate
How to Get Id of the Last Updated Row in MySQL
PHP Sort a Multidimensional Array by Element Containing Y-M-D H:I:S Date
How to Solve PHP Error 'Notice: Array to String Conversion In...'
How to Get a Hex Dump of a String in PHP
Why I Have to Call 'Exit' After Redirection Through Header('Location..') in PHP