0

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

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

1
  • Why would you do something like that? Commented Sep 25, 2023 at 15:56

4 Answers 4

3

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.

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

Comments

2

Pass parameters like this:

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

call function like this:

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

Comments

1

Use parse_str.

Comments

0
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

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.