title: Notice: Trying to get property of non-object in PHP while printing into HTML Table
body: <p>I currently this error while parsing for Stock price from Yahoo. 
  I receive only for some stocks however. 
  I've looked through some other posts and haven't been able to resolve this. </p>
  
  <p>Here's my scraper code:</p>
  
  <pre><code>&lt;?php
  //session_start();
  #$stockname = "COALINDIA.NS";
  $stockname = "AAPL";
  #$stockname = "GOOG";
  #$stockname = "TCS.NS";
  
  $url = 'https://finance.yahoo.com/quote/' . $stockname;
  
  $data = file_get_contents($url);
  $dom = new DOMDocument();
  libxml_use_internal_errors(true);
  $dom-&gt;loadHTML($data);
  libxml_clear_errors();
  $dom-&gt;saveHTML();
  
  $xpath = new DOMXPath($dom);
  $price = $xpath-&gt;query('//*[@id="quote-header-info"]/div[3]/div[1]/div/span[1]/text()');
  $sign = $xpath-&gt;query('//*[@id="quote-header-info"]/div[3]/div[1]/div/span[2]/text()');
  $change = $xpath-&gt;query('//*[@id="quote-header-info"]/div[3]/div[1]/div/span[2]/text()[2]');
  
  $current_price = $price-&gt;item(0)-&gt;nodeValue;
  $gain_and_percent = $sign-&gt;item(0)-&gt;nodeValue . $change-&gt;item(0)-&gt;nodeValue;
  echo $current_price;
  echo "&lt;br&gt;";
  echo "Gain (Gain %):  ". $gain_and_percent;
  ?&gt;
  </code></pre>
  
  <p>The error I receive is:
  Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/Portfolio-Manager/scripts/scraper.php on line 27</p>
  
  <p>Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/Portfolio-Manager/scripts/scraper.php on line 28</p>
  
  <p>This refers to the lines where I assign the variable $current_price and $gain_and_percent. I do not receive this notice when I echo values separately, however I do when I assign into a HTML Table as follows:</p>
  
  <pre><code>&lt;?php
  while ($record = mysqli_fetch_assoc($result)) {
  ?&gt;
  &lt;td&gt;
  &lt;?php
  $stockname = $symbol;
  require 'scripts/scraper.php';
  echo $current_price;
  ?&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>Thank you for looking over my code and your time. </p>