devWhiz Posted May 22, 2011 Share Posted May 22, 2011 <?php $a = array(3, 4, 18, 15); $b = array(12, 10, 11, 24); $c = array(5, 6); $d = array(7, 8, 13); $e = array(19, 16); function test($ID) { if(in_array($ID, $a)) { echo "a"; } if(in_array($ID, $b)) { echo "b"; } if(in_array($ID, $c)) { echo "c"; } if(in_array($ID, $d)) { echo "d"; } if(in_array($ID, $e)) { echo "e"; } } $test = 3; test($test); ?> Why doesn't that echo "a" I don't really get what I am doing wrong, any help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/237164-what-am-i-doing-wrong-with-this-function/ Share on other sites More sharing options...
salathe Posted May 22, 2011 Share Posted May 22, 2011 Have a read of the variable scope section of the manual. If you still aren't sure what is happening, let us know; though that page should make everything clear. Quote Link to comment https://forums.phpfreaks.com/topic/237164-what-am-i-doing-wrong-with-this-function/#findComment-1218868 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 $GLOBALS[] was the fix, Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/237164-what-am-i-doing-wrong-with-this-function/#findComment-1218875 Share on other sites More sharing options...
jcbones Posted May 23, 2011 Share Posted May 23, 2011 Globals is never the fix, and should be depreciated IMHO. Either build the arrays inside the function, or pass them as arguments. Setting Globals defeats the purpose of a function, as you have now tied the function to a specific instance, and can no longer use the function, if one of the globals doesn't exist in the script. Quote Link to comment https://forums.phpfreaks.com/topic/237164-what-am-i-doing-wrong-with-this-function/#findComment-1218882 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.