-2

Is $time ?: 3600 equivalent to isset($time) ? $time : 3600 ?

Thank you all.

Lewis
  • 14,132
  • 12
  • 66
  • 87

2 Answers2

2

No. There are cases for which they evaluate to the same value, but they are not equivalent.

<?php
print $time ?: 3600;
print "\n";
print isset($time) ? $time : 3600;
print "\n\n";

$time = 0;
print $time ?: 3600;
print "\n";
print isset($time) ? $time : 3600;
print "\n\n";

$time = 30;
print $time ?: 3600;
print "\n";
print isset($time) ? $time : 3600;
print "\n\n";
?>

Output:

PHP Notice:  Undefined variable: time in /home/hq6/PHP/Test2.php on line 2
3600
3600

3600
0

30
30
merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

No, not quite. $time ?: 3600 is similar to doing $time ? $time : 3600.

The difference would be if $time was 0. isset($time) would return TRUE so, you'd get $time (0), whereas if $time was 0 in the first one you'd get 3600.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337