PHP空合并运算符混淆

PHP空合并运算符混淆,php,output,operator-keyword,Php,Output,Operator Keyword,我有点困惑。 报告说: 但我自己的例子说明了一些非常不同的情况: echo "<pre>"; $array['intValue'] = time(); $array['stringValue'] = 'Hello world!'; $array['boolValue'] = false; $resultInt = isset($array['intValue']) ?? -1; $resultString = isset($array['stringValue']) ?? 'An

我有点困惑。 报告说:

但我自己的例子说明了一些非常不同的情况:

echo "<pre>";
$array['intValue'] = time();
$array['stringValue'] = 'Hello world!';
$array['boolValue'] = false;


$resultInt = isset($array['intValue']) ?? -1;
$resultString = isset($array['stringValue']) ?? 'Another text';
$resultBool = isset($array['boolValue']) ?? true;

var_dump($resultInt);
var_dump($resultString);
var_dump($resultBool);

echo '<br/>';

if(isset($array['intValue'])) $_resultInt = $array['intValue'];
else $_resultInt = -1;

if(isset($array['stringValue'])) $_resultString = $array['stringValue'];
else $_resultString = 'Another text';

if(isset($array['boolValue'])) $_resultBool = $array['boolValue'];
else $_resultBool = true;


var_dump($_resultInt);
var_dump($_resultString);
var_dump($_resultBool);

echo "</pre>";
因此,正如我的示例所示,if条件的结果与null合并运算符的结果不一样,正如文档所述。谁能解释一下,我做错了什么

谢谢大家!

您正在做:

$resultInt = $array['intValue'] ?? -1;
$resultString = $array['stringValue'] ?? 'Another text';
$resultBool = $array['boolValue'] ?? true;
但是
的要点是它为您执行
isset
调用。因此,试试这个,只需在不使用
isset
的情况下输入值即可:

你正在做:

$resultInt = $array['intValue'] ?? -1;
$resultString = $array['stringValue'] ?? 'Another text';
$resultBool = $array['boolValue'] ?? true;
但是
的要点是它为您执行
isset
调用。因此,试试这个,只需在不使用
isset
的情况下输入值即可:

$resultInt = isset($array['intValue']) ?? -1;
$resultString = isset($array['stringValue']) ?? 'Another text';
$resultBool = isset($array['boolValue']) ?? true;
$resultInt = $array['intValue'] ?? -1;
$resultString = $array['stringValue'] ?? 'Another text';
$resultBool = $array['boolValue'] ?? true;