I am using the PHP Simple HTML DOM Parser to pull information from a webpage. One of the values that I retrieve is a number. Here's how I retrieve the value:
$num = $html->find('a[id=reviews_link] span');
Then if I want to print the number(s):
foreach($num as $val){
echo $val;
}
This works properly and prints the correct number(s) from the original page. However, if I want to use the number in a logical statement, it does not work. So for example if there is a single number returned and its value is 20, when I do this:
foreach($num as $val){
if($val > 15){
echo $val." is greater than 15";
}
}
The if statement is not triggered and nothing prints. The same happens if I add quotes around the variable ("$val"
). I have also tried using various PHP functions like intval
or float
but that doesn't change anything.
How can I get this variable to be treated as a numerical value?
When I print_r($val)
there is a super long simple_html_dom_node Object
that prints, it appears to contain a bunch of arrays.
$val
is actually a simple_html_dom_node
object, and echo $val;
only works as this class implements the magic __toString()
method. This only works in certain situations though, but especially not in comparisons.
Here you need to refer to actual object properties, here probably
if ($val->outertext > 15)
or
if ($val->plaintext > 15)
See also the "Magic Attributes" tab under http://simplehtmldom.sourceforge.net/manual.htm#section_access