Re: Undefined Variables
| From: | Stuart Dallas | Date: | Thu, 14 Feb 2013 21:01:37 +0000 |
| Subject: | Re: Undefined Variables | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320222@lists.php.net to get a copy of this message | ||
On 14 Feb 2013, at 20:57, Al <news@ridersite.org> wrote:
> On 2/14/2013 1:54 PM, Stuart Dallas wrote:
>> Sorry for the top post!
>>
>> I don't know numbers, but my gut instinct is that the cycles wasted raising the notice
>> (it gets raised even if it goes nowhere so turning display and log doesn't remove the hit
>> completely) are better spent executing defensive code.
>>
>> There is no reason, ever, that production code should raise notices about which you
>> don't care. If PHP is telling you something might be wrong, something might be wrong! And if
>> you're investigating the code already, figure out what's happening and deal with it
>> properly.
>>
>> Only lazy and/or developers ignore notices. If you're one of them and this statement
>> offends you, you probably know it's right!
>>
>> -Stuart
>>
>
> I agree with Stuart.
>
> To minimize the overhead of testing every possible undefined variable with isset(), I assign
> them at the top of the page which uses them. e.g.,
>
> $userInstrHtmlSizeWarning = false;
> $currentUserRecArray = array();
> if(!isset($_SESSION['pwPassed']))$_SESSION['pwPassed'] = false;
>
> I also have this snippet at the top of my app config file.
>
> if(true){ // TRUE for debug only
> ini_set("display_errors", "on"); //use off if users will see them
> error_reporting(E_ALL)
> $error_reporting = '<span style="color:red">Error display and logging
> on</span> ';
> }
> else $error_reporting=null;
>
> I put this at a convenient place on the page so I don't forget to turn off the error
> reporting when the code goes live.
>
> if($error_reporting) echo $error_reporting;
I've used the following method for accessing elements of the global arrays for a long time. It
saves a lot of typing!
http://stut.net/2011/04/12/php-snippet-array-element-access/
Note that if you're using PHP5 you should remove the & before the $a in the function
definition.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/