Answers for "php null coalesce"

PHP
4

null coalescing operator php

// The null coalescing operator (??) is used to replace the ternary operation
// in conjunction with isset() function and returns its first operand if it
// exists and is NOT NULL; otherwise it returns its second operand.
$username = $_GET['username'] ?? 'not passed';

// Equivalent code using ternary operator
$username = isset($_GET['username']) ? $_GET['username'] : 'not passed';
Posted by: Guest on April-14-2021
2

php null coalesce

// if $_POST['name'] doesn't exist $result will equal to John
$result = $_POST['name'] ?? 'John';
Posted by: Guest on February-03-2020
0

null collapse operator php

$name = $_GET['name'] ?? $user_name ?: 'anonymous';
Posted by: Guest on December-11-2020

Browse Popular Code Answers by Language