![]() ![]() Initialize your variables at the top of a block of code in which you're going to use them. There's no need to check whether $bar or $baz are set inside the function because you just set them, all you need to worry about is if their value evaluates to true or false (or whatever else). Depending on the situation there are different ways to do that:įunction arguments: function foo ($bar, $baz = null) You will always need isset or empty somewhere in your code, the only way to reduce their occurrence is to initialize your variables properly. Turning error reporting off is for production environments only, to avoid information leakage and provide a better user experience even in the face of buggy code. If you get a warning "You're trying to work with something that doesn't exist!", your reaction should be "Oops, my bad, let me fix that ASAP." How else are you going to tell the difference between "variables that work just fine undefined" and honestly wrong code that may lead to serious errors? This is also the reason why you always, always, develop with error reporting turned to 11 and keep plugging away at your code until not a single NOTICE is issued. These warnings are there to help you, not to annoy you. The fact that PHP allows you to do it doesn't mean you should. Trying to access non-existent variables should never ever happen, other languages balk at this at compile time. Having hundreds of points in your code that regularly try to use non-existent variables sounds like a rather badly structured program. ![]() IMHO you should think about not just making the app "E_NOTICE compatible", but restructuring the whole thing. ![]() For those interested, I have expanded this topic into a small article, which provides the below information in a somewhat better structured form: The Definitive Guide To PHP's isset And empty ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |