Re: if (empty versus if (isset
| From: | Tedd Sperling | Date: | Wed, 20 Feb 2013 16:32:04 +0000 |
| Subject: | Re: if (empty versus if (isset | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320259@lists.php.net to get a copy of this message | ||
On Feb 20, 2013, at 9:10 AM, Jim Giner <jim.giner@albanyhandball.com> wrote:
> Basically it tells a savvy programmer whether or not his logic has caused the var in question
> to "exist". Many times it is important simply to know that, not what the var contains,
> which can lead to an error in processing.
>
> The isset() will tell you that "yes, I have this variable", letting you then
> correctly interpret the contents. If a $_POST var is not set (meaning the user made no input to
> it), the use of empty() will insist on telling you that the var is empty even tho it really was not
> provided by the user (assuming that you don't get an error msg for having an invalid index in
> the POST array).
>
> They seem to be needlessly redundant, but in fact do provide knowledge for those seeking it.
>
That's one of the reason why I recommend using a Ternary Operator to check the POST array, such
as:
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
That way, you never encounter an error looking for something that is not there.
Cheers,
tedd
____________________
tedd@sperling.com
http://sperling.com