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

New Cases In Switch

The result of comparisons between empty values, such as 0, '' (empty string), or [] (empty array), have changed. The impact is obvious with the == operator, and it is less obvious with switch, which relies on the same underlying code.

In particular, when there are several falsy cases in a switch, the selection of cases may be different between PHP versions, like in this illustration.

In PHP 7.4 and older, 0 == '', so the first case is selected. After PHP 8.0, 0 != '', and the second case is selected.

PHP code

<?php

foreach([0, '', null, []] as $a )
	switch($a) {
		case 0;
			print Zero\n;
			break;
			
		case '':
			print Empty string\n;
			break;
			
		case []:
			print []\n;
			break;
	}

?>

Before

Zero
Zero
Zero
[]

After

Zero
Empty string
Zero
[]

PHP version change

This behavior changed in 8.0.

Extension

© 2023-2026, Damien Seguy - Exakat