@fernandosavio/

Operadores: Ternário vs Null Coalesce

PHP (Legacy)

https://pt.stackoverflow.com/a/226652/6333

fork
loading
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

echo "null ?: 'Nope' == " . (null ?: 'Nope') . "\n";
echo "null ?? 'Nope' == " . (null ?? 'Nope') , "\n\n";

echo "'' ?: 'Nope' == " . ('' ?: 'Nope') . "\n";
echo "'' ?? 'Nope' == " . ('' ?? 'Nope') , "\n\n";

echo "' ' ?: 'Nope' == " . (' ' ?: 'Nope') . "\n";
echo "' ' ?? 'Nope' == " . (' ' ?? 'Nope') , "\n\n";

echo "false ?: 'Nope' == " . (false ?: 'Nope') . "\n";
echo "false ?? 'Nope' == " . (false ?? 'Nope') , "\n\n";

echo "0 ?: 'Nope' == " . (0 ?: 'Nope') . "\n";
echo "0 ?? 'Nope' == " . (0 ?? 'Nope') , "\n\n";

echo "1 ?: 'Nope' == " . (1 ?: 'Nope') . "\n";
echo "1 ?? 'Nope' == " . (1 ?? 'Nope') , "\n\n";

echo "[] ?: 'Nope' == " . ([] ?: 'Nope') . "\n";
echo "[] ?? 'Nope' == " . ([] ?? 'Nope') , "\n\n";

?>
PHP 7.0.8 (Legacy: use the following for new features: Command-line PHP: https://repl.it/languages/php_cli PHP Web Server: https://repl.it/languages/php7