Convert object to integer in PHP

  • Value $ total_results = 10
  • $ total_results in the object, according to gettype ()
  • I cannot use math operators on $ total_results because it is not numeric
  • Tried $ total_results = intval ($ total_results) to convert to an integer but no luck
  • The notification I receive: Object of class Zend_Gdata_Extension_OpenSearchTotal The results cannot be converted to int

How can I convert to an integer?

+4
source share
5 answers

It works?

$val = intval($total_results->getText()); 
+7
source
 $results_numeric = (int) $total_results; 

or maybe this:

 $results_numeric = $total_results->count(); 
+6
source

maybe the object has a build method to get it as an integer?

Otherwise, try this very hacky approach (relies on __toString (), returning this value 10)

 $total_results = $total_results->__toString(); $total_results = intval($total_results); 

However, if the object has a built-in method without magic, you should use it!

+1
source

Here you can see the class methods here . Then you can try various methods yourself. For example, there is a getText () method.

+1
source

to try

 class toValue { function __toString() { return '3'; // you must return a string } } $a = new toValue; var_dump("$a" + 2); 

Result: Int (5)

+1
source

All Articles