Creating default object from empty value warning i

2019-08-15 03:21发布

Strict Standards: Creating default object from empty value in <--php_page_file_path--> on line <-- line_number-->

include('functions/simple_html_dom.php');
$content=str_get_html($submission);
$content->find('a', 0)->class='article-link';
$content->find('a', 0)->target='_blank';
$content->find('a', 0)->rel='nofollow';
$content->find('img', 0)->class='article-inner-image';

The variable $submission will get the POST data from a form. It may or may not contain <a> and <img> tags. When both are present the warning is not thrown. But when one or both tags are not present, it throws this warning. How can I resolve this issue?

1条回答
Animai°情兽
2楼-- · 2019-08-15 04:00

When the HTML is parsed, check if certain elements exist before altering them.

// Check if and '<a>' tag was submitted
if($a = $content->find('a', 0)) {

  // If so, set attributes on it.
  $a->class='article-link';
  ...
}

// Check if an '<img>' tag was submitted
if($img = $content->find('img', 0)) {
  ...
}
查看更多
登录 后发表回答