From 7ecd7c616d628ec770f56357f5c48c3127d76050 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 28 Sep 2004 15:41:36 +0000 Subject: [PATCH] Merged parameter function from STABLE (and added phpdoc) --- lib/moodlelib.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index ceb7f11372..571cb7c337 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -69,6 +69,7 @@ define('HOURMINS', 60); * Ensure that a variable is set or display error * * If $var is undefined display an error message using the {@link error()} function. + * This function will soon be made obsolete by {@link parameter()} * * @param mixed $var the variable which may not be set */ @@ -85,6 +86,7 @@ function require_variable($var) { * * If $var is undefined set it (by reference), otherwise return $var. * This function is very similar to {@link nvl()} + * This function will soon be made obsolete by {@link parameter()} * * @param mixed $var the variable which may be unset * @param mixed $default the value to return if $var is unset @@ -96,6 +98,32 @@ function optional_variable(&$var, $default=0) { } } +/** + * Returns a particular value for the named variable, taken from + * POST or GET, otherwise returning a given default. + * + * This function should be used to initialise all values in a script + * that are based on parameters. Usually it will be used like this: + * + * $id = (int)parameter('id'); + * + * @param string $varname the name of the parameter variable we want + * @param mixed $default the default value to return if nothing is found + * @return mixed + */ +function parameter($varname, $default=NULL) { + + if (isset($_POST[$varname])) { // POST has precedence + return $_POST[$varname]; + } + + if (isset($_GET[$varname])) { + return $_GET[$varname]; + } + + return $default; +} + /** * Set a key in global configuration * -- 2.39.5