What's your fav...Invalid argument supplied for foreach()
This weeks PHP code mistake
Moderator: General Moderators
This weeks PHP code mistake
When someone forgets to check whether an array is empty or even defined before looping through it producing:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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
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
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;
}
}Code: Select all
while ($item = $array->getItem() !== false) {
/* .. */
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Uranium-235
- Forum Newbie
- Posts: 13
- Joined: Tue Aug 08, 2006 3:57 pm
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...JayBird wrote:still the missing final paranthesis for me i.e.
Code: Select all
if(!empty($someVar)
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>1. Open the body tag and right after that close it:
Code: Select all
<body></body>Code: Select all
<body>Some text</body>Of course, like anybody else, I forgot to close my tags
Once I started to follow this rule/guideline, I have never forgotten to close my tags anymore
Then maybe you are just no used to it, once you get used to it - you never forgets
When I code PHP I type something like:
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.
When I code PHP I type something like:
Code: Select all
if ()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!
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!