Skip to content

Instantly share code, notes, and snippets.

@Gisleburt
Last active December 27, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gisleburt/7317082 to your computer and use it in GitHub Desktop.
Save Gisleburt/7317082 to your computer and use it in GitHub Desktop.
Get a variable from request or url
/**
* Get the given variable from $_REQUEST or from the url
* @param string $variableName
* @param mixed $default
* @return mixed|null
*/
function getParam($variableName, $default = null) {
// Was the variable actually part of the request
if(array_key_exists($variableName, $_REQUEST))
return $_REQUEST[$variableName];
// Was the variable part of the url
$urlParts = explode('/', preg_replace('/\?.+/', '', $_SERVER['REQUEST_URI']));
$position = array_search($variableName, $urlParts);
if($position !== false && array_key_exists($position+1, $urlParts))
return $urlParts[$position+1];
return $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment