stevieontario Posted October 10, 2013 Share Posted October 10, 2013 Afternoon Freaks, I have a script that dies at this point: if ($a != 0) $ratio = $a/$b; My script is supposed to process a bunch of XML, almost all of which is numerical (digits). However, at one point in the XML file, the value that $a represents is "N/A". The script dies when it tries to execute line 128, i.e. at $ratio = $a/b; The error message reads: "Fatal error: Unsupported operand types in /[path/file.php] on line 128" My layman's guess is that php doesn't know what to do when I tell it to divide some number by "N/A". Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/ Share on other sites More sharing options...
stevieontario Posted October 10, 2013 Author Share Posted October 10, 2013 sorry, should have said php does not know what to do when I tell it to divide "N/A" by some number. Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453462 Share on other sites More sharing options...
Ch0cu3r Posted October 10, 2013 Share Posted October 10, 2013 Why are you dividing a string with a number? Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453463 Share on other sites More sharing options...
gizmola Posted October 10, 2013 Share Posted October 10, 2013 Correct -- you can't divide "N/A" by a number. What do you want to happen in these cases? You could first off check if it is a number with a php function like http://php.net/manual/en/function.is-numeric.php You could also force php to cast "N/A" to an integer or a real, which would be 0. $ratio = (float)$a/$b; ...or you could have some code like this: $a = ($a == 'N/A' ? 1 : $a); Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453464 Share on other sites More sharing options...
Barand Posted October 10, 2013 Share Posted October 10, 2013 You could use $a = intval($a); first, which will give 0 if $a is "N/A"; You also need to check if $b==0 or you will get "division by zero" error Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453465 Share on other sites More sharing options...
AbraCadaver Posted October 10, 2013 Share Posted October 10, 2013 (edited) I would use floatval() or (float), and this will test that both need to be a number greater than 0: if((float)$a && (float)$b) echo $ratio = $a/$b; Edited October 10, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453469 Share on other sites More sharing options...
stevieontario Posted October 10, 2013 Author Share Posted October 10, 2013 thanks everyone. How about if a value is just empty? I neglected to view the source of the XML table. In source files that work, here's how the XML looks: <Output> <Hour>1</Hour> <Energy>0</Energy> </Output> ... and here's how it looks when I'm getting that error: <Output> <Hour>1</Hour> </Output> i.e., there is no Energy data. Not even a tag. Somehow, that translates into "N/A" on the web table. How do I handle this? My script is looking for some data in the Energy tag, but there isn't even an Energy tag. Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453472 Share on other sites More sharing options...
Barand Posted October 10, 2013 Share Posted October 10, 2013 (edited) perhaps $str = "<Data> <Output> <Hour>1</Hour> <Energy>0</Energy> </Output> <Output> <Hour>1</Hour> </Output> </Data>"; $xml = simplexml_load_string($str); foreach ($xml->Output as $op) { $hr = isset($op->Hour) ? $op->Hour : 0; $en = isset($op->Energy) ? $op->Energy : 0; $ratio = floatval($hr) && floatval($en) ? $en/$hr : 0; echo $ratio.'<br>'; } Edited October 10, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1453474 Share on other sites More sharing options...
stevieontario Posted October 23, 2013 Author Share Posted October 23, 2013 thanks again everyone. Barand, using isset() might just solve my problem. Newbie question (VERY newbie) though: can I mix object oriented syntax in with conventional? i.e. I have been denoting arrays using $k => $v syntax Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1455100 Share on other sites More sharing options...
Barand Posted October 23, 2013 Share Posted October 23, 2013 I could have used foreach ($xml->Output as $k => $op) but as $k would be unused it wasn't worth the huge extra effort Quote Link to comment https://forums.phpfreaks.com/topic/282873-unsupported-operand-types-help/#findComment-1455102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.