This weeks PHP code mistake

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

This weeks PHP code mistake

Post by neophyte »

When someone forgets to check whether an array is empty or even defined before looping through it producing:
Invalid argument supplied for foreach()
What's your fav...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

half a screen full of these:
CLI wrote:Strict Standards: Declaration of MockRequestUserAgent::_createHttpRequest() should be compatible with that of SimpleUserAgent::_createHttpRequest() in DNA/3rdParty/SimpleTest/mock_objects.php(1058) : eval()'d code on line 129

Strict Standards: Declaration of PartialSimplePageBuilder::_createPage() should be compatible with that of SimplePageBuilder::_createPage() in DNA/3rdParty/SimpleTest/mock_objects.php(1058) : eval()'d code on line 134

Strict Standards: Declaration of PartialSimplePageBuilder::_createParser() should be compatible with that of SimplePageBuilder::_createParser() in DNA/3rdParty/SimpleTest/mock_objects.php(1058) : eval()'d code on line 134

Strict Standards: Declaration of MockParseSimpleBrowser::_createUserAgent() should be compatible with that of SimpleBrowser::_createUserAgent() in DNA/3rdParty/SimpleTest/mock_objects.php(1058) : eval()'d code on line 134
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

When some forgets a bracket match or a semicolon and gets this









and asks why the screen is blank. Better, when they ask one of us to find the error for them.

But my personal favorite are undefined index error from mispelled array indexes or undefined function errors from mispelled function names.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

mine is mimilar... forgoten semicolon: "unexpected tstring/whatdaf_ck"

or... due to not using for cycles so many times like i did back when i programed C++

for (i=0;i<100;i++){
...
}

produces something i dont remember and damn difficult to debug... aparently when i look to the above PHP code everything looks ok to me, lol
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

public function getItem ($key = null)
	{	
		if ((!is_null($key)) && (isset($this->array[$key]))) {
			return $this->array[$key];
		} elseif ((is_null($key)) && (isset($this->keys[$this->index]))) {
			return $this->array[$this->keys[$this->index]];
		} else {
			$this->index = 0;
			return FALSE;
		}
	}
and wondering why

Code: Select all

while ($item = $array->getItem() !== false) { 
  /* .. */
}
loops infinitely, for 45mins of head scratching fun.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

omiting WHERE clause in the UPDATE statement :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Weirdan wrote:omiting WHERE clause in the UPDATE statement :)
I usually scream "Noooooooo!!!1!!1" (ones included) when that happens. Then I remember I'm working from a copy. :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Weirdan wrote:omiting WHERE clause in the UPDATE statement :)
Crapdangle, I can't count the number of times I hit myself in the head with a cold hand after doing that...
User avatar
Uranium-235
Forum Newbie
Posts: 13
Joined: Tue Aug 08, 2006 3:57 pm

Post by Uranium-235 »

Weirdan wrote:omiting WHERE clause in the UPDATE statement :)
omitting the WHERE clause in a DELETE statement :D
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

still the missing final paranthesis for me i.e.

Code: Select all

if(!empty($someVar)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

JayBird wrote:still the missing final paranthesis for me i.e.

Code: Select all

if(!empty($someVar)
Oh, that never happens to me thanks to a simple technique I learned while reading an HTML book several years ago...
I'll explain this on HTML, but the same is true with PHP. All you need to do is to follow one simple rule/guideline:

Right after you open a tag - close it.

For example, if you want to code something like this:

Code: Select all

<body>Some text</body>
type it in this order:

1. Open the body tag and right after that close it:

Code: Select all

<body></body>
2. Now add the content:

Code: Select all

<body>Some text</body>
When I first read this HTML book I was like: "That's not going to happen to me, why the hell would I forget to close a tag?".
Of course, like anybody else, I forgot to close my tags :P
Once I started to follow this rule/guideline, I have never forgotten to close my tags anymore :D
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Oren: i actually do do that...but on the odd occasion, in my haste, i still forget
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Then maybe you are just no used to it, once you get used to it - you never forgets :wink:
When I code PHP I type something like:

Code: Select all

if ()
and only then I type the condition. Once you get used to it, there is no chance you'll forget - no matter how fast you code.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

or use a text editor that does it for you.

re: omitting WHERE on DELETE.. I know someone who worked for a bank supporting their transactions database. They received a call to remove a 'corrupt' record..

they entered : DELETE FROM `transactions`;

the bank had to freeze for 24hours until the backup arrived and was updated to include all the transactions from the day that was lost.

Bye bye job and hello Sarbanes&Oxley auditors having a field day!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Banks are always fun to hear about ;)
Post Reply