Categories
PHP

Syntax Bug, or Program Bug?

So, while coding today I came across a very strange bug in PHP. It seems as though subtracting two arrays will return null.

However, if I wrap that in the floor() function, it works as expected.

Go figure. Took me 30 minutes to find that annoying bug.

By Shawn Wilsher

The man behind the site.

6 replies on “Syntax Bug, or Program Bug?”

Have you tried a simple test program to make sure you’re just not making too complicated a program?

105);
$foo2 = array(‘two’=>159);
echo ‘$foo1[\’one\’] is ‘.$foo1[‘one’].”;
echo ‘$foo2[\’two\’] is ‘.$foo2[‘two’].”;
$result = $foo2[‘two’] – $foo1[‘one’];
echo ‘the result is ‘.$result;
?>

See how that runs…

A little. The 105); was supposed to be the PHP start string. Everything else looks about right… :P

Oh, except this line was dropped in the beginning:
$foo1 = array(‘one’=>102);

If it’s only in the $_POST variable, try this variation:

if (isset($_POST[‘one’]) {
$foo2 = array(’two’=>159);
echo ‘$_POST[\’one\’] is ‘.$_POST[’one’].’’;
echo ‘$foo2[\’two\’] is ‘.$foo2[’two’].’’;
$result = $foo2[’two’] – $_POST[’one’];
echo ‘the result is ‘.$result;
} else {
echo ”;
}
?>

Yeah, that got hosed as well. It’s not that hard to htmlspecialchars() and nl2br() a comment, is it? :P

The echo was supposed to be… convert () to brackets…

(form method=”post”)(input type=”text” name=”one”)(input type=”submit”)(/form)

Ah, I see, it drops out wannabe HTML code. Why can’t it just print it? In the first comment it dropped out everything between (?php and $foo1=>105);. It saw the PHP tag’s bracket as the start, and the arrow/bracket in the => as the end tag.

Yuck. What a fucking mess… :P

Comments are closed.