<?php
isset
检测变量是否设置,且不是null,注意'\0'不是null
ps:
对数组中的元素同样有效 isset($arr['key'])
问题来了,字符串也可用数组偏移量的方式访问
$str[0],$str[1]
看看实际中会遇到什么问题
$url = 'xxx';
$httBody = http_get($url); //
/*
$httBody = <<<EOF
'<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.2.9</center>
</body>
</html>'
EOF;
*/
$data = json_decode($httBody, true);//string
if (isset($data['data'])) { //5.4之前为true
//process data
foreach ($data['data'] as $row) { // warning
}
}
可以用is_array提高兼容性
$data = json_decode($httBody, true);
if (is_array($data)) {
foreach ($data['data'] as $row) {
}
}