is_string() of an integer or float returns false, so it might be useful to include an is_numeric() when checking if a value is stringy:
<?php
function is_stringy($val) {
return (is_string($val) || is_numeric($val)
|| (is_object($val) && method_exists($val, '__toString')));
}
?>
Test code (which should print "vector N OK" for each test vector):
<?php
foreach ([[NULL, false], [false, false], [true, false],
[0, true], [[], false], [0.1, true], ["x", true],
["", true], [new Exception("x"), true]] as $idx => $vector) {
list ($val, $expected) = $vector;
if (is_stringy($val) != $expected) {
print ("mismatch at $idx\n");
var_dump($val);
} else {
print ("vector $idx OK\n");
}
}
?>