Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

switch() Changed Comparison Style

The switch command uses a relaxed comparison style. Hence, the associated cases changed in PHP 8.0, whenever they use the special values such a 0, empty string ‘’ or null.

PHP code

<?php

$a = 0;
switch ($a) {
    case 'a': 
        print 'a'.PHP_EOL;
        break;

    case 0: 
        print 'Null'.PHP_EOL;
        break;
        
    default:
        print 'Default'.PHP_EOL;
}

?>

Before

a

After

Null

PHP version change

This behavior changed in 8.0.

Extension

Analyzer

© 2023-2026, Damien Seguy - Exakat