Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Make a simple if-isset-empty function
    primarykey
    data
    text
    <p>I'm coding a worksheet app for a printer company. I'm getting flood of forms. For every single input field I have to check if the <code>$_POST</code> variables are set, and if, so echo back the value. (In case of some error, for example after a validation error, the user shouldn't retype the whole form)</p> <p>Sample code:</p> <pre><code>if(isset($_POST['time'])&amp;&amp;!empty($_POST['time'])){echo $_POST['time'];} </code></pre> <p>I had to implement this about a hundred times. So I tried to figure out some kind of function to make this simple and readable.</p> <p>Something like this:</p> <pre><code>function if_post_echo($key, $default = "") { if(isset($_POST[$key])&amp;&amp;!empty($_POST[$key])){ echo $_POST[$key]; }else{ echo $default; } } </code></pre> <p>But this wont work. I have tried to pass in the <code>$_POST</code> for the <code>$key</code> variable like this:</p> <pre><code>if_post_echo($_POST['time']) function if_request_echo($key, $default = "") { if(isset($key)&amp;&amp;!empty($key)){ echo $key; }else{ echo $default; } } </code></pre> <p>And I also tried this:</p> <pre><code>function if_request_echo($key, $default = null) { return isset($_REQUEST[$key])&amp;&amp;!empty($_REQUEST[$key]) ? $_REQUEST[$key] : $default; } </code></pre> <p>Without any reasonable outcome.</p> <h3>The question:</h3> <p>How can I forge a function that looks for the necessary <code>$_POST</code> variable and returns it or if its unset then returns an empty string. And is there a way to do this for <code>$_GET</code> and <code>$_REQUEST</code>, too? (Or simply duplicate?)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload