php - Variables that can be echoed -
i created function know if can pass variable language structure echo
useful output (for example, arrays output array
, not see useful).
function echo_able($var) { if (is_string($var)) return true; if (is_numeric($var)) return true; if (is_object($var) && method_exists($var, '__tostring')) return true; return false; }
is there other types forgot?
because php bit messy, can not find way know sure types of variable can echoed.
the php documentation doesn't list can echoed.
any variable can echoed. when that, content converted string, using language rules. problem not can't echo something, maybe not result want. need pay attention type of conversion happen default , if want.
edit: looks objects can not converted string unless there __tostring
method implementation. manual, can see behavior changed.
it worth noting before php 5.2.0 __tostring() method called when directly combined echo or print. since php 5.2.0, called in string context (e.g. in printf() %s modifier) not in other types contexts (e.g. %d modifier). since php 5.2.0, converting objects without __tostring() method string cause e_recoverable_error.
Comments
Post a Comment