0

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

jonnypixel
  • 327
  • 5
  • 27
  • 1
    I think @wizkid figured out what you were asking. If his answer doesn't do it for you, please explain a little better what you're really trying to do. – TecBrat May 15 '14 at 03:20
  • 1
    Possible duplicate of http://stackoverflow.com/questions/9257505/dynamic-variable-names-in-php – codescribblr May 15 '14 at 03:23

2 Answers2

3

This should work:

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

But I would suggest that using arrays is probably better:

$q[$n] = $db->getQuery(true);
WizKid
  • 4,888
  • 2
  • 23
  • 23
  • Thanks wizkid, im trying this but it may get complicated as i need to apply that right through the file and in some cases it may not work using the top example. May do with the bottom one but im trying to work out how. – jonnypixel May 15 '14 at 03:25
  • Working on implementing array, it is getting messy. Thanks wizkid – jonnypixel May 15 '14 at 03:44
0

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
?>
1owk3y
  • 1,115
  • 1
  • 15
  • 30