Jokes IRL: How many apples would you have?


Teacher: If I gave you 2 apples and another 2 apples, how many will you have?
Beth: Five, Sir.
Teacher: Are you sure?
Beth: Yes.
Teacher (sarcastically): Okay, now tell me how did you get the number.
Beth (innocently showing an apple from her bag): I have one, you would give me another four, it will make five.

The same unexpected outcome can be seen if you do something like

while ($row $stmt->fetch()) {
$array[] = $row;

given $array already has been used above.

And it was much worse back in the days of the notorious register_globals configuration setting, which was intended to populate the global variables right from the request, i.e. having your script called such as test.php?id=1 you would have a variable called $id initialized with value 1 automatically!

You could imagine what would happen if some $admin variable has been used and someone just called your script lile test.php?admin=1.

However, it wouldn't have been that much problem, if $admin has been initialized to FALSE before use. Then no configuration setting, however malicious ur stupid it could be, would make this variable overwritten.

The moral of the story: always initialize your variables!

Related articles: