關於PHP字元與數組的疑義

來源:互聯網
上載者:User
關於PHP字元與數組的疑問
$str="123456";
var_dump($str["abc"]);

為什麼結果是第一個字元而不是null 而且我unset($str["abc"])又提示沒這個下標。
------解決方案--------------------
字串不是數組,只有位置,沒有下標和關聯鍵
數組相關函數不能用於字串

因此 $str["abc"] 中的 "abc" 被轉換成數值 0
對於 $str="123456"; 而言 $str["abc"] 就是 $str[0],所以得到 1

雖然訪問字串的某個位置可以寫作 $str[$i]
但是為防止歧義,php 建議你這麼寫 $str{$i}
------解決方案--------------------
手冊 比較子 一節
------------------------------------------
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("1" == "1e0"); // 1 == 1 -> true
switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
?>
------------------------
switch那個例子就很明顯了

字串索引不能用“字串”,但php把它“智能”地處理了變成0
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.