Skip to content

Incomplete statement about the Null Coalescing Operator #5526

@Krause-a

Description

@Krause-a

From manual page: https://php.net/language.operators.comparison


In the Null Coalescing Operator section, the documentation states "The expression (expr1) ?? (expr2) evaluates to expr2 if expr1 is null, and expr1 otherwise." which is not fully correct. The next sentence states it suppresses notices and warning for non existent expr1, just like isset. The code block above has an "identical" example given of an array access situation with ?? and with if/else block using isset. This led me to believe that I could substitute that array access for any expression and it would still be identical. This is not the case. This next code block appears to be a proper equivalence example.

<?php
// Example usage for: Null Coalesce Operator
$result = _expr_ ?? 'default';

// The above is identical to this if/else statement
if (isset(_expr_)) {
	$tmp_var = _expr_;
	if ($tmp_var !== null) {
		$result = $tmp_var;
	}
	else {
		$result = 'default';
	}
} else {
    $result = 'default';
}
?>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions