Obscure PHP problem
Oct. 20th, 2010 02:39 pmThe next “Company 𝔾” conference is coming up very soon! Three customer complaints came in over the last two days, saying that the website wouldn't let them have their sets of slides for sessions-not-attending, even though it had already given them the slides for the sessions they did plan to attend.
The problem is here:if ($x == "all") {
…some code…
}So, does some code get executed or not?
I can sort of see why the PHP folks might have thought that it should work that way, but that's not what I meant! Fix:if ($x === "all") {
…some code…
}The third equals sign means exactly equal rather than close enough.
Of the 536 customers for this conference, only 52 have tried so far to get their non-attending slides. Because of this bug, the software told all of them that their downloads were “unauthorized”. Bad software, bad! Fourteen customers got around the problem by using the download-in-parts function, which is just supposed to be for large books downloaded via slow, unreliable modems. As for the other people? It seems they meekly accepted the computer's claim that they were not entitled to their books (which were supposed to be included in their conference purchase price). “Mr. Bear” sent them emails apologizing for the computer's impertinence and asking them to please try again to get their books for the conference.
The problem is here:if ($x == "all") {
…some code…
}So, does some code get executed or not?
‣ Yes if $x is the string "all";
‣ No if $x is the empty string "";
‣ No if $x is Boolean FALSE (which prints as an empty string);
‣ No if $x is the number 1 or the string "1";
‣ Yes if $x is Boolean TRUE (which prints as "1").
‣ No if $x is the empty string "";
‣ No if $x is Boolean FALSE (which prints as an empty string);
‣ No if $x is the number 1 or the string "1";
‣ Yes if $x is Boolean TRUE (which prints as "1").
I can sort of see why the PHP folks might have thought that it should work that way, but that's not what I meant! Fix:if ($x === "all") {
…some code…
}The third equals sign means exactly equal rather than close enough.
Of the 536 customers for this conference, only 52 have tried so far to get their non-attending slides. Because of this bug, the software told all of them that their downloads were “unauthorized”. Bad software, bad! Fourteen customers got around the problem by using the download-in-parts function, which is just supposed to be for large books downloaded via slow, unreliable modems. As for the other people? It seems they meekly accepted the computer's claim that they were not entitled to their books (which were supposed to be included in their conference purchase price). “Mr. Bear” sent them emails apologizing for the computer's impertinence and asking them to please try again to get their books for the conference.