tags:

views:

38

answers:

2

I have written a function:

function url_query(){
  if (is_numeric($_GET['cmd'])) {
    $get = $_GET['cmd'];
  }
  return $get;
}

but I want that this function be global. This function works only for eg.

index.php?cmd=...

Can I revise this function to use for index.php?page=... and index.php?catID=... etc? Thanks

A: 

Is this what you are looking for?

function url_query($queryParam){ 
   if (is_numeric($_GET[$queryParam])) {
     $get = $_GET[$queryParam];
  }
 return $get;

}

Or with an array:

function url_query($queryParams){ 

 $get = array();

 foreach($queryParams as $queryParam){ 

    if (is_numeric($_GET[$queryParam])) {
     $get[] = $_GET[$queryParam];
    }
  }
 return $get;
}

Possible calls:

$value = url_query('cmd');

$values = url_query(array('cmd','catID'));
andreas
Don’t forget to use `isset` or `array_key_exists`!
Gumbo
@andreas;Thanks alot
phpExe
A: 
function url_query($param){
  $get = null;
  if (isset($_GET[$param]) && is_numeric($_GET[$param])) {
    $get = $_GET[$param];
  }
  return $get;
}

I've ensured that the return value is null if the parameter is not numeric, so that client code has to explicitly check for that case.

Flavius Stef
Umm, if you return a variable that never gets defined, it is already NULL, you don't have to define it as NULL...
animuson
@Flavius; I have tried :function url_query($cmd){ if (is_numeric($_GET['$cmd'])) { $cmd = $_GET['$cmd']; } return $cmd;}And this was not worked. I think that the problem because double qouetes.Thanks alot.
phpExe
Well, actually I don't know what the return value would be. But it definitely would raise an E_NOTICE error. Plus, it would rely on a language "hack" to get the work done, whereas I prefer code to be obvious.
Flavius Stef
@phpExe You added some extra apostrophesIt's $_GET[$cmd], not $_GET['$cmd'].
Flavius Stef
@Flavius Stef, you are right, Thanks for help
phpExe