Last active
December 27, 2015 11:18
-
-
Save Gisleburt/7317082 to your computer and use it in GitHub Desktop.
Get a variable from request or url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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