Sam3
Sam3

Reputation: 73

php function parameters

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

student('name=Nick&roll=1234');

Upvotes: 0

Views: 192

Answers (4)

Oliver A.
Oliver A.

Reputation: 2900

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

Upvotes: 0

Daff
Daff

Reputation: 44215

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.

Upvotes: 3

Diablo
Diablo

Reputation: 3418

Pass parameters like this:

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

call function like this:

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

Upvotes: 2

Kalessin
Kalessin

Reputation: 2302

Use parse_str.

Upvotes: 1

Related Questions