Avoiding Notices: When to Use isset() and empty()

One of the most confusing things for new programmers (and it even trips me up sometimes) is how to test for boolean conditions in code. As developers, we want to develop code that never emits notices or warnings, and PHP gets a bit antsy when we develop code that utilizes uninitialized variables. Lucky for us, PHP makes it easy to test for these variables without getting these notices.

PHP (like most languages) evaluates a logical argument left to right. For an AND condition, both conditions have to be true; PHP stops evaluating if it finds any condition untrue; that means that we can use isset() or empty() as the first parameter in an if statement, and avoid raising notices.


Wednesday, September 23rd, 2009 @ 1:00 am | Comment (13) | Categories: Best Practices, General PHP
Tags: ,

Copyright © 2024 by Brandon Savage. All rights reserved.