Using braces with dynamic variable names in PHP
P粉717595985
P粉717595985 2023-08-29 12:05:01
0
2
297
<p>I'm trying to use dynamic variable names (I'm not sure what they are actually called), but something like this: </p> <pre class="brush:php;toolbar:false;">for($i=0; $i<=2; $i ) { $("file" . $i) = file($filelist[$i]); } var_dump($file0);</pre> <p>returns <code>null</code>, which tells me it doesn't work. I don't know what syntax or technology I'm looking for, which makes research difficult. <code>$filelist</code> was defined before. </p>
P粉717595985
P粉717595985

reply all(2)
P粉043295337

Wrap them in {}:

${"file" . $i} = file($filelist[$i]);

Working example


Using ${} is a way to create dynamic variables, a simple example:

${'a' . 'b'} = 'hello there';
echo $ab; // hello there
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!