Please help clear up some php error notice issue? [message #180036] |
Sat, 29 December 2012 03:12 |
r.mariotti
Messages: 17 Registered: December 2011
Karma: 0
|
Junior Member |
|
|
Since upgrading to version 5.4.x I've been seeing a common error
complaining about undefined variables.
I know that the E_Notice can be suppressed but I want to eliminate the
cause as well.
To address this issue I've added statements at the beginning of the
code to determine if the named variable is set and if not to set it to
null. This should then satisfy this complaint.
Here is what this code typically looks like:
if (!isset($Usage)) { $Usage = ''; }
if (!isset($_userid)) { $_userid = ''; }
if (!isset($_userno)) { $_userno = ''; }
if (!isset($V0)) { $V0 = ''; }
So my understanding is that references to any/all of the above defined
variables should NOT cause that error notice. However, it seems to
continue.
Without going bonkers would some of you who understand this somewhat
syntactical issue please try to explain the best way to handle this
issue so me and I'm sure many, many other php developers can once and
for all rid outselves of this so common issue?
Thanks.
|
|
|
Re: Please help clear up some php error notice issue? [message #180038 is a reply to message #180036] |
Sat, 29 December 2012 04:32 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 12/28/2012 10:12 PM, BobMCT wrote:
> Since upgrading to version 5.4.x I've been seeing a common error
> complaining about undefined variables.
> I know that the E_Notice can be suppressed but I want to eliminate the
> cause as well.
>
> To address this issue I've added statements at the beginning of the
> code to determine if the named variable is set and if not to set it to
> null. This should then satisfy this complaint.
>
> Here is what this code typically looks like:
>
> if (!isset($Usage)) { $Usage = ''; }
> if (!isset($_userid)) { $_userid = ''; }
> if (!isset($_userno)) { $_userno = ''; }
> if (!isset($V0)) { $V0 = ''; }
>
> So my understanding is that references to any/all of the above defined
> variables should NOT cause that error notice. However, it seems to
> continue.
>
> Without going bonkers would some of you who understand this somewhat
> syntactical issue please try to explain the best way to handle this
> issue so me and I'm sure many, many other php developers can once and
> for all rid outselves of this so common issue?
>
> Thanks.
>
Well, first of all, if this is the top of the code, then you really
don't need the if statement - all non-superglobals (i.e. not $_GET,
$_SESSION, etc.) will be undefined. The only exception would be if
you're running with register_globals enabled in your PHP configuration,
which it should NOT be.
However, you didn't show us the code that's causing your problem, so
there's not much more we can do to help you.
Try posting the entire code that's failing with the applicable messages
and we can take a closer look at it.
P.S. Congratulations on trying to write good code. Too many programmers
are sloppy in their writing, creating potential problems later.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Please help clear up some php error notice issue? [message #180039 is a reply to message #180036] |
Sat, 29 December 2012 04:37 |
Richard Yates
Messages: 86 Registered: September 2013
Karma: 0
|
Member |
|
|
On Fri, 28 Dec 2012 22:12:53 -0500, BobMCT <r(dot)mariotti(at)fdcx(dot)net>
wrote:
> Since upgrading to version 5.4.x I've been seeing a common error
> complaining about undefined variables.
> I know that the E_Notice can be suppressed but I want to eliminate the
> cause as well.
>
> To address this issue I've added statements at the beginning of the
> code to determine if the named variable is set and if not to set it to
> null. This should then satisfy this complaint.
>
> Here is what this code typically looks like:
>
> if (!isset($Usage)) { $Usage = ''; }
> if (!isset($_userid)) { $_userid = ''; }
> if (!isset($_userno)) { $_userno = ''; }
> if (!isset($V0)) { $V0 = ''; }
>
> So my understanding is that references to any/all of the above defined
> variables should NOT cause that error notice. However, it seems to
> continue.
>
> Without going bonkers would some of you who understand this somewhat
> syntactical issue please try to explain the best way to handle this
> issue so me and I'm sure many, many other php developers can once and
> for all rid outselves of this so common issue?
>
> Thanks.
I don't know the solution to your problem. There may be something
terribly simple about it that I do not understand (I am a relative
beginner at php) . However, if I had that problem I would try to
approach it systematically. For instance:
What happens if you place a reference to one of those error prompting
variables immediately after the line where you are setting the
variable to ''?
Does the error occur? If it does, then what happens if you set it to
something besides ''? If you cannot get the if(!isset... line to
prevent the error in a referencfe immediately following it, then post
THAT here.
If it DOES prevent the error then the subsequent reference to the
variable is:
1. not actually after the if(!isset...line, or
2. is not the same variable, or
3. is within a function without access to the original set value, or
4. has been unset somewhere between the start and the error.
Without your whole script these 4 cannot be evaluated.
|
|
|
Re: Please help clear up some php error notice issue? [message #180040 is a reply to message #180036] |
Sat, 29 December 2012 09:26 |
M. Strobel
Messages: 386 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
Am 29.12.2012 04:12, schrieb BobMCT:
> Since upgrading to version 5.4.x I've been seeing a common error
> complaining about undefined variables.
> I know that the E_Notice can be suppressed but I want to eliminate the
> cause as well.
>
> To address this issue I've added statements at the beginning of the
> code to determine if the named variable is set and if not to set it to
> null. This should then satisfy this complaint.
---cut
> So my understanding is that references to any/all of the above defined
> variables should NOT cause that error notice. However, it seems to
> continue.
>
> Without going bonkers would some of you who understand this somewhat
> syntactical issue please try to explain the best way to handle this
> issue so me and I'm sure many, many other php developers can once and
> for all rid outselves of this so common issue?
There is nothing hidden or special about variables: if you use (read) them before
setting (writing to) them, you get a notice.
It is a decision of the PHP makers to give you a notice. Following accepted
programming standards it should give you an error.
How to avoid it? Your development system must display all PHP messages, including
deprecated and notice. From your first line of code there must be no notice/error at all.
Hints: unset variables can hide in templates. They came from initializing variables
in an if branch, and forgetting to do so in the else branch, or there is no else. If
your program detects several "states" with if - elseif, and you think you have
covered all possible cases and need no else branch: write an else branch and put
trigger_error('logical error', E_USER_ERROR) into it, it can save you headaches.
/Str.
|
|
|