在 php 中附加两个变量,附加字符串中有 `$` 符号
appending two variables in php having `$` symbol in the appending string
我能知道如何使用循环创建以下类型的字符串吗?
$row[$opt1val]
$row[$opt2val]
$row[$opt3val]
$row[$opt4val]
//...
一般我是这样创作的
`
foreach($myArray as $someVar => $value){
$i++;
${"opt".$i."val"} = $value;
}
但是关于如何创建这种 `$row[$opt1val] 类型的字符串,以便该字符串应该表现为变量。
我尝试了但失败了,因为我可能不会在 $
符号处做,请耐心等待,因为我已经一天了 php
$varName = 'row' . $i . 'val';
$row[ $$varName ];
使用变量中的变量 -
$opt1val = '1';
$opt2val = '2';
for($i = 1; $i <= 2; $i++) {
$val = 'opt'.$i.'val';
echo $$val. " - ";
}
在你的情况下它会是 -
$val= 'opt' . $i . 'val';
$row[ $$val];
我能知道如何使用循环创建以下类型的字符串吗?
$row[$opt1val]
$row[$opt2val]
$row[$opt3val]
$row[$opt4val]
//...
一般我是这样创作的 `
foreach($myArray as $someVar => $value){
$i++;
${"opt".$i."val"} = $value;
}
但是关于如何创建这种 `$row[$opt1val] 类型的字符串,以便该字符串应该表现为变量。
我尝试了但失败了,因为我可能不会在 $
符号处做,请耐心等待,因为我已经一天了 php
$varName = 'row' . $i . 'val';
$row[ $$varName ];
使用变量中的变量 -
$opt1val = '1';
$opt2val = '2';
for($i = 1; $i <= 2; $i++) {
$val = 'opt'.$i.'val';
echo $$val. " - ";
}
在你的情况下它会是 -
$val= 'opt' . $i . 'val';
$row[ $$val];