/ Published in: PHP
I don't want to write function empty() again and again.
Expand |
Embed | Plain Text
Comments
Subscribe to comments
You need to login to post a comment.
I don't want to write function empty() again and again.
Subscribe to comments
You need to login to post a comment.
This function is nonsensical.
The empty() function will return true if the POST or GET arrays are empty. There is no need to loop over the values like this.
I don't directly loop POST or GET. I use like this.
$val1 = $POST['val1']; $val2 = $POST['val2']; $val4 = $_POST['val3'];
$arr = array($val1, $val2, $val3);
if(check_empty($arr) === false) { // codes here }
Any advice?
Okay, but I am not quite sure in what situation a function like that would be useful.
If you want to know if an array is empty simply use:
If you want to know if an array is NOT empty. ("!" means "not" and basically flips boolean values on their heads, so that false becomes true and vice versa):
if you want to know if an array CONTAINS one or more empty values.
In PHP if a value can be thought of as empty it is considered to be of the type null: 0 (zero), false (boolean), "" (empty string), array() (empty array), 0.0 (empty decimal number), and so on, are all considered to be null. Be careful, though, to not use === in cases like this, because === also checks for the variable type and then the aforementioned values will not equal null.
Also, you should take a look at PHP's array functions. It is rare that you need to define your own functions for use with arrays.
Anyways, so you can do something like this: