【问题标题】:php array dereference in if statement expression [duplicate]if语句表达式中的php数组取消引用[重复]
【发布时间】:2016-07-26 00:41:14
【问题描述】:

为什么我需要在 if 语句表达式中使用花括号进行数组索引? 为什么以下是非法的?

$birthday = "1990-01-18";
$date_birth = explode("-", $birthday);
if ($date_birth[1] != "00" && $date_birth[2] != "00") {
    $monthName = date('F', mktime(0, 0, 0, $date_birth[1], 10));
    echo "$monthName $date_birth[2]";
}

但是,以下工作正常:

$birthday = "1990-01-18";
$date_birth = explode("-", $birthday);
if ($date_birth{1} != "00" && $date_birth{2} != "00") {
    $monthName = date('F', mktime(0, 0, 0, $date_birth[1], 10));
    echo "$monthName $date_birth[2]";
}

【问题讨论】:

标签: php arrays brackets curly-braces


【解决方案1】:

两个版本完全相同,as shown here

另外,php manual page on arrays 声明:

方括号和花括号可以互换使用来访问数组元素(例如,$array[42]$array{42} 都会做同样的事情 [...])

【讨论】:

    猜你喜欢
    • 2012-12-23
    • 2014-10-31
    • 1970-01-01
    • 2016-12-18
    • 2019-08-03
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多