473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

undefined: good or bad?

I decided to run some code with errors set to E_ALL, just to see what I
would run across. It caught a few things, but 90% or better of the
messages were of the 'undefined' kind:

PHP Notice: Undefined variable
PHP Notice: Undefined index
PHP Notice: Undefined property

I'd like to go back and fix what I can, but I'm wondering: how good or
bad is this? I mean, I rely on the fact that the first use of a variable
defines it. However, I'm also very used to "use strict" in perl, so I
know that on-the-fly variable declarations can be dangerous.

Is there a general rule of thumb for correcting these? Best practice? Do
most people not care? If it ain't broke don't fix it?

--cd
Jul 17 '05 #1
7 2786
Coder Droid <co********@likethiswouldstopspam.hotmail.com> wrote:
I decided to run some code with errors set to E_ALL, just to see what I
would run across. It caught a few things, but 90% or better of the
messages were of the 'undefined' kind: [snip] I'd like to go back and fix what I can, but I'm wondering: how good or
bad is this? I mean, I rely on the fact that the first use of a variable
defines it. However, I'm also very used to "use strict" in perl, so I
know that on-the-fly variable declarations can be dangerous.


IMHO I only get undefined warnings if:
-I made a typo
-I make an potentially conditional error

--

Daniel Tryba

Jul 17 '05 #2
Coder Droid wrote:
I decided to run some code with errors set to E_ALL, just to see what I
would run across. It caught a few things, but 90% or better of the
messages were of the 'undefined' kind:

PHP Notice: Undefined variable
PHP Notice: Undefined index
PHP Notice: Undefined property

I'd like to go back and fix what I can, but I'm wondering: how good or
bad is this? I mean, I rely on the fact that the first use of a variable
defines it. However, I'm also very used to "use strict" in perl, so I
know that on-the-fly variable declarations can be dangerous.

Is there a general rule of thumb for correcting these? Best practice? Do
most people not care? If it ain't broke don't fix it?


You won't get "undefined variable" etc if you do an assignment, but you will
get the error if you try to reference it if it hasn't yet been assigned.

For example, if this is the first line in your file, you'll get an
"undefined variable" error message (although if you had register globals on
a foo was passed in the query string then you'd be ok, but that's bad
practise anyway):

print $foo;

It's a good idea to code with notices on when you are in development as it
helps you catch stuff like this out which may actually cause more problems
than you realise.

You'll get undefined index if referencing an array item which hasn't yet
been defined eg

print $foo['bar'];

and undefined property when it an object property hasn't been assigned eg

$foo = new foo();
print $foo->bar;

class foo {
// yes, it's empty!
}
However, due to the dynamic nature of PHP this extension of the above
example is acceptable, even though the bar propert was not declared in the
foo class:
$foo = new foo();
$foo->bar = 1;

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3
> For example, if this is the first line in your file, you'll get an
"undefined variable" error message:

print $foo;

But $foo will have a clean initial value, right? That is, it's not
unpredicable garbage (like this was C or something...) So it really
comes down to: did you mean to do this or not, it would seem.
It's a good idea to code with notices on when you are in development as it helps you catch stuff like this out which may actually cause more problems than you realise.


That makes sense. If you get it down to ones you know about and are
acceptable, you can move to production with a good sense that you don't
have any loose ends: even things unit tests might not catch.

Hmmm...

--cd
Jul 17 '05 #4
Coder Droid wrote:
For example, if this is the first line in your file, you'll get an
"undefined variable" error message:

print $foo;


But $foo will have a clean initial value, right? That is, it's not
unpredicable garbage (like this was C or something...) So it really
comes down to: did you mean to do this or not, it would seem.


Yes, assuming register gloabals is OFF. But it's still better to assign it
yourself.

If register globals is ON it may very well have already been assigned a
value eg with www.domain.com/?foo=1 then $foo will have already been set to
1 so you cannot trust the value.

This is why they changed the default for register globals to OFF.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #5
>> For example, if this is the first line in your file, you'll get an
"undefined variable" error message:

print $foo;

But $foo will have a clean initial value, right? That is, it's not


Not if register_globals is on and someone found it useful (or
malicious) to give $foo some other initial value. The message may
also be a good way of catching that you really meant $food.
unpredicable garbage (like this was C or something...) So it really
comes down to: did you mean to do this or not, it would seem.
It's a good idea to code with notices on when you are in development

as it
helps you catch stuff like this out which may actually cause more

problems
than you realise.


That makes sense. If you get it down to ones you know about and are
acceptable, you can move to production with a good sense that you don't
have any loose ends: even things unit tests might not catch.


I say: leave the notices on in production, but send them to syslog.
Users are ingenious in using stuff in ways the designer didn't expect.
Scanning the logs occasionally for problems may help you find problems.

Much of my PHP code makes heavy use of isset(), especially on
variables expected to be imported from the HTTP request. It calls
attention to issues such as: What happens if $_GET['email'] ISN'T
set? It *CAN* happen, even if you have Javascript on your page to
require it being filled in. Remember, anyone can get a copy of
your page (as sent from the browser: this won't include the PHP,
but it will include the Javascript if any), then edit it removing
any input checks, and submit it. Also, anyone can telnet to the
web server and make up any sort of HTTP request with manual typing
that they want.

Gordon L. Burditt
Jul 17 '05 #6
> This is why they changed the default for register globals to OFF.

Yeah, I can't see where that would EVER be a good idea.

--cd
Jul 17 '05 #7
> I say: leave the notices on in production, but send them to syslog.

Hmmm.... that's an interesting idea. Although, I highly doubt I could do
that in my shared hosting environment. But it has sparked an idea or
two.
Users are ingenious in using stuff in ways the designer didn't expect.


No kidding. I have a rudimentary shopping cart which every once in a
while gets checked out with 0 items in the cart. I can NOT reproduce
this behavior whatsoever. But they sure can from time to time!

What's the old phrase? About how programmers are trying to make
idiot-proof systems while the universe is producing better idiots...?

--cd
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: dingbat | last post by:
I'm writing a "tabbed folder" nav bar. Site standards are graphical prettiness, CSS throughout, valid code, but accesibility is ignored where it conflicts with prettiness. The particular issue...
15
by: shablool | last post by:
Hi all, Two distinct compilers give different output for the following code: #include <iostream> #include <string> int main(void) { std::string s("0124"); s.replace(0, 3,...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
4
by: r.nikhilk | last post by:
Hi, We are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by including the...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
55
by: Kenny McCormack | last post by:
#include <stdio.h> int main(void) { printf("hello, world\n"); } I compile it with all warning turned on and it still works fine.
14
by: jl_post | last post by:
Hi, I've heard that if you've declared a variable (such as a double or an int) and not initialize it, then the result of printing out its value is undefined. I've also heard that "undefined...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.