3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getusername($user) { return 'genericUser'; } function test ($user) { { // original question $usernameA = $user ?: getusername($user); echo '$usernameA = ', var_export($usernameA, true), PHP_EOL; } { // equivalent if( $user ) { $usernameB = $user; } else { $usernameB = getusername($user); } echo '$usernameB = ', var_export($usernameB, true), PHP_EOL; } } $testCases = array('someUser', true, '', null, false, 0, @$undefined); foreach ( $testCases as $testCase ) { echo 'test with ', var_export($testCase , true) ,' ...', PHP_EOL; test($testCase); echo 'test with ', var_export($testCase , true) ,' DONE.', PHP_EOL; echo PHP_EOL; }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1 - 8.4.2
test with 'someUser' ... $usernameA = 'someUser' $usernameB = 'someUser' test with 'someUser' DONE. test with true ... $usernameA = true $usernameB = true test with true DONE. test with '' ... $usernameA = 'genericUser' $usernameB = 'genericUser' test with '' DONE. test with NULL ... $usernameA = 'genericUser' $usernameB = 'genericUser' test with NULL DONE. test with false ... $usernameA = 'genericUser' $usernameB = 'genericUser' test with false DONE. test with 0 ... $usernameA = 'genericUser' $usernameB = 'genericUser' test with 0 DONE. test with NULL ... $usernameA = 'genericUser' $usernameB = 'genericUser' test with NULL DONE.

preferences:
103.78 ms | 410 KiB | 5 Q