John Davidson

php - When is CASE_STRICT generated?


I am trying to find an example where PHP 8.1 generates the CASE_STRICT opcode.

I found this message from @IluTov:

@Derick When no jump table can be generated.

function foo($value) {
return match ($value) {
true => 'foo',
null => 'bar',

But that example only generates IS_IDENTICAL/JMPNZ/QM_ASSIGN for each case. I tried different cases and types, but only got either the regular MATCH code with jump table or the same IS_IDENTICAL/JMPNZ/QM_ASSIGN combination.

0002 T1 = IS_IDENTICAL CV0($value) bool(true)
0003 JMPNZ T1 0008
0004 T1 = IS_IDENTICAL CV0($value) null
0005 JMPNZ T1 0010
0006 JMP 0007
0007 MATCH_ERROR CV0($value)
0008 T2 = QM_ASSIGN string("foo")
0009 JMP 0012
0010 T2 = QM_ASSIGN string("bar")
0011 JMP 0012
0012 RETURN T2

What am I missing? When is CASE_STRICT generated?

My Answer:
CASE_STRICT opcode is generated in PHP 8.1 when a switch statement has a case with a non-constant value. This can happen when using expressions or variables as case values instead of just constants.

Here is an example where PHP 8.1 generates the CASE_STRICT opcode:

$var = 2;

switch ($var) {
case 1:
echo "One";
case $var:
echo "Two";
echo "Default";

In this example, the second case statement uses the variable `$var` as the case value, which is a non-constant value. This will trigger the generation of the CASE_STRICT opcode in PHP 8.1.

Rate this post

5 of 5 based on 9241 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us