php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80900 switch statement behavior inside function
Submitted: 2021-03-23 19:09 UTC Modified: 2021-03-24 11:15 UTC
From: srohde at newshour dot org Assigned:
Status: Closed Package: opcache
PHP Version: 7.4.16 OS: OSX
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: srohde at newshour dot org
New email:
PHP Version: OS:

 

 [2021-03-23 19:09 UTC] srohde at newshour dot org
Description:
------------
When Opcache is enabled, the evaluation of a boolean variable is inconsistent within a switch statement with a default case that is inside of a function scope. If a variable that is assigned a boolean value is passed, the results do not evaluate as expected, however, if a boolean value is directly passed, the evaluation runs as expected. Outside of a function scope, the evaluation runs as expected, regardless of variable assignment.

Zend Engine v3.4.0
Zend OPcache v7.4.16
opcache.optimization_level 0x7FFEBFFF
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=2
opcache.fast_shutdown=1
opcache.enable_cli=1

Test script:
---------------
<?php

function testWithVar() {

    $booleanVar = false;

    switch ($booleanVar) {

        case 'a_test_case':
            echo 'inside testWithVar - "a_test_case"';
            break;

        default:
            echo 'inside testWithVar - "default"';
            break;

    }

}

function testWithVal() {

    switch (false) {

        case 'a_test_case':
            echo 'inside testWithVal - "a_test_case"';
            break;

        default:
            echo 'inside testWithVal - "default"';
            break;

    }

}

$booleanVar = false;

switch ($booleanVar) {

    case 'a_test_case':
        echo 'outside function - "a_test_case"';
        break;

    default:
        echo 'outside function - "default"';
        break;

}

echo PHP_EOL;

testWithVar();

echo PHP_EOL;

testWithVal();


Expected result:
----------------
outside function - "default"
inside testWithVar - "default"
inside testWithVal - "default"

Actual result:
--------------
outside function - "default"
inside testWithVar - "a_test_case"
inside testWithVal - "default"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-24 11:15 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-03-24 11:15 UTC] cmb@php.net
I can confirm this.  With optimization_level=0xA0, testWithVal()
fails as well.
 [2021-04-13 16:30 UTC] git@php.net
Automatic comment on behalf of twose
Revision: https://github.com/php/php-src/commit/7c6cf094631e5bea07ebbf972d2cc5c0d3611984
Log: Fixed bug #80900
 [2021-04-13 16:30 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC