在php中获取变量值


Getting Variable value in php

我有变量$CV1$CV2$CV3。。。如何在PHP中的while循环中获取这些变量的值?下面是我的代码

$CV1=$fetch->Custom_field1;
$CV2=$fetch->Custom_field2;
$CV3=$fetch->Custom_field3;
$CV4=$fetch->Custom_field4;
$query=mysql_query("select * from customfields where CustomField like '%Invoice%' and CustomLabel != ''");
while($get=mysql_fetch_object($query))
        {
            $i++;
            $Label=$get->CustomLabel;
            $CFV='$CV'.$i;
            echo "<tr><td>$Label</td><td><input type='text' name='custom_field$i' value='$CFV'></td></tr>";
        }

而不是得到$CV1$CV2的值。我正在获取变量名称本身作为输出。$CV1$CV2

使用array()而不是命名变量。

$CV[1] = $fetch->Custom_field1;
$CV[2] = $fetch->Custom_field2;

然后通过这种方式获得您的价值:

$CFV = $CV[$i];

另外,不要忘记初始化$i

您可以使用:

$CFV = ${'CV'.$i};

参见代码板示例

这是因为你用单引号包装变量名,而不是解析变量值,所以你只是按原样获取文本。

尝试:

$CFV = $CV{$i};

使用类似的php变量

$CFV="${CV$i}";