PHP concatenates two variable names

I have a php script that gets $_POST to decide which array will be returned. Example:

 $n = $_POST['n']; // 1, 2 or 3 $a1 = array ('something', 'something else', 'another thing'); $a2 = array ('something 2', 'something else 2', 'another thing 2'); $a3 = array ('something 3', 'something else 3', 'another thing 3'); 

now I want to get an array corresponding to the value of $n , say "2" .

How can I say echo $a . $n echo $a . $n to get $a2

Thanks.

+6
source share
2 answers

${'a'.$n} gives you $a2 if $n is 2 .

+16
source

It would be better if you did this:

 $a = array(); $a[1] = array('bla bla', 'bla bla'); $a[2] = array('asdasd', 'asdasd'); 

And then you can call this:

 echo $a[intval($n)] 
+3
source

Source: https://habr.com/ru/post/896377/


All Articles