I am trying to find an example where PHP 8.1 generates the CASE_STRICT
opcode.
I found this message from @IluTov: https://chat.stackoverflow.com/transcript/message/50366061#50366061
@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:
php
$var = 2;
switch ($var) {
case 1:
echo "One";
break;
case $var:
echo "Two";
break;
default:
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
3 of 5 based on 9601 votesComments