Page 1 of 3

This weeks PHP code mistake

Posted: Wed Aug 16, 2006 4:02 pm
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...

Posted: Wed Aug 16, 2006 4:05 pm
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

Posted: Wed Aug 16, 2006 4:52 pm
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.

Posted: Wed Aug 16, 2006 6:41 pm
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

Posted: Wed Aug 16, 2006 8:15 pm
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.

Posted: Wed Aug 16, 2006 8:16 pm
by Weirdan
omiting WHERE clause in the UPDATE statement :)

Posted: Wed Aug 16, 2006 8:18 pm
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. :)

Posted: Wed Aug 16, 2006 9:06 pm
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...

Posted: Wed Aug 16, 2006 9:43 pm
by Uranium-235
Weirdan wrote:omiting WHERE clause in the UPDATE statement :)
omitting the WHERE clause in a DELETE statement :D

Posted: Thu Aug 17, 2006 4:11 am
by JayBird
still the missing final paranthesis for me i.e.

Code: Select all

if(!empty($someVar)

Posted: Thu Aug 17, 2006 4:26 am
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

Posted: Thu Aug 17, 2006 4:28 am
by JayBird
Oren: i actually do do that...but on the odd occasion, in my haste, i still forget

Posted: Thu Aug 17, 2006 4:41 am
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.

Posted: Thu Aug 17, 2006 5:45 am
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!

Posted: Thu Aug 17, 2006 12:30 pm
by Weirdan
Banks are always fun to hear about ;)