Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Whats the point of isset? Can I just use: if (!$_POST[var]) instead?

nbsp point
0
10 Posted

Whats the point of isset? Can I just use: if (!$_POST[var]) instead?

0

The problem with using (!$_POST[‘var’]) as an expression is that you are asking the parser to evaluate the expression as a boolean. If $_POST[‘var’] isn’t a boolean (and it won’t be, because all POST or GET variables are treated as strings unless you cast them) then the parser does an implicit cast to boolean while evaluating your expression. This is fine for most cases, but let’s say that $_POST[‘var’] *is* set to either an empty string (“”) or a string containing zero (“0”). The (!$_POST[‘var’]) test will fail, because both of these are evaluated as FALSE when cast as a boolean. That’s the reason that isset() is a little more general purpose. If you aren’t worried about missing an empty string or a string containing “0” then you can use the (!$_POST[‘var’]) method. For information is in the php manual at: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123