dsaba Posted May 28, 2007 Share Posted May 28, 2007 here is an array: $sampleArray_1[] = 'hello'; $sampleArray_1[] = 'world'; a function: $number = 1; function whatever($num) { $array = 'sampleArray_'.$num; print_r($GLOBALS[$$array]); } whatever($number); it does not print the $sampleArray How do I do this correctly? as you can see I am using a dynamic variable name with the variable variable syntax if I do this outside the function: $num = 1 $array = 'sampleArray'.$num; print_r($$array); it prints out just fine also, if I do this inside the function: print_r(GLOBALS['sampleArray_1']); it also prints out fine it is doing this variable variable syntax accessing within the function that is causing problems how do I do it correctly? -thank you Quote Link to comment https://forums.phpfreaks.com/topic/53283-solved-how-to-print-a-variable-variable-globals-array/ Share on other sites More sharing options...
kenrbnsn Posted May 28, 2007 Share Posted May 28, 2007 You're not really using variable variables here, just use the index plain: <?php $sampleArray_1[] = 'hello'; $sampleArray_1[] = 'world'; function whatever($num) { $array = 'sampleArray_'.$num; echo '<pre>' . print_r($GLOBALS[$array],true) . '</pre>'; } whatever(1); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/53283-solved-how-to-print-a-variable-variable-globals-array/#findComment-263307 Share on other sites More sharing options...
dsaba Posted May 28, 2007 Author Share Posted May 28, 2007 your solution works! thanks however you said that I don't need to use variable variables, or that I am not using variable variables?? because if you are saying that I am not using variable variables then what do you call this? $num = 1 $array = 'sampleArray'.$num; print_r($$array); I call that using variable variable syntax, so is that what its called or what? (ugh... this terminology) Quote Link to comment https://forums.phpfreaks.com/topic/53283-solved-how-to-print-a-variable-variable-globals-array/#findComment-263318 Share on other sites More sharing options...
wildteen88 Posted May 28, 2007 Share Posted May 28, 2007 no, ken said that you don't need to use variables variables you just use $array as a normal index to the $GLOBALS superglobal. You only need to use $array as a variable variable if you wasn't using the $GLOBALS superglobal. Quote Link to comment https://forums.phpfreaks.com/topic/53283-solved-how-to-print-a-variable-variable-globals-array/#findComment-263320 Share on other sites More sharing options...
dsaba Posted May 28, 2007 Author Share Posted May 28, 2007 tricky tricky wording ! hehe I understand now, that's what I thought he said Topic Solved! Quote Link to comment https://forums.phpfreaks.com/topic/53283-solved-how-to-print-a-variable-variable-globals-array/#findComment-263324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.