Convert object to integer in PHP
31,955
Solution 1
Does this work?
$val = intval($total_results->getText());
Solution 2
$results_numeric = (int) $total_results;
or maybe this:
$results_numeric = $total_results->count();
Solution 3
try
class toValue {
function __toString()
{
return '3'; // you must return a string
}
}
$a = new toValue;
var_dump("$a" + 2);
result: int(5)
Solution 4
perhaps the object has a build in method to get it as an integer?
Otherwise try this very hacky approach (relys on __toString() returning that 10)
$total_results = $total_results->__toString();
$total_results = intval($total_results);
However if the object has a build in non-magic method, you should use that!
Solution 5
You can see the methods of the class here. Then you can try out the different methods yourself. There is a getText() method for example.
Author by
ryonlife
Updated on June 18, 2020Comments
-
ryonlife about 4 years
- The value of $total_results = 10
- $total_results in an object, according to gettype()
- I cannot use mathematical operators on $total_results because it's not numeric
- Tried $total_results = intval($total_results) to convert to an integer, but no luck
- The notice I get is: Object of class Zend_Gdata_Extension_OpenSearchTotalResults could not be converted to int
How can I convert to an integer?
-
Pim Jager over 15 years(int) does the same as calling intval().
-
Phil Hudson over 10 yearsBut int is 300 to 600% faster