PHP Make a simple if-isset-empty function

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?)

+6
source share
8 answers

Your PHP test function:

 <?php function test_req($key, $default = '') { if(isset($_REQUEST[$key]) and !empty($_REQUEST[$key])) { return $_REQUEST[$key]; } else { return $default; } } ?> 

Then in your HTML form:

 <input name="my_field" value="<?php echo htmlentities(test_req('my_field')); ?>" /> 

$_REQUEST (linked) is a PHP supercomputer that contains the POST ( $_POST ) and GET ( $_GET ) request parameters.

If you only want to capture the parameters of the POST request, this will be:

 <?php function test_req($key, $default = '') { if(isset($_POST[$key]) and !empty($_POST[$key])) { return $_POST[$key]; } else { return $default; } } ?> 

For instance.

+7
source

If you have a large number of fields, I would suggest that you also use an array of default values:

 $defaults = array( "time" => "default", "name" => "enter name here", "text..." => "...", ); $fields = array_filter($_POST) + $defaults; 

$fields will then contain a list of form values ​​with POST data or a predefined default value. No isset, see?

massive page of file_page , in particular: if no callback is provided, all input entries equal to FALSE will be deleted. There is some way to explain the work behind this decision.

+6
source

This should work:

 function if_post_echo($key, $default = ''){ if(isset($_POST[$key]) AND !empty($_POST[$key]){ echo $_POST[$key]; } echo $default; } 

If you are having problems, I recommend that you try var_dump($_POST) or print_r($_POST) to see if everything is placed correctly.

+2
source

Just to note that this is redundant:

 isset($_POST[$key]) && !empty($_POST[$key]) 

An unset variable will always be "empty", therefore isset() implied in your call to empty() .

For your logic, you can achieve the same result, simply:

 !empty($_POST[$key]) 
+2
source

Your first function works fine for me.

Why do you think this does not work?

However, the best option would be

 function _post($key, $default = "") { if(isset($_POST[$key])){ return $_POST[$key]; }else{ return $default; } } 

To use it:

 echo $_post($key); // You could define the message as a second parameter. 
+1
source
 function requireArray( $array, $required ) { foreach( $required as $k=>$v ) { if ( !isset($array[$k]) || empty($array[$k]) ) return false; } return true; } #call like this: requireArray($_POST, array('time', 'username', 'foo')); 

If you want to know specifically:

 function missingFrom( $array, $required ) { $r = array(); foreach( $required as $k ) { if ( !isset($array[$k]) || empty($array[$k]) ) $r[] = $k; } return $r; } 

Called as the previous function.

0
source

Your method works fine:

 function if_post_echo($key, $default = "") { if(isset($_POST[$key])&&!empty($_POST[$key])){ echo $_POST[$key]; }else{ echo $default; } } 

I made a simple input with a name test, and the form method - POST and using echo if_post_echo('test'); .

He placed on the page what was in the text box.

0
source

This feature is added in PHP 7 as the "Null Coalesce Operator" using two question marks:

echo ($_GET['message'] ?? 'default-if-not-set');

https://wiki.php.net/rfc/isset_ternary

0
source

Source: https://habr.com/ru/post/885662/


All Articles