Question

I have tried:

$n=1;

$q$n = $db->getQuery(true);

etc...

$q.$n = $db->getQuery(true);

etc...

$q[$n] = $db->getQuery(true);

etc...

$q{$n} = $db->getQuery(true);

none of them will give me

$q1 = $db->getQuery(true);

I know its probably a loop thing but the file will only ever have one digit for $n.

eg: $n=1; only once in that file.

Thanks in advance for any help on this

Cheers Jonnypixel

Was it helpful?

Solution

This should work:

${'q'.$n} = $db->getQuery(true);

But I would suggest that using arrays is probably better:

$q[$n] = $db->getQuery(true);

OTHER TIPS

What you are describing is known as 'variable variables' in PHP. Here's the docs for it, and here's a working, tested example.

<?php 
  $q1 = "Just an example";
  $n = 1;
  //
  $varname = "q$n"; // "q1"
  echo "Value of $" . $varname . " is: " . ${$varname};
  // writes: 
  // Value of $q1 is: Just an example
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top