Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

use ternary operator as null coalescing operator in php

<?php

$a = null;

print $a ?? 'b'; // b
print "
";

print $a ?: 'b'; // b
print "
";

print $c ?? 'a'; // a
print "
";

print $c ?: 'a'; // Notice: Undefined variable: c in /in/apAIb on line 14
print "
";

$b = array('a' => null);

print $b['a'] ?? 'd'; // d
print "
";

print $b['a'] ?: 'd'; // d
print "
";

print $b['c'] ?? 'e'; // e
print "
";

print $b['c'] ?: 'e'; // Notice: Undefined index: c in /in/apAIb on line 33
print "
";
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #ternary #operator #null #coalescing #operator #php
ADD COMMENT
Topic
Name
1+7 =