<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:series="http://unfoldingneurons.com/" > <channel><title>Comments on: Avoiding Notices: When to Use isset() and empty()</title> <atom:link href="http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/feed/" rel="self" type="application/rss+xml" /><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/</link> <description>The personal blog of Brandon Savage. Contains entries of a personal and professional nature focusing on PHP, Apple, LAMP, MySQL and Washington, DC.</description> <lastBuildDate>Thu, 29 Jul 2010 19:10:45 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>By: Dominik Bonsch</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1775</link> <dc:creator>Dominik Bonsch</dc:creator> <pubDate>Wed, 28 Oct 2009 15:28:13 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1775</guid> <description>@Steven Albarracin you checkIfNotSet() has a bug ;-)isset( $var ) in your function is not the same as using isset() in the code, cause of the parameter $var in your function $var always exists.Maybe you should increase your error level for developing.In my opionion there is no need for empty(). All you need ist isset() is_null() or trim($var) === &#039;&#039;. This three methods are more specific than empty() and you get exactly the information you need, without any bug risk cause of 0 = false coversions etc..</description> <content:encoded><![CDATA[<p>@Steven Albarracin you checkIfNotSet() has a bug ;-)</p><p>isset( $var ) in your function is not the same as using isset() in the code, cause of the parameter $var in your function $var always exists.</p><p>Maybe you should increase your error level for developing.</p><p>In my opionion there is no need for empty().<br /> All you need ist isset() is_null() or trim($var) === &#8221;.<br /> This three methods are more specific than empty() and you get exactly the information you need, without any bug risk cause of 0 = false coversions etc..</p> ]]></content:encoded> </item> <item><title>By: Matthew Purdon</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1516</link> <dc:creator>Matthew Purdon</dc:creator> <pubDate>Thu, 08 Oct 2009 04:17:10 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1516</guid> <description>There are so many times when I wished that my fellow developers had developed with all errors being reported. You should always know what type your variables should be and use the appropriate test against them. Relying on a magic catch-all function not only allows ambiguity to creep in, it can prevent other developers from coding against your interfaces properly.</description> <content:encoded><![CDATA[<p>There are so many times when I wished that my fellow developers had developed with all errors being reported. You should always know what type your variables should be and use the appropriate test against them. Relying on a magic catch-all function not only allows ambiguity to creep in, it can prevent other developers from coding against your interfaces properly.</p> ]]></content:encoded> </item> <item><title>By: Steven Albarracin</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1422</link> <dc:creator>Steven Albarracin</dc:creator> <pubDate>Thu, 01 Oct 2009 14:43:14 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1422</guid> <description>I&#039;ve actually used my own function which I believe addresses all the &quot;empty/null/blank&quot; values.function checkIfNotSet($var,$zerosAreEmpty = false){ if($zerosAreEmpty) $empty = empty($var); $var = trim($var); if(!isset($var) &#124;&#124; $empty &#124;&#124;$var == &quot;&quot;){ echo &quot;Is Not Set&quot;; }else{ echo &quot;Is set&quot;; } }$var = 0; checkIfNotSet($var);the trim function eliminates the use of the is_null function and also &quot; &quot;Hope this will come in useful for someone out there.</description> <content:encoded><![CDATA[<p>I&#8217;ve actually used my own function which I believe addresses all the &#8220;empty/null/blank&#8221; values.</p><p>function checkIfNotSet($var,$zerosAreEmpty = false){<br /> if($zerosAreEmpty) $empty = empty($var);<br /> $var = trim($var);<br /> if(!isset($var) || $empty ||$var == &#8220;&#8221;){<br /> echo &#8220;Is Not Set&#8221;;<br /> }else{<br /> echo &#8220;Is set&#8221;;<br /> }<br /> }</p><p>$var = 0;<br /> checkIfNotSet($var);</p><p>the trim function eliminates the use of the is_null function and also &#8221; &#8221;</p><p>Hope this will come in useful for someone out there.</p> ]]></content:encoded> </item> <item><title>By: Brandon Savage</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1320</link> <dc:creator>Brandon Savage</dc:creator> <pubDate>Thu, 24 Sep 2009 11:51:22 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1320</guid> <description>Juilian, I don&#039;t usually allow flame wars...I think they&#039;re bad for business. ;-)I learned about notices when my web host turned notices on for code that was deployed on their servers. The practical use of eliminating notices for me was that they were scattered throughout my code - and that was bad.PHP is very fault tolerant, and does a good job at mitigating a number of the issues that very junior programmers introduce. That said, I do think it&#039;s wise to teach standards to new developers as much as is possible.Is reducing or eliminating notices a critical component of writing PHP code? No. PHP code can work for years without eliminating notices. My goal in writing this article was two-fold: first, to identify a solution to a common problem, and second, to make new developers aware of the fact that things such as notices exist in PHP, hopefully prompting them to learn some more about it.The next article in this series is a PDO primer. It discusses using PDO, and the flexibility that PDO provides. I didn&#039;t want to spend time discussing the differences between =, == and ===, or really cover anything that&#039;s explicitly talked about in the PHP manual; instead, I wanted to go a little deeper and expose new programmers to tools they might not have otherwise seen.To that end, I&#039;m pretty sure that this article was boring as hell for most advanced developers...at least, once I fixed it. ;-)As for bad practices, they should *never* be taught. That&#039;s just the way it is. Teaching bad practices results in bad practices, and I think that a gentle correction is appropriate. But we should never encourage bad behavior.</description> <content:encoded><![CDATA[<p>Juilian, I don&#8217;t usually allow flame wars&#8230;I think they&#8217;re bad for business. ;-)</p><p>I learned about notices when my web host turned notices on for code that was deployed on their servers. The practical use of eliminating notices for me was that they were scattered throughout my code &#8211; and that was bad.</p><p>PHP is very fault tolerant, and does a good job at mitigating a number of the issues that very junior programmers introduce. That said, I do think it&#8217;s wise to teach standards to new developers as much as is possible.</p><p>Is reducing or eliminating notices a critical component of writing PHP code? No. PHP code can work for years without eliminating notices. My goal in writing this article was two-fold: first, to identify a solution to a common problem, and second, to make new developers aware of the fact that things such as notices exist in PHP, hopefully prompting them to learn some more about it.</p><p>The next article in this series is a PDO primer. It discusses using PDO, and the flexibility that PDO provides. I didn&#8217;t want to spend time discussing the differences between =, == and ===, or really cover anything that&#8217;s explicitly talked about in the PHP manual; instead, I wanted to go a little deeper and expose new programmers to tools they might not have otherwise seen.</p><p>To that end, I&#8217;m pretty sure that this article was boring as hell for most advanced developers&#8230;at least, once I fixed it. ;-)</p><p>As for bad practices, they should *never* be taught. That&#8217;s just the way it is. Teaching bad practices results in bad practices, and I think that a gentle correction is appropriate. But we should never encourage bad behavior.</p> ]]></content:encoded> </item> <item><title>By: Logan Bailey</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1309</link> <dc:creator>Logan Bailey</dc:creator> <pubDate>Thu, 24 Sep 2009 06:47:12 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1309</guid> <description>Nice article, another good article about this is: http://www.phpro.org/articles/Difference-Between-isset-empty-is-null.htmlIt also contains the is_null() function as well.</description> <content:encoded><![CDATA[<p>Nice article, another good article about this is:<br /> <a href="http://www.phpro.org/articles/Difference-Between-isset-empty-is-null.html" rel="nofollow">http://www.phpro.org/articles/Difference-Between-isset-empty-is-null.html</a></p><p>It also contains the is_null() function as well.</p> ]]></content:encoded> </item> <item><title>By: Julian Egelstaff</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1307</link> <dc:creator>Julian Egelstaff</dc:creator> <pubDate>Thu, 24 Sep 2009 02:56:35 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1307</guid> <description>OK, maybe this will start a huge flame war reigning down on my head, but here&#039;s another take on this...If you&#039;re an absolute beginner, one of the great things about PHP is that it really doesn&#039;t care much about imperfect code.  Just like HTML forgives all kinds of heresy, such as the case of properties in tags, and whether there are &quot; &quot; around values, and even whether there are proper closing tags or not on certain elements, PHP forgives lots of sloppy coding practices and still gives you a web page in your web browser despite what you&#039;re doing &quot;wrong&quot;.I think this is one of the most important features of PHP for beginners.  It ramps up the feedback loop and makes the language really easy to tinker with and learn and teach, without having to get into more abstract programming issues like whether variables are set or null or something else.If you think of it from the perspective of a non-programmer coming to PHP from HTML, these things that are bad coding practices to us, are what make PHP so accessible to them.And these things have produced a lot of bad code.  And given PHP a bad name in some circles.  But that&#039;s maybe a whole other story.I do think that anyone who is serious about using the language needs to learn about these issues and incorporate them into their coding toolbox.  But I think it&#039;s wrong to declare that not using isset is wrong (and I&#039;m not necessarily accusing the original post of saying that, just sensing it&#039;s a perspective lying around here).  If it were truly wrong, PHP would throw an error not a notice or warning or whatever it throws when you don&#039;t do use it.I think this idea is related a lot to the issues raised in &quot;The Beginner Pattern&quot; blog post at http://blog.phpdeveloper.org/?p=198.  Introducing newcommers to the language takes a different perspective than the perspective that seasoned coders have, especially when a lot of newcommers to PHP do not have a coding background.I am very interested to know what other people think of this, and especially how you would go about introducing this particular &quot;isset&quot; concept to a PHP newcommer.  Should &quot;bad habits&quot; never be taught, or conversely at what point do you explain that the easy way you&#039;ve been doing things up till now is actually not the best way to do things?Maybe it&#039;s not very insightful, but I would say that bad habits are just fine to start with, and the dividing line is when it starts to matter to the kind of things you&#039;re doing with the code.  The really hard part is how can a beginner identify that line?  Maybe the most important lesson anyone can give to people starting with PHP is that they should simply be aware that there are many levels to climb before they master the language, and  not to get set in their ways early.--Julian</description> <content:encoded><![CDATA[<p>OK, maybe this will start a huge flame war reigning down on my head, but here&#8217;s another take on this&#8230;</p><p>If you&#8217;re an absolute beginner, one of the great things about PHP is that it really doesn&#8217;t care much about imperfect code.  Just like HTML forgives all kinds of heresy, such as the case of properties in tags, and whether there are &#8221; &#8221; around values, and even whether there are proper closing tags or not on certain elements, PHP forgives lots of sloppy coding practices and still gives you a web page in your web browser despite what you&#8217;re doing &#8220;wrong&#8221;.</p><p>I think this is one of the most important features of PHP for beginners.  It ramps up the feedback loop and makes the language really easy to tinker with and learn and teach, without having to get into more abstract programming issues like whether variables are set or null or something else.</p><p>If you think of it from the perspective of a non-programmer coming to PHP from HTML, these things that are bad coding practices to us, are what make PHP so accessible to them.</p><p>And these things have produced a lot of bad code.  And given PHP a bad name in some circles.  But that&#8217;s maybe a whole other story.</p><p>I do think that anyone who is serious about using the language needs to learn about these issues and incorporate them into their coding toolbox.  But I think it&#8217;s wrong to declare that not using isset is wrong (and I&#8217;m not necessarily accusing the original post of saying that, just sensing it&#8217;s a perspective lying around here).  If it were truly wrong, PHP would throw an error not a notice or warning or whatever it throws when you don&#8217;t do use it.</p><p>I think this idea is related a lot to the issues raised in &#8220;The Beginner Pattern&#8221; blog post at <a href="http://blog.phpdeveloper.org/?p=198" rel="nofollow">http://blog.phpdeveloper.org/?p=198</a>.  Introducing newcommers to the language takes a different perspective than the perspective that seasoned coders have, especially when a lot of newcommers to PHP do not have a coding background.</p><p>I am very interested to know what other people think of this, and especially how you would go about introducing this particular &#8220;isset&#8221; concept to a PHP newcommer.  Should &#8220;bad habits&#8221; never be taught, or conversely at what point do you explain that the easy way you&#8217;ve been doing things up till now is actually not the best way to do things?</p><p>Maybe it&#8217;s not very insightful, but I would say that bad habits are just fine to start with, and the dividing line is when it starts to matter to the kind of things you&#8217;re doing with the code.  The really hard part is how can a beginner identify that line?  Maybe the most important lesson anyone can give to people starting with PHP is that they should simply be aware that there are many levels to climb before they master the language, and  not to get set in their ways early.</p><p>&#8211;Julian</p> ]]></content:encoded> </item> <item><title>By: Cameron Junge</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1305</link> <dc:creator>Cameron Junge</dc:creator> <pubDate>Wed, 23 Sep 2009 22:43:57 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1305</guid> <description>A gotcha that not many people seem to either a) know about, or b) talk about is that a string containing 0 (ie. &quot;0&quot;) is also considered empty. Eg: $var = &quot;0&quot;; echo (empty($var) ? &#039;yes&#039; : &#039;no&#039;);Outputs: yes</description> <content:encoded><![CDATA[<p>A gotcha that not many people seem to either a) know about, or b) talk about is that a string containing 0 (ie. &#8220;0&#8243;) is also considered empty.<br /> Eg:<br /> $var = &#8220;0&#8243;;<br /> echo (empty($var) ? &#8216;yes&#8217; : &#8216;no&#8217;);</p><p>Outputs: yes</p> ]]></content:encoded> </item> <item><title>By: Richard Lynch</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1300</link> <dc:creator>Richard Lynch</dc:creator> <pubDate>Wed, 23 Sep 2009 18:12:51 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1300</guid> <description>Please don&#039;t use empty().I can&#039;t count the number of bugs and logic errors that I&#039;ve run across over the years because of it.Use isset() and then test the value with strlen() or == 0 or whatever is appropriate for the data type.Plus, the actual definition of what is/isn&#039;t empty changed with every major release so far.  Not a very reliable function.</description> <content:encoded><![CDATA[<p>Please don&#8217;t use empty().</p><p>I can&#8217;t count the number of bugs and logic errors that I&#8217;ve run across over the years because of it.</p><p>Use isset() and then test the value with strlen() or == 0 or whatever is appropriate for the data type.</p><p>Plus, the actual definition of what is/isn&#8217;t empty changed with every major release so far.  Not a very reliable function.</p> ]]></content:encoded> </item> <item><title>By: Brandon Savage</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1282</link> <dc:creator>Brandon Savage</dc:creator> <pubDate>Wed, 23 Sep 2009 13:29:45 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1282</guid> <description>I agree. I wanted to give beginners a method for using one or the other properly (this post is pretty fundamental), but I think you&#039;re right that we need a variable_defined() function.The PHP Internals list is waiting for your patch. ;-)</description> <content:encoded><![CDATA[<p>I agree. I wanted to give beginners a method for using one or the other properly (this post is pretty fundamental), but I think you&#8217;re right that we need a variable_defined() function.</p><p>The PHP Internals list is waiting for your patch. ;-)</p> ]]></content:encoded> </item> <item><title>By: Daniel Andre Eikeland</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1275</link> <dc:creator>Daniel Andre Eikeland</dc:creator> <pubDate>Wed, 23 Sep 2009 09:52:05 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1275</guid> <description>The point I usually bring up whenever a discussion about the odd isset() behaviour surfaces, is that PHP lacks a proper &quot;variable_defined()&quot; method - isset() tries to do both isset() *and* variable_defined(), which causes problems.It&#039;s really odd that there isn&#039;t such a function, seeing as we already have defined() (for constants), and property_exists() (for class properties/class variables).</description> <content:encoded><![CDATA[<p>The point I usually bring up whenever a discussion about the odd isset() behaviour surfaces, is that PHP lacks a proper &#8220;variable_defined()&#8221; method &#8211; isset() tries to do both isset() *and* variable_defined(), which causes problems.</p><p>It&#8217;s really odd that there isn&#8217;t such a function, seeing as we already have defined() (for constants), and property_exists() (for class properties/class variables).</p> ]]></content:encoded> </item> <item><title>By: Brandon Savage</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1274</link> <dc:creator>Brandon Savage</dc:creator> <pubDate>Wed, 23 Sep 2009 09:49:13 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1274</guid> <description>Bruce, I added a note about null; you&#039;re right that it does create problems.As for the amp;amp;amp; issue that&#039;s due to the funky nature of my syntax highlighter. I resolved that. For some reason, I was unable to resolve the emptyempty issue, though; instead I replaced the examples with unhighlighted divs in the meantime.I&#039;m not sure what code examples would be better. I&#039;ll think about it this morning and try to post something up later on in the day.</description> <content:encoded><![CDATA[<p>Bruce, I added a note about null; you&#8217;re right that it does create problems.</p><p>As for the amp;amp;amp; issue that&#8217;s due to the funky nature of my syntax highlighter. I resolved that. For some reason, I was unable to resolve the emptyempty issue, though; instead I replaced the examples with unhighlighted divs in the meantime.</p><p>I&#8217;m not sure what code examples would be better. I&#8217;ll think about it this morning and try to post something up later on in the day.</p> ]]></content:encoded> </item> <item><title>By: Daniel Andre Eikeland</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1271</link> <dc:creator>Daniel Andre Eikeland</dc:creator> <pubDate>Wed, 23 Sep 2009 07:49:09 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1271</guid> <description>&quot;If the variable has been defined in the global scope, in any form, this will return true. Otherwise, it will return false.&quot;Yes, this is the ideal way it works, but as Bruce mentions above, it simply isn&#039;t true - and is a source of much unecessary headache.Here&#039;s the description from the documentation: &quot;Determine if a variable is set and is not NULL.&quot;Also agree with Bruce that your examples aren&#039;t very good.</description> <content:encoded><![CDATA[<p>&#8220;If the variable has been defined in the global scope, in any form, this will return true. Otherwise, it will return false.&#8221;</p><p>Yes, this is the ideal way it works, but as Bruce mentions above, it simply isn&#8217;t true &#8211; and is a source of much unecessary headache.</p><p>Here&#8217;s the description from the documentation:<br /> &#8220;Determine if a variable is set and is not NULL.&#8221;</p><p>Also agree with Bruce that your examples aren&#8217;t very good.</p> ]]></content:encoded> </item> <item><title>By: Bruce Weirdan</title><link>http://www.brandonsavage.net/avoiding-notices-when-to-use-isset-and-empty/#comment-1268</link> <dc:creator>Bruce Weirdan</dc:creator> <pubDate>Wed, 23 Sep 2009 06:25:26 +0000</pubDate> <guid isPermaLink="false">http://www.brandonsavage.net/?p=735#comment-1268</guid> <description>Brandon, your description of isset() is far from true because you missed how it treats nulls. isset() returns false on a variable that has been set to null (but otherwise exist in the scope, does not throw notices on access, etc). I&#039;m afraid the only sure way to test for variable existence in the scope is using array_key_exists + get_defined_vars()And your last code snippet just doesn&#039;t make sense, because strpos would never return null and you are assigning strpos result to $var, thus defining it. Taking out isset($var) check from there changes nothing - therefore it has no reason to be there in the first place.BTW, both second and third snippets have emptyempty() instead of single empty(), and all of the examples have funky strings of amp;amp;amp;s</description> <content:encoded><![CDATA[<p>Brandon, your description of isset() is far from true because you missed how it treats nulls. isset() returns false on a variable that has been set to null (but otherwise exist in the scope, does not throw notices on access, etc). I&#8217;m afraid the only sure way to test for variable existence in the scope is using array_key_exists + get_defined_vars()</p><p>And your last code snippet just doesn&#8217;t make sense, because strpos would never return null and you are assigning strpos result to $var, thus defining it. Taking out isset($var) check from there changes nothing &#8211; therefore it has no reason to be there in the first place.</p><p>BTW, both second and third snippets have emptyempty() instead of single empty(), and all of the examples have funky strings of amp;amp;amp;s</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using apc (user agent is rejected)
Database Caching 45/52 queries in 0.030 seconds using disk

Served from: www.brandonsavage.net @ 2010-09-08 13:04:46 -->