Reputation: 259
I am new to PHP. I would like to generate the variable names based on the for loop condition value. This is my code.
<?php
for ( $i = 1; $i < $totalcolumns; $i++ )
{
$pattern1 .= '\$'.'a'.$i'';
}
$pattern = str_repeat ('%d', $totalcolumns);
As per the above code, I have defined $pattern to generate %d as per the value of totalcolumns. The $pattern part is perfectly fine in the below loop.
while (fscanf ($file,'\''.$pattern.'\'','\''.$pattern1.'\''))
So if for example, my totalcolumns value is 3, the above while loop should get expanded as below.
while (fscanf ($file,'%d%d%d',$a1,$a2,$a3))
The pattern is getting expanded correctly, which I checked using echo statement. However, if I include the code for generating pattern1 my program doesn't produce any output.
I am trying to generate the pattern $a1, $a2, $a3 using the variable pattern1. I am using the string concatenation of PHP but I am not able to see any output in the screen. Can someone please guide me in the right direction?
Upvotes: 0
Views: 260
Reputation: 436
<?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
based fully on: "I am trying to generate the pattern $a1, $a2, $a3"
I'm pretty sure you don't have to escape a dollar($) sign in single quotes either.
"\$"
'$'
Then if I wanted to do something with the output
<?php
$filledInString = str_replace('$a1', "REPLACED!", $pattern1);
$filledInString = str_replace('$a3', "AGAIN!", $filledInString);
echo $filledInString;
//REPLACED!, $a2, AGAIN!
?>
Or you could just be looking for variable variables, but maybe this is what you were after. Dunno, hope it helped :-)
Upvotes: 0
Reputation: 2492
May be try this :
<?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;
Will output :
\$a1\$a2
The above answers your ( actual ) question . But it seems that your need is to call a function with variable number of parameters . If it is the case call_user_func_array could help you in something along these lines :
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
}
Upvotes: 2