note 125192 added to function.is-string
| From: | jcours.numifinancial.com via php-notes | Date: | Wed, 15 Jul 2020 19:59:49 +0000 |
| Subject: | note 125192 added to function.is-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214872@lists.php.net to get a copy of this message | ||
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");
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.167.36 (proxied: 75.80.44.201)
----
Manual Page -- http://php.net/manual/en/function.is-string.php
Edit -- https://master.php.net/note/edit/125192
Del: integrated -- https://master.php.net/note/delete/125192/integrated
Del: useless -- https://master.php.net/note/delete/125192/useless
Del: bad code -- https://master.php.net/note/delete/125192/bad+code
Del: spam -- https://master.php.net/note/delete/125192/spam
Del: non-english -- https://master.php.net/note/delete/125192/non-english
Del: in docs -- https://master.php.net/note/delete/125192/in+docs
Del: other reasons-- https://master.php.net/note/delete/125192
Reject -- https://master.php.net/note/reject/125192
Search -- https://master.php.net/manage/user-notes.php