<?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;
}
preferences:
57.65 ms | 404 KiB | 5 Q