Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.