Null coalescing operátor php

Příklady kódu

1
0

php null coalesce

// if $_POST['name'] doesn't exist $result will equal to John
$result = $_POST['name'] ?? 'John';
1
0

null coalescing operátor 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';
0
0

php 7


<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>

0
0

null collapse operátor php

        $name = $_GET['name'] ?? $user_name ?: 'anonymous';

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................