尝试获取非对象的 属性 -> 如何检查对象是否存在
Trying to get property of non-object -> how can I check Object Exists
在使用 Dom 文档时如何检查对象是否存在,因为我正在获取
error "Trying to get property of non-object"
$getplacename = '//table//tr';
$getplacename2 = $xpath->query($getplacename);
//loop through each individual card
foreach($getplacename2 as $getplacename22) {
echo $getplacename22 ->childNodes->item(0)->childNodes->item(2)->textContent;
echo $getplacename22 ->childNodes->item(4)->textContent;
echo "<br /><br /><br />";
}
好吧,我认为最简单的方法是将代码更改为类似这样的内容(假设错误发生在以下行之一):
foreach($getplacename2 as $getplacename22) {
if ($getplacename22->childNodes->item(0) != null && $getplacename22->childNodes->item(0)->childNodes->item(2) != null) {
echo $getplacename22->childNodes->item(0)->childNodes->item(2)->textContent;
}
if($getplacename22 ->childNodes->item(4) != null) {
echo $getplacename22->childNodes->item(4)->textContent;
}
echo "<br /><br /><br />";
}
在使用 Dom 文档时如何检查对象是否存在,因为我正在获取
error "Trying to get property of non-object"
$getplacename = '//table//tr';
$getplacename2 = $xpath->query($getplacename);
//loop through each individual card
foreach($getplacename2 as $getplacename22) {
echo $getplacename22 ->childNodes->item(0)->childNodes->item(2)->textContent;
echo $getplacename22 ->childNodes->item(4)->textContent;
echo "<br /><br /><br />";
}
好吧,我认为最简单的方法是将代码更改为类似这样的内容(假设错误发生在以下行之一):
foreach($getplacename2 as $getplacename22) {
if ($getplacename22->childNodes->item(0) != null && $getplacename22->childNodes->item(0)->childNodes->item(2) != null) {
echo $getplacename22->childNodes->item(0)->childNodes->item(2)->textContent;
}
if($getplacename22 ->childNodes->item(4) != null) {
echo $getplacename22->childNodes->item(4)->textContent;
}
echo "<br /><br /><br />";
}