0

How to call this function with url parameter

function test($str, $str_){

     if ($str == $str)
     echo "null";
     else
     echo "helloworld";
}
3
  • Please define "doesn't work". Commented Aug 22, 2011 at 23:54
  • well as said cant send variables to function test($str, $str_), i need to send 2 variables with url link to the function. Commented Aug 22, 2011 at 23:58
  • I update my reply taking in care this new information. Maybe that helps. Commented Aug 23, 2011 at 0:16

4 Answers 4

4

Use this:

switch($_GET['cmd']) {
case 'hello':
    test($_GET['cmd'],'second_parameter_value');
}

The problem is that you aren't passing a value for the second parameter. I execute previous code and prints "helloworld"

Second part:

If your intention is to call the function using two parameters in the Url you can use the follow (I'm only modifying the important parts in your own initial code):

function test($str, $str_){

     if ($str == "null")
        echo "null";
     else
        echo "helloworld";
}

switch($_GET['cmd']) {
case 'hello':
    test($_GET['cmd'],$_GET['cmd2']);
}

and the Url to call this is: execute.php?cmd=hello&cmd2=hello2

Sign up to request clarification or add additional context in comments.

5 Comments

Great! What a simple answer :D
I recommend use what DaveRandom suggest when you are developing: "turn on display_errors in php.ini, or you can put the line ini_set('display_errors',TRUE); at the top of your script" and remove this settings before final users use your scripts, to avoid show them possible important information about your scripts or server.
Power-Mosfet, answering your comment: your function is declared to receive two parameters, but you are calling the function passing to it only one. Which is your intention with the second parameter called "$str_"?
Im aware of this. I have modified my question, how to call test($str, $str_) with a url link
I modify the reply with "Second part" section, that is what you mean?
2

The function is defined with two parameters, but you have only passed in one.

This will cause a fatal error and PHP stop execution - you should get an error message to this effect, if you are not it would be advisable for you to turn on display_errors in php.ini, or you can put the line ini_set('display_errors',TRUE); at the top of your script.

Try this:

function test ($str) {
  if ($str == "null") {
    echo "null";
  } else {
    echo "helloworld";
  }
}

switch ($_GET['cmd']) {
  case 'hello':
    test($_GET['cmd']);
    break;
  default:
    echo "No match in switch structure";
} 

Comments

0
function Init()
{
   if(isset($_GET['cmd']))
     {
       $command = $_GET['cmd'];
       switch($command)
       {
           case 'hello':
              test($command);
           break;
          default:
            echo 'hello world';
       }
    } else {
      echo 'Enter Command';
    }
}
function test($cmd)
 {
   echo 'Command: ' . $cmd;
}
Init();

Maybe this can help you

3 Comments

No explanation of your very modified code for an obvious newb?
Does anyone needs explanation for this??
I don't, but since the OP didn't clock the fact that he wasn't passing a second parameter to the function, he might. No offence meant, but a couple of comments in the code cost nothing... :-)
0

Try this one

$urlParams = explode('/', $_SERVER['REQUEST_URI']);
$functionName = $urlParams[2];
$functionName($urlParams);


function func1 ($urlParams) {
    echo "In func1";
}

function func2 ($urlParams) {
    echo "In func2";
    echo "<br/>Argument 1 -> ".$urlParams[3];
    echo "<br/>Argument 2 -> ".$urlParams[4];
}

and the urls can be as below
http://domain.com/url.php/func1
http://domain.com/url.php/func2/arg1/arg2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.