开发者

php function parameters

How to write a php function in order to pass parameters like this

student('name=Nick&a开发者_高级运维mp;roll=1234');


If your format is URL encoded you can use parse_str to get the variables in your functions scope:

function student($args)
{
    parse_str($args);
    echo $name;
    echo $roll;
}

Although, if this string is the scripts URL parameters you can just use the $_GET global variable.


Pass parameters like this:

student($parameter1, $parameter2){
//do stuff
return $something;
}

call function like this:

student($_GET['name'], $_GET['roll']);


Use parse_str.


function foo($paraString){

    try{
    $expressions = explode('&',$paraString);
    $args = array();
    foreach($expressions as $exoression){
      $kvPair =  explode('=',$exoression);
      if(count($kvPair !=2))throw new Exception("format exception....");
      $args[$kvPair[0]] = $kvPair[1];
    }
    ....

Edit: that wayyou can do it manually. If you just want to get something out of a querrystring there are already functions to get the job done

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜