PHP如果变量为空,则don';不要展示任何东西,不要';t显示错误
我正在使用PHP如果变量为空,则don';不要展示任何东西,不要';t显示错误,php,if-statement,simple-html-dom,is-empty,Php,If Statement,Simple Html Dom,Is Empty,我正在使用simple\u html\u dom从url获取链接,但我无法解决这个问题 <?php $title_name_lower = strtolower($title_name); $file_headers = @get_headers($title_name_lower); if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
simple\u html\u dom
从url获取链接,但我无法解决这个问题
<?php
$title_name_lower = strtolower($title_name);
$file_headers = @get_headers($title_name_lower);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
echo '<center><strong style="color: #dd3333;">No links found :( </strong><center>';
}
else {
$title_url = file_get_html(''.$title_name_lower.'');
$items = array();
$items_text = array();
foreach($title_url->find('table[class=alternate_color variousfont] tr td a') as $item) {
$items[] = $item->href.'<br>';
$items_text[] = $item->plaintext.'<br>';
}
//print_r($items_text);
//print_r($items);
$m_title_1 = file_get_html(''.$items[3].'');
$m_title_2 = file_get_html(''.$items[6].'');
$m_title_3 = file_get_html(''.$items[9].'');
$m_title_4 = file_get_html(''.$items[12].'');
$m_title_5 = file_get_html(''.$items[15].'');
$m_title_6 = file_get_html(''.$items[27].'');
$m_title_7 = file_get_html(''.$items[30].'');
$link_1 = $m_title_1->find('div[class=post-single-content box mark-links] a', 0)->href;
$link_2 = $m_title_2->find('div[class=post-single-content box mark-links] a', 0)->href;
$link_3 = $m_title_3->find('div[class=post-single-content box mark-links] a', 0)->href;
$link_4 = $m_title_4->find('div[class=post-single-content box mark-links] a', 0)->href;
$link_5 = $m_title_5->find('div[class=post-single-content box mark-links] a', 0)->href;
$link_6 = $m_title_6->find('div[class=post-single-content box mark-links] a', 0)->href;
$link_7 = $m_title_7->find('div[class=post-single-content box mark-links] a', 0)->href;
}
echo $link_1.' - '.$items_text[3];
echo $link_5.' - '.$items_text[15];
echo $link_7.' - '.$items_text[30];
?>
我认为你需要做这样的事情
if (is_object($m_title_7)) {
$link_7 = $m_title_7->find('div[class=post-single-content box mark-links] a', 0)->href;
} else {
$link_7 = "";
}
这将首先检查$m_title_7是否是对象,如果是,则将设置$link_7
编辑将else添加到空$link_7