I am encoding a worksheet application for a printer company. I get a stream of forms. For each individual input field, I have to check if the $_POST
variables are set, and if so, return the value back. (In case of some error, for example, after a validation error, the user should not retype the entire form)
Code example:
if(isset($_POST['time'])&&!empty($_POST['time'])){echo $_POST['time'];}
I had to implement this about a hundred times. So I tried to figure out some kind of function to make it simple and straightforward.
Something like that:
function if_post_echo($key, $default = "") { if(isset($_POST[$key])&&!empty($_POST[$key])){ echo $_POST[$key]; }else{ echo $default; } }
But that will not work. I tried passing $_POST
to the $key
variable as follows:
if_post_echo($_POST['time']) function if_request_echo($key, $default = "") { if(isset($key)&&!empty($key)){ echo $key; }else{ echo $default; } }
And I also tried this:
function if_request_echo($key, $default = null) { return isset($_REQUEST[$key])&&!empty($_REQUEST[$key]) ? $_REQUEST[$key] : $default; }
Without any reasonable results.
Question:
How can I generate a function that searches for the necessary variable $_POST
and returns it, or if its unset then returns an empty string. And is there a way to do this for $_GET
and $_REQUEST
too? (Or just duplicate?)
source share