3v4l.org

run code in 300+ PHP versions simultaneously
<?php function maybe_change_user_language( $selected_lang, $user_lang, $site_lang ) { if ( $selected_lang !== $site_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, $site_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/1HVOn
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
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   53     1      > FE_RESET_R                                       $5      !0, ->28
          2    > > FE_FETCH_R                                       ~6      $5, !1, ->28
          3    >   ASSIGN                                                   !2, ~6
   54     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
   55    11        ECHO                                                     !3
         12        ECHO                                                     '%0A'
         13        ECHO                                                     !2
         14        ECHO                                                     '%0A'
         15        ECHO                                                     !3
         16        ECHO                                                     '%0A'
   56    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'
   53    27      > JMP                                                      ->2
         28    >   FE_FREE                                                  $5
   57    29      > RETURN                                                   1

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

End of function test_respect_user_language_preference

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
9.98 ms | 416 KiB | 7 Q