devWhiz Posted April 27, 2011 Share Posted April 27, 2011 <?php function URLs(){ $a = "http://www.yahoo.com"; $b = "http://www.google.com"; $c = "http://www.phpfreaks.com"; return($b); } echo URLs($b); sleep(10000); ?> how would I get it to echo out $a without having to change the return value in the function, I want it to be like where I can put URLs($c) without having to change the return part of the function, any help is appreciated, thanks Link to comment https://forums.phpfreaks.com/topic/234847-help-with-this-function/ Share on other sites More sharing options...
cyberRobot Posted April 27, 2011 Share Posted April 27, 2011 You could change it to something like: <?php function URLs($getURL = '') { $urlToReturn = ''; switch($getURL) { case 'yahoo': $urlToReturn="http://www.yahoo.com"; break; case 'google': $urlToReturn="http://www.google.com"; break; case 'phpfreaks': $urlToReturn="http://www.phpfreaks.com"; break; } return $urlToReturn; } echo URLs('google'); sleep(10000); ?> Note that the code is untested. Link to comment https://forums.phpfreaks.com/topic/234847-help-with-this-function/#findComment-1206851 Share on other sites More sharing options...
KevinM1 Posted April 27, 2011 Share Posted April 27, 2011 You've made it too inflexible. Use an array, and pass in the array index through the argument list: function URLs($name) { $urls = array('some key' => 'http://www.yahoo.com', 'another key' => 'http://www.google.com', 'yet another key' => 'http://www.phpfreaks.com'); if (array_key_exists($name, $urls)) { return $urls[$name]; } } Of course, there are still better ways to do this where your allowed URLs aren't hard coded in the function. Link to comment https://forums.phpfreaks.com/topic/234847-help-with-this-function/#findComment-1206853 Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2011 Share Posted April 27, 2011 I would use an array of the choices and use the index/key value to pick and return the one you want. Edit: Code posted above ^^^. Link to comment https://forums.phpfreaks.com/topic/234847-help-with-this-function/#findComment-1206854 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.