How t开发者_如何学Pythono call this function with url parameter
function test($str, $str_){
if ($str == $str)
echo "null";
else
echo "helloworld";
}
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
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";
}
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
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
精彩评论