Message: Illegal string offset

2020/05/27 posted in  日常问题

示例

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$a = '';

echo $a['k'];

// Warning: Illegal string offset 'k'

$a = [];

echo $a['k'];

// Notice: Undefined index: k

总结

  • 尝试把字符串当成数组取元素时,会报错:Warning: Illegal string offset
  • 尝试读取数组不存在的元素时,会报错:Notice: Undefined index: k