|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79495 PHP fails to emit notices about undefined variables in switch expression
Submitted: 2020-04-19 17:46 UTC Modified: 2021-02-18 12:01 UTC
Avg. Score:4.0 ± 0.8
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: Assigned:
Status: Open Package: Output Control
PHP Version: Next Major Version OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2020-04-19 17:46 UTC]
PHP does not emit notices in switch where the only case is the default case (or where there are no cases)

This can be fixed by Zend/zend_compile.c emitting a CHECK_VAR opcode when the temporary value is IS_CV (a compiled variable such as $undefSwitch), in the cases where:
- there are no case statements
- if the only case statement is the default case.

Noticed in is related, but it's from 2010 (php 5.2), and I expect opinions have changed since then, e.g. with getting approved

Test script:
ini_set('display_errors', 'stderr');
echo $undefVar;  // This does emit a notice
switch($undefSwitch) {}  // This does not emit a notice
switch($undefSwitch2) { default: echo "default\n";}  // This does not emit a notice

Expected result:
PHP should emit an undefined variable notice for the two switch statements in the test script

Actual result:
PHP does not emit any notices for the two switch statements in the test script, in php 5.3, 5.6, 7.0, 7.4, and 8.0-dev

Apparently, it did emit notices in 5.5 - suggests that emitting notices was the expected behavior for 5.5.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-22 17:27 UTC]
If the switch body is empty, or has just a default clause, the
switch is superfluous, and as such optimized away by the compiler
(not OPcache).  If there should be diagnostic message, it should
be about the superfluous switch, not the irrelevant variable in
the switch expression.
 [2021-02-18 12:01 UTC]
A corollary is that you also get multiple undefined variable warnings if there are multiple cases:
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 22 16:01:31 2024 UTC