Using const in php switch in php 5.5.9

After installing PHP 5.5.9 on Ubuntu 14, I discovered this strange behavior with a switch statement and a constant PHP_OS.

I assume that in PHP 5.5.9 the switch statement also checks the same type (===)?

Or is this a PHP bug?

echo PHP_OS; // Linux
$os = PHP_OS;

switch (PHP_OS) {
    case "WINNT":
        echo 'Windows';
        break;
    case "Linux":
        echo 'Linux';
        break;
    default:
        echo 'Default';
        break;
}
// Default

switch ((string) PHP_OS) {
    case "WINNT":
        echo 'Windows';
        break;
    case "Linux":
        echo 'Linux';
        break;
    default:
        echo 'Default';
        break;
}
// Default

switch ($os) {
    case "WINNT":
        echo 'Windows';
        break;
    case "Linux":
        echo 'Linux';
        break;
    default:
        echo 'Default';
        break;
}
// Linux
+4
source share
1 answer

PHP switches use free comparison, for example ==, so it should match.

You could try:

switch (constant("PHP_OS"))
+2
source

All Articles