I’m having some trouble understanding why the following doesn’t result in a compiler error in 5.3.3 (errored-out correctly on my coworkers 5.2.5):
<?php
echo "starting\n";
switch(1) {
case 2:
echo "two\n";
break;
defalut: // note the misspelling
echo "deflaut\n";
}
echo "ending\n";
Instead of giving me a compiler error (or even a warning) it just gives this:
starting
ending
However, if I use it in an if-statement it gives me what I’d expect:
<?php
if (1 == deflaut)
echo "deflaut2\n";
gives:
PHP Notice: Use of undefined constant deflaut - assumed 'deflaut' in ...
Why is this? Is there a setting somewhere I can disable to tell it to be strict about this sort of thing?
The problem is that your code isn’t doing what you think. A
caseblock only ends when the nextcaseblock occurs, or whendefault:is found, or when the closing}is reached. This means thatdefalutis part of thecase 2:block. So it is never even interpreted.However, it doesn’t even fire a syntax error (not even if you do
switch (2). This is because thegotooperator was introduced in PHP 5.3. The syntaxword:at the beginning of a PHP statement is now a target accessible viagoto. Sogoto defalut;can be used to go to the label.(Actually, it can’t, because of a restriction on targets inside
switchblocks to avoid infinite loops, but this should illustrate the point…)You can make it force an error by doing
case defalut, when the error that you expect is found.