Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Odd php error..

Credits go to BAF, bamccaig, and Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Odd php error..
Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

What are some examples of hard to notice logic errors that will trigger E_NOTICEs?

Typos, logic that prevents a variable from being defined, unexpected index values...

It gets more useful as the project becomes more complex. For simple one-off scripts, it's not that big of a deal. But when you're creating an entire framework/library of code, then it's very useful. (Especially when you forget how you did things...)

bamccaig
Member #7,536
July 2006
avatar

BAF said:

So you're telling me that I can do the following in C++ to see if a variable exists...

PHP is in no way C++! Besides, the compiler will tell you if a variable doesn't 'exist' in C++ so, to my knowledge, you shouldn't need to check if one does exist...

BAF, have you even programmed a script in PHP!?

Matthew Leverton said:

Typos, logic that prevents a variable from being defined, unexpected index values...

If you misspell a variable you should quickly see that the intended action isn't happening correctly (during testing) and can use print() statements to trace the execution path.

In terms of logic that prevents a variable from being defined, this can be almost eliminated by declaring your variables at the top of your script, functions, and methods. Which is also a best practice...

???

Again, I guess I agree that you should use isset(), but I still don't like it.

BAF
Member #2,981
December 2002
avatar

Quote:

BAF, have you even programmed a script in PHP!?

Resisting... urge...... to be..... smartass...........

I have done a LOT of PHP work. Once upon a (short) time I was pretty naive like you. Then I learned the importance of writing clean code.

Quote:

If you misspell a variable you should quickly see that the intended action isn't happening correctly (during testing) and can use print() statements to trace the execution path.

In terms of logic that prevents a variable from being defined, this can be almost eliminated by declaring your variables at the top of your script, functions, and methods. Which is also a best practice...

Again, I guess I agree that you should use isset(), but I still don't like it.

You may not notice a variable issue like that if it's in an obscure not-well-used code path. The notices really do help save a lot of time. IF you ever start writing important code, you will (I hope..) learn the importance of clean code and the easy way of debugging. You don't need to predefine all your variables at the top, but you definately shouldn't assume stuff like post variables exist. That is just foolish. Also, isset() makes your code more readable.

bamccaig
Member #7,536
July 2006
avatar

BAF said:

You don't need to predefine all your variables at the top, but you definately shouldn't assume stuff like post variables exist.

You don't need to, but you should. And I never assumed it was set. Scripting langauges like PHP are funny in that if the variable doesn't exist it will evaluate it as false and therefore in a boolean condition can be treated like a flag. If it was set it would evaluate to true. If it's not set (doesn't exist) is will evaluate to false, similar to null.

I've seen isset() code and I say it's less readable. It all depends. There may be circumstances where my way won't work, but in the even of checking form fields that cannot be zero or empty it works fine. To make it more readable you could easy create a hidden form field to check instead of the button:

1...etc
2 
3<?php
4 if($_POST['hidSubmitted'])
5 {
6 // Form was submitted...
7 }
8?>
9 
10...etc...
11 
12 <form ...>
13 
14...etc...
15 
16 <input type="hidden" name="hidSubmitted" id="hidSubmitted" value="true" />
17 
18...etc...
19 
20 </form>
21 
22etc...

I do write clean code. I'm more obsessive about it than most. It all depends on what you call clean.

 1   2   3 


Go to: