Do you really need to check for both isset() and empty() at the same time?
The below code can be seen too often to be ignored on this site dedicated to treating PHP delusions, being one of the most frequent cases:
if (isset($someVar) && !empty($someVar))
Being unfamiliar with PHP's loose typing, learners often mistake
if(!empty($someVar) with just
empty() will check for the empty-like values such as an empty string, a zero and such, whereas in PHP, for this purpose could be used the variable itself: thanks to the type juggling, when used in the conditional operator, any value will be cast to boolean, which will effectively check for the "emptiness" already, making a dedicated function quite useless.
So you can tell that the above snippet could be shortened to
if (isset($someVar) && $someVar)
but the most funny part is... this code being the exact definition for
!empty(), because the very purpose of this function is to tell whether a variable is either not set or "empty".
So now you can tell that checking for both
empty() is an obvious overkill and
empty() alone is more than enough, making the initial condition as simple as
On the other hand, people often use
empty() against a variable that deliberately exists. It would be overkill again, because for the already declared variable,
if(!empty($var)) would be exactly the same as just
if($var) which is way more clean and readable. Same goes for the expressions as well.
An important note: as a matter of fact, both
empty(), as well as a null coalescing operator often get misused. See an excellent comment from Hayley Watson and also an article I wrote about such a misuse. In a nutshell, you should think twice, whether you want to use such operators, or you want to have your variables more organized and take PHP to warn you about possible issues
- Relative and absolute paths, in the file system and on the web server.
- PHP error reporting
- What's wrong with popular articles telling you that foo is faster than bar?
- MVC in simpler terms or the structure of a modern web-application
- How to get a single player's rank based on the score
- Operator precedence or how does 'or die()' work.
- Do you abuse the null coalescing operator (and isset/empty as well)?
- Why should I use prepared statements if escaping is safe?
- Numerical strings comparison
- Do generators really reduce the memory usage?
- How to make Stack Overflow a nice place
- Iterating over array getting look-ahead items along
- How to debug small PHP programs
O for An SQL injection against which prepared statements won't help:
Please, exemple with select and backtrik
Ron for The Hitchhiker's Guide to SQL Injection prevention:
Terrible website. I suggest you contact the person who had designed this & get your money back....
Robery for Mysqli helper function:
I want to thank you for your sane content. Iím not a developer or trained on PHP. My introduction...
muhammad Sanusi for Mysqli SELECT query with prepared statements:
there is this query in procedural mysql, where u can compare two columns of the same rows. a am...
Globaliser for Simple yet efficient PDO wrapper:
This is really nice. I also extended PDO and PDOStatement classes, because default naming is very...