Deprecate terminating case statements with a semicolon (#19215)

Part of https://wiki.php.net/rfc/deprecations_php_8_5

Closes GH-15258
This commit is contained in:
Theodore Brown 2025-08-11 07:18:06 -05:00 committed by GitHub
parent d01aa02ef5
commit 5f8d648af6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 22 additions and 11 deletions

2
NEWS
View file

@ -7,6 +7,8 @@ PHP NEWS
been deprecated. (Girgias)
. The $exclude_disabled parameter of the get_defined_functions() function has
been deprecated, as it no longer has any effect since PHP 8.0. (Girgias)
. Terminating case statements with a semicolon instead of a colon has
been deprecated. (theodorejb)
- DOM:
. Fixed bug GH-18877 (\Dom\HTMLDocument querySelectorAll selecting only the

View file

@ -322,6 +322,9 @@ PHP 8.5 UPGRADE NOTES
. The $exclude_disabled parameter of the get_defined_functions() function has
been deprecated, as it no longer has any effect since PHP 8.0.
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_exclude_disabled_parameter_of_get_defined_functions
. Terminating case statements with a semicolon instead of a colon has
been deprecated.
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_semicolon_after_case_in_switch_statement
- FileInfo:
. The finfo_close() function has been deprecated.

View file

@ -6313,6 +6313,11 @@ static void zend_compile_switch(zend_ast *ast) /* {{{ */
continue;
}
if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
CG(zend_lineno) = case_ast->lineno;
zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
}
zend_compile_expr(&cond_node, cond_ast);
if (expr_node.op_type == IS_CONST

View file

@ -1127,6 +1127,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_TENTATIVE_BIT) != 0)
#define ZEND_DIM_IS (1 << 0) /* isset fetch needed for null coalesce. Set in zend_compile.c for ZEND_AST_DIM nested within ZEND_AST_COALESCE. */
#define ZEND_ALT_CASE_SYNTAX (1 << 1) /* deprecated switch case terminated by semicolon */
/* Attributes for ${} encaps var in strings (ZEND_AST_DIM or ZEND_AST_VAR node) */
/* ZEND_AST_VAR nodes can have any of the ZEND_ENCAPS_VAR_* flags */

View file

@ -713,15 +713,14 @@ switch_case_list:
case_list:
%empty { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
| case_list T_CASE expr case_separator inner_statement_list
| case_list T_CASE expr ':' inner_statement_list
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, $3, $5)); }
| case_list T_DEFAULT case_separator inner_statement_list
| case_list T_CASE expr ';' inner_statement_list
{ $$ = zend_ast_list_add($1, zend_ast_create_ex(ZEND_AST_SWITCH_CASE, ZEND_ALT_CASE_SYNTAX, $3, $5)); }
| case_list T_DEFAULT ':' inner_statement_list
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, NULL, $4)); }
;
case_separator:
':'
| ';'
| case_list T_DEFAULT ';' inner_statement_list
{ $$ = zend_ast_list_add($1, zend_ast_create_ex(ZEND_AST_SWITCH_CASE, ZEND_ALT_CASE_SYNTAX, NULL, $4)); }
;

View file

@ -15,9 +15,9 @@ class ZException extends Exception {
function dummy($query) {
try {
switch ($query) {
case 1;
case 1:
break;
case 2;
case 2:
break;
default:
throw new Exception('exception');

View file

@ -37,7 +37,8 @@ switch ($a):
break;
endswitch;
?>
--EXPECT--
--EXPECTF--
Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in %s
If: 11
While: 12346789
For: 0123401234

View file

@ -15,7 +15,7 @@ for ($i = 0; $i < $len; $i++) {
$str = '*';
switch ($str[0]) {
case '*';
case '*':
echo "OK\n";
break;
default: