2013-10-02 52 views
0

我是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的字符串连接,但我无法在屏幕中看到任何输出。有人能指导我在正确的方向吗?

+0

你在哪里打印输出 – zod

+0

我在定义$模式值后打印它。但是,如果我尝试打印$ pattern1没有得到任何输出。 –

+1

看起来你正在尝试创建变量变量。也许这会帮助http://php.net/manual/en/language.variables.variable.php。 – PHPglue

回答

2

可能是试试这个:

<?php 
// You probably might have code here to populate $totalcolumns . 
// For test purpose I assumed a value . 
    $totalcolumns = 3; 

// Initialize $pattern1 
    $pattern1 = ''; 
// Make sure weather you need $i < $totalcolumns or $i <= $totalcolumns 
    for ($i = 1; $i < $totalcolumns; $i++) 
    { 
    // Your line was $pattern1 .= '\$'.'a'.$i''; which has systax error due to two single quotes before the semicolon 
     $pattern1 .= '\$'.'a'.$i; 
    } 
    echo $pattern1; 

将输出:以上

\$a1\$a2 

回答您的(实际)问题。但似乎您需要调用具有可变数量参数的函数。如果是这种情况call_user_func_array可以帮助你的东西沿着这些路线:

call_user_func_array
Variable variables
How to pass variable number of arguments to a PHP function

<?php 
// You probably might have code here to populate $totalcolumns . 
// For test purpose I assumed a value . 
    $totalcolumns = 3; 

// Also assuming some values for $a1, $a2, $a3 etc. 
    $a1 = 'abc'; 
    $a2 = 'pqr'; 
    $a3 = 'xyz'; 

// For test purpose I used a string replace it with the actual file handle 
    $file = 'File handle'; 

// Initialize $pattern 
    $pattern = ''; 

// Define an array to hold parameters for call_user_func_array 
    $parameterArray = array(); 

// Put first parameter of fscanf at index 0 of $parameterArray 
    $parameterArray[0] = $file; 

// Initialize second parameter of fscanf at index 1 of $parameterArray 
    $parameterArray[1] = $pattern; 

    $parameterArrayIndex = 2; 
    for ($i = 0; $i < $totalcolumns; $i++) 
    { 
     $pattern .= '%d'; 
     $parameterArray[$parameterArrayIndex] = ${"a".($i+1)}; // Variable variables 
     $parameterArrayIndex++; 
    } 

// Update second parameter of fscanf at index 1 of $parameterArray 
    $parameterArray[1] = $pattern; 

    var_dump($parameterArray); 

    while(call_user_func_array('fscanf', $parameterArray)) 
    { 
     // Do what ever you need to do here 
    } 
0
<?php 
$totalcolumns = 3 
for ($i = 1; $i <= $totalcolumns; $i++){ 
    $pattern1 .= '$a' . $i . ', '; 
} 
//remove the last comma and space. 
$pattern1 = rtrim($pattern1, ", "); 
echo $pattern1; 
//$a1, $a2, $a3 

充分基于: “我试图生成模式$ a1,$ a2,$ a3“

我很肯定你没有必须避免在单引号中输入美元($)。

“\ $”

“$”

然后,如果我想做一些与输出

<?php 
$filledInString = str_replace('$a1', "REPLACED!", $pattern1); 
$filledInString = str_replace('$a3', "AGAIN!", $filledInString); 
echo $filledInString; 
//REPLACED!, $a2, AGAIN! 
?> 

或者你可能只是在寻找可变的变量,但也许这是你在做什么。不知道,希望它帮助:-)