3v4l.org

run code in 500+ PHP versions simultaneously
<?php function maybe_change_user_language( $site_lang, $user_lang, $selected_lang ) { $result = 'Do nothing.'; if ( $site_lang !== $selected_lang && $user_lang !== $selected_lang ) { $result = "Change from $user_lang to $selected_lang."; } return $result; } // 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( $site_lang, $user_lang, $selected_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' => 'Do nothing.', 'site_lang' => 'English', 'user_lang' => 'English', 'selected_lang' => 'English', ), 'no existing preference and selects Spanish' => array( 'expected' => 'Change from English to Spanish.', 'site_lang' => 'English', 'user_lang' => 'English', 'selected_lang' => 'Spanish', ), 'already prefers Spanish and selects Spanish' => array( 'expected' => 'Do nothing.', 'site_lang' => 'English', 'user_lang' => 'Spanish', 'selected_lang' => 'Spanish', ), 'already prefers Spanish and selects nothing' => array( 'expected' => 'Do nothing.', '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/p8gRv
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
-----------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                       !0, <array>
   55     1      > FE_RESET_R                                           $5      !0, ->28
          2    > > FE_FETCH_R                                           ~6      $5, !1, ->28
          3    >   ASSIGN                                                       !2, ~6
   56     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
   57    11        ECHO                                                         !3
         12        ECHO                                                         '%0A'
         13        ECHO                                                         !2
         14        ECHO                                                         '%0A'
         15        ECHO                                                         !3
         16        ECHO                                                         '%0A'
   58    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                                          0  $12     
         24        ECHO                                                         $12
         25        ECHO                                                         '%0A'
         26        ECHO                                                         '%0A'
   55    27      > JMP                                                          ->2
         28    >   FE_FREE                                                      $5
   59    29      > RETURN                                                       1

Function maybe_change_user_language:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 8
filename:       /in/p8gRv
function name:  maybe_change_user_language
number of ops:  17
compiled vars:  !0 = $site_lang, !1 = $user_lang, !2 = $selected_lang, !3 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
    4     3        ASSIGN                                                       !3, 'Do+nothing.'
    6     4        IS_NOT_IDENTICAL                                     ~5      !0, !2
          5      > JMPZ_EX                                              ~5      ~5, ->8
          6    >   IS_NOT_IDENTICAL                                     ~6      !1, !2
          7        BOOL                                                 ~5      ~6
          8    > > JMPZ                                                         ~5, ->15
    7     9    >   ROPE_INIT                                         5  ~8      'Change+from+'
         10        ROPE_ADD                                          1  ~8      ~8, !1
         11        ROPE_ADD                                          2  ~8      ~8, '+to+'
         12        ROPE_ADD                                          3  ~8      ~8, !2
         13        ROPE_END                                          4  ~7      ~8, '.'
         14        ASSIGN                                                       !3, ~7
   10    15    > > RETURN                                                       !3
   11    16*     > 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/p8gRv
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
-----------------------------------------------------------------------------------------
   15     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
          3        RECV_INIT                                            !3      'English'
   16     4        INIT_FCALL                                                   'maybe_change_user_language'
   17     5        SEND_VAR                                                     !1
   18     6        SEND_VAR                                                     !2
   19     7        SEND_VAR                                                     !3
   16     8        DO_FCALL                                          0  $4      
          9        ASSIGN                                                       !2, $4
   22    10        ECHO                                                         'Expected%3A+'
         11        ECHO                                                         !0
         12        ECHO                                                         '%0A'
   23    13        ECHO                                                         'Actual%3A+++'
         14        ECHO                                                         !2
         15        ECHO                                                         '%0A'
   25    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
   26    23*     > RETURN                                                       null

End of function test_respect_user_language_preference

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170.01 ms | 2018 KiB | 17 Q