我正在使用这个三元运算符:
$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 ?
你应该在PHP中阅读the difference和print
之间的echo
。 Tl; dr使用print代替。
$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.';
关于什么
echo(
$this->checkIfProductCategoriesContainsString($productId, $categoryNeedle) !== false
? "Category containing categoryNeedle exists"
: "Category does not exist."
);