Re: [PHP] if (empty versus if (isset

[email protected] (Tedd Sperling)
Newsgroups php.general
Message-ID <[email protected]>
On Feb 20, 2013, at 9:10 AM, Jim Giner <[email protected]> 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


____________________
[email protected]
http://sperling.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.