我正在使用此三元运算符:
$this->checkIfProductCategoriesContainsString($productId, $categoryNeedle) !== false ? echo "Category containing categoryNeedle exists" : echo "Category does not exist.";
我也这样尝试:
($this->checkIfProductCategoriesContainsString($productId, $categoryNeedle) !== false) ? echo "Category containing categoryNeedle exists" : echo "Category does not exist.";
但我的IDE说unexpected echo after ?
呢
echo(
$this->checkIfProductCategoriesContainsString($productId, $categoryNeedle) !== false
? "Category containing categoryNeedle exists"
: "Category does not exist."
);
您应该在PHP中阅读有关print
和echo
之间的差异。tl; dr使用打印。
$this->checkIfProductCategoriesContainsString($productId, $categoryNeedle) ?
print "Category containing category needle exists" :
print "Category does not exist.";
,但最好只有:
echo $this->checkIfProductCategoriesContainsString($productId, $categoryNeedle) ?
'Category containing category needle exists' :
'Category does not exist.';