Jump to content

Help with this function...


devWhiz

Recommended Posts


<?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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.