Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if item can be converted to string?

I am writing a debug method.

What I have is

if(is_xxx($item)){  //echo output info for type } 

what I want to do at the end is

if(can_be_string($item)) echo $item; 

Is there a can_be_string type function?

like image 703
Hailwood Avatar asked Mar 31 '11 07:03

Hailwood


1 Answers

Ok, edited, with incorporating Michiel Pater's suggestion (who's answer is gone now) ans @eisberg's suggestions. settype will return true with objects no matter what, as it seems.

if(     ( !is_array( $item ) ) &&     ( ( !is_object( $item ) && settype( $item, 'string' ) !== false ) ||     ( is_object( $item ) && method_exists( $item, '__toString' ) ) ) ) {     echo $item; } 
like image 145
Decent Dabbler Avatar answered Oct 19 '22 07:10

Decent Dabbler