← CodePrimer
PHP Beginner Basics

Ternary and ??

How do I assign with a short conditional?

?: and null coalescing.

Sample
<?php
$role = ($isAdmin ?? false) ? 'admin' : 'user';
$name = $_GET['name'] ?? 'Guest';

Notes

?? only replaces null/undefined index — not empty string.

Keywords: ternary · null coalescing · ?? · ??=

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share