Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farahandev's avatar

A silly question on PHP.

I know it's a silly question, however, I don't know why we use that. That's why I am asking. Don't mind plz.

I saw people are using ?? in Laravel / PHP. Why exactly we use that?

1 like
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@farahandev

It's called Null Coalescing Operator in PHP. If need to check whether the value is null or not, that time you can use that.

Here is the example-

// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';

// The above is identical to this if/else statement
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'default';
}

Ref: http://us2.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

9 likes

Please or to participate in this conversation.