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; }
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.4, 8.3.6 - 8.3.15, 8.4.1 - 8.4.2
============================== defaults with nothing selected ============================== Expected: English Actual: English Result: Pass ========================================== no existing preference and selects Spanish ========================================== Expected: Spanish Actual: Spanish Result: Pass =========================================== already prefers Spanish and selects Spanish =========================================== Expected: Spanish Actual: Spanish Result: Pass =========================================== already prefers Spanish and selects nothing =========================================== Expected: Spanish Actual: Spanish Result: Pass
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 ============================== defaults with nothing selected ============================== Expected: English Actual: English Result: Pass ========================================== no existing preference and selects Spanish ========================================== Expected: Spanish Actual: Spanish Result: Pass =========================================== already prefers Spanish and selects Spanish =========================================== Expected: Spanish Actual: Spanish Result: Pass =========================================== already prefers Spanish and selects nothing =========================================== Expected: Spanish Actual: Spanish Result: Pass

preferences:
84.17 ms | 410 KiB | 5 Q