我是PHP新手。我想根据for循环条件值生成变量名称。这是我的代码。PHP - 字符串连接产生模式
<?php
for ($i = 1; $i < $totalcolumns; $i++)
{
$pattern1 .= '\$'.'a'.$i'';
}
$pattern = str_repeat ('%d', $totalcolumns);
根据上面的代码,我已经定义了$ pattern来根据totalcolumns的值生成%d。 $ pattern部分在下面的循环中完美无缺。
while (fscanf ($file,'\''.$pattern.'\'','\''.$pattern1.'\''))
因此,如果例如我的totalcolumns值是3,则上面的while循环应该如下展开。
while (fscanf ($file,'%d%d%d',$a1,$a2,$a3))
模式正在正确扩展,我使用echo语句进行了检查。但是,如果我包含用于生成pattern1的代码,我的程序不会生成任何输出。
我正在尝试使用变量pattern1生成模式$ a1,$ a2,$ a3。我正在使用PHP的字符串连接,但我无法在屏幕中看到任何输出。有人能指导我在正确的方向吗?
你在哪里打印输出 – zod
我在定义$模式值后打印它。但是,如果我尝试打印$ pattern1没有得到任何输出。 –
看起来你正在尝试创建变量变量。也许这会帮助http://php.net/manual/en/language.variables.variable.php。 – PHPglue