Having a strange behavior with Ternary Logic

74 views Asked by At

When I do -

echo "Hello ". (true) ? 'World' : 'XYZ';

The output - World instead of Hello World. But when -

$person = (true) ? 'World' : 'XYZ';
echo  "Hello ".$person;

The output - Hello World which is expected. Don't understand why this behave like this! Is it expected?

1

There are 1 answers

3
xdazz On BEST ANSWER
echo "Hello ". (true) ? 'World' : 'XYZ';

is same as

echo ("Hello ". (true)) ? 'World' : 'XYZ';

You need:

echo "Hello ". (true ? 'World' : 'XYZ');