【问题标题】:What is the meaning of the "?" in this statement in php [duplicate]“?”是什么意思?在 php 中的这个语句中 [重复]
【发布时间】:2018-05-18 23:40:13
【问题描述】:
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']) 之后

【问题讨论】:

  • 了解短 IF 语法
  • 我能有链接吗
  • 只需在 Google 中输入“php statement question mark”即可找到副本。下次请做一些适当的研究。

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


【解决方案1】:

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

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

可以转化为:

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

更多信息可以查看php operations documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2012-01-02
    • 1970-01-01
    相关资源
    最近更新 更多