"是什么意思?"在PHP的这个声明中

时间:2017-12-05 10:33:55

标签: php class if-statement foreach session-cookies

function countproduct(){
        $count = 0;
        $cart = isset($_SESSION['cart']) ? $_SESSION['cart']:array();
        foreach($cart as $row):
            if($row['qty']!=0){
                $count = $count + 1;
            }
        endforeach;

        return $count;

我想知道什么意思?在isset之后($ _ SESSION [' cart'])

1 个答案:

答案 0 :(得分:1)

这是一个三元运算符,这些行:

$cart = isset($_SESSION['cart']) ? $_SESSION['cart']:array();

可以转换为:

if (isset($_SESSION['cart'])) {
    $cart = $_SESSION['cart'];
} else {
    $cart = array();
}

有关详细信息,请查看php operations documentation