3v4l.org

run code in 300+ PHP versions simultaneously
<?php function maybe_change_user_language( $selected_lang, $user_lang ) { if ( $selected_lang !== $user_lang ) { $user_lang = $selected_lang; } return $user_lang; } // Tests - Scroll to the bottom for results. function test_respect_user_language_preference( $expected, $site_lang, $user_lang, $selected_lang = 'English' ) { $user_lang = maybe_change_user_language( $selected_lang, $user_lang ); echo 'Expected: ', $expected, PHP_EOL; echo 'Actual: ', $user_lang, PHP_EOL; return 'Result: ' . ( $expected === $user_lang ? 'Pass' : 'Fail' ); } $tests = array( 'defaults with nothing selected' => array( 'expected' => 'English', 'site_lang' => 'English', 'user_lang' => 'English', 'selected_lang' => 'English', ), 'no existing preference and selects Spanish' => array( 'expected' => 'Spanish', 'site_lang' => 'English', 'user_lang' => 'English', 'selected_lang' => 'Spanish', ), 'already prefers Spanish and selects Spanish' => array( 'expected' => 'Spanish', 'site_lang' => 'English', 'user_lang' => 'Spanish', 'selected_lang' => 'Spanish', ), 'already prefers Spanish and selects nothing' => array( 'expected' => 'Spanish', 'site_language' => 'English', 'user_lang' => 'Spanish', 'selected_lang' => 'English', ), ); foreach ( $tests as $test => $args ) { $sep = str_pad( '', strlen( $test ), '=' ); echo $sep, PHP_EOL, $test, PHP_EOL, $sep, PHP_EOL; echo test_respect_user_language_preference( ...array_values( $args ) ), PHP_EOL, PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 28
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 28
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/K3dkQ
function name:  (null)
number of ops:  30
compiled vars:  !0 = $tests, !1 = $args, !2 = $test, !3 = $sep
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   ASSIGN                                                   !0, <array>
   52     1      > FE_RESET_R                                       $5      !0, ->28
          2    > > FE_FETCH_R                                       ~6      $5, !1, ->28
          3    >   ASSIGN                                                   !2, ~6
   53     4        INIT_FCALL                                               'str_pad'
          5        SEND_VAL                                                 ''
          6        STRLEN                                           ~8      !2
          7        SEND_VAL                                                 ~8
          8        SEND_VAL                                                 '%3D'
          9        DO_ICALL                                         $9      
         10        ASSIGN                                                   !3, $9
   54    11        ECHO                                                     !3
         12        ECHO                                                     '%0A'
         13        ECHO                                                     !2
         14        ECHO                                                     '%0A'
         15        ECHO                                                     !3
         16        ECHO                                                     '%0A'
   55    17        INIT_FCALL                                               'test_respect_user_language_preference'
         18        INIT_FCALL                                               'array_values'
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $11     
         21        SEND_UNPACK                                              $11
         22        CHECK_UNDEF_ARGS                                         
         23        DO_FCALL                                      1  $12     
         24        ECHO                                                     $12
         25        ECHO                                                     '%0A'
         26        ECHO                                                     '%0A'
   52    27      > JMP                                                      ->2
         28    >   FE_FREE                                                  $5
   56    29      > RETURN                                                   1

Function maybe_change_user_language:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/K3dkQ
function name:  maybe_change_user_language
number of ops:  7
compiled vars:  !0 = $selected_lang, !1 = $user_lang
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        IS_NOT_IDENTICAL                                         !0, !1
          3      > JMPZ                                                     ~2, ->5
    5     4    >   ASSIGN                                                   !1, !0
    8     5    > > RETURN                                                   !1
    9     6*     > RETURN                                                   null

End of function maybe_change_user_language

Function test_respect_user_language_preference:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/K3dkQ
function name:  test_respect_user_language_preference
number of ops:  23
compiled vars:  !0 = $expected, !1 = $site_lang, !2 = $user_lang, !3 = $selected_lang
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      'English'
   14     4        INIT_FCALL                                               'maybe_change_user_language'
   15     5        SEND_VAR                                                 !3
   16     6        SEND_VAR                                                 !2
   14     7        DO_FCALL                                      0  $4      
          8        ASSIGN                                                   !2, $4
   19     9        ECHO                                                     'Expected%3A+'
         10        ECHO                                                     !0
         11        ECHO                                                     '%0A'
   20    12        ECHO                                                     'Actual%3A+++'
         13        ECHO                                                     !2
         14        ECHO                                                     '%0A'
   22    15        IS_IDENTICAL                                             !0, !2
         16      > JMPZ                                                     ~6, ->19
         17    >   QM_ASSIGN                                        ~7      'Pass'
         18      > JMP                                                      ->20
         19    >   QM_ASSIGN                                        ~7      'Fail'
         20    >   CONCAT                                           ~8      'Result%3A+++', ~7
         21      > RETURN                                                   ~8
   23    22*     > RETURN                                                   null

End of function test_respect_user_language_preference

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.36 ms | 1445 KiB | 17 Q