meltingpoint Posted September 13, 2010 Share Posted September 13, 2010 I run these functions to find the key of a given element inside an array. Like so; $ban_ip_file = file("ip_file.txt"); foreach($ban_ip_file as $key => $value) { $value = trim($value); if($value == $ip_from_form)// passed to page by a form $set = $key; } How do I make $set available for testing and file writing further down the script? Tried assigning global $set; just above the $set=$key; ----but it did not work. Quote Link to comment https://forums.phpfreaks.com/topic/213266-making-variable-avaliable-outside-function-global/ Share on other sites More sharing options...
objnoob Posted September 13, 2010 Share Posted September 13, 2010 you can use the return keyword inside the function to return a value! $myVariable = myFunction('Bye'); function myFunction($var){ switch ($var){ case 'Hi'; return 'Hiya!'; break; case 'Bye': return 'See you later!'; break; default: return 'Sorry, I don\'t understand'; } } Quote Link to comment https://forums.phpfreaks.com/topic/213266-making-variable-avaliable-outside-function-global/#findComment-1110457 Share on other sites More sharing options...
meltingpoint Posted September 13, 2010 Author Share Posted September 13, 2010 objNoob- I don't follow at all how that will work for this case? Quote Link to comment https://forums.phpfreaks.com/topic/213266-making-variable-avaliable-outside-function-global/#findComment-1110458 Share on other sites More sharing options...
PFMaBiSmAd Posted September 13, 2010 Share Posted September 13, 2010 Your title implies that the code you posted is contained within a user-written function? Is that so and if so, please post the whole function definition and an example of how you are calling it. Quote Link to comment https://forums.phpfreaks.com/topic/213266-making-variable-avaliable-outside-function-global/#findComment-1110462 Share on other sites More sharing options...
meltingpoint Posted September 13, 2010 Author Share Posted September 13, 2010 My apologies for not being more clear. It is not in a user defined function. it is simply run as you see it. The code above gives the following; Fatal error: Can't use function return value in write context in /home/XXXX/public_html/ban_ip.php on line 31 Line 31 follows the above code and is: if(strlen($set) = "")// line 31 { echo " There was not a match to the IP address to delete"; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/213266-making-variable-avaliable-outside-function-global/#findComment-1110466 Share on other sites More sharing options...
meltingpoint Posted September 13, 2010 Author Share Posted September 13, 2010 Sorry guys..............dump error. if(strlen($set = "")) ----------should be ($set == "") Threw everything off. Late and I'm calling it a day. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/213266-making-variable-avaliable-outside-function-global/#findComment-1110468 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.