英文:
PHP: Has there been variations in the return of is_string() over time?
问题
我正在尝试为一个遗留的PHP应用程序做贡献,它非常向后兼容,我想要确保is_string()
的行为。
例如,我想要假设这些事实在所有PHP版本中都成立。我真诚地只关心PHP 5.4之后的版本,但了解是否有不同行为的旧版本也不错。
从PHP 5.4到PHP 8.2,对我来说必须成立的情况包括:
is_string(null)
→false
is_string([ "a" ])
→false
is_string("0")
→true
is_string("")
→true
相关文档:
- https://www.php.net/manual/en/function.is-string.php - 官方文档似乎没有提到任何重大更改
- https://onlinephp.io/ - 这个工具可能对你测试有用
- https://we.phorge.it/T15190 - 一个相关的下游讨论
- https://github.com/php/php-src - PHP源代码,其中有很多单元测试,也许你能找到与
is_string()
相关的测试,但它们似乎保存在像001.phpt
这样的文件中,对我来说不太容易从这些文件名中找到相关的测试。
英文:
I'm trying to contribute to a legacy PHP application that is very backward-compatible and I would like to be sure about the behavior of is_string()
.
For example, I would like to assume that these facts are true in all versions of PHP. I'm honestly interested only after PHP 5.4 but it's not bad to understand if there are older versions with different behavior.
These are the cases that must be true for me since PHP 5.4 to PHP 8.2:
is_string(null)
→false
is_string([ "a" ])
→false
is_string("0")
→true
is_string("")
→true
Related documents:
- https://www.php.net/manual/en/function.is-string.php - official documentation that it seems to do not mention any breaking change
- https://onlinephp.io/ - tool that could be useful to you to test stuff
- https://we.phorge.it/T15190 - a related downstream discussion
- https://github.com/php/php-src - source code of PHP in which there are a lot of Unit test and maybe you are good at finding the one related to
is_string()
but it seems they are saved in files like001.phpt
and it's not so evident to find the test from file names like these, to me
答案1
得分: 1
短回答:没有问题。
你提到的is_string()
函数的用例非常稳定,你可以在至少从PHP 4.0.6版本开始的所有已知PHP版本中依赖它们。
为了确保这一点,我所做的
我创建了这个不使用PHP的任何新功能(不使用throw new Exception
,不使用[]
等)的小测试用例:
<?php
// 报告所有PHP错误
error_reporting(E_ALL);
function assert_equals($msg, $v, $expected) {
if( $v !== $expected ) {
echo "Fail test: $msg\n";
exit(1);
}
}
assert_equals('null', is_string(null), false);
assert_equals('array', is_string(array("a")), false);
assert_equals('zero string', is_string("0"), true);
assert_equals('empty', is_string(""), true);
然后我针对以下版本进行了测试:
- PHP 4.0.6
- PHP 4.1.0
- PHP 4.2.0
- PHP 4.3.0
- PHP 4.4.0
- PHP 5.0.3
- PHP 5.1.0
- PHP 5.2.0
- PHP 5.3.0
- PHP 5.4.0
- PHP 5.5.0
- PHP 5.6.0
- PHP 7.0
- PHP 7.0.33
- PHP 7.1.0
- PHP 7.1.33
- PHP 7.2.0
- PHP 7.2.34
- PHP 7.3.0
- PHP 7.3.33
- PHP 7.4.0
- PHP 7.4.33
- PHP 8.0
- PHP 8.1
- PHP 8.2
我没有注意到任何变化。所以,再次强调,你对is_string()
的测试用例可以被认为在任何PHP版本中都是有效的。
英文:
Short answer: no problem.
The cases you mentioned of the function is_string()
are very stable and you can rely on these in all known PHP versions at least since PHP 4.0.6.
What I have done to be sure of that
I created this small test case that does not use any new feature of PHP (do not use throw new Exception
, do not use []
, etc.):
<?php
// Report all PHP errors
error_reporting(E_ALL);
function assert_equals($msg, $v, $expected) {
if( $v !== $expected ) {
echo "Fail test: $msg\n";
exit(1);
}
}
assert_equals('null', is_string(null), false);
assert_equals('array', is_string(array("a")), false);
assert_equals('zero string', is_string("0"), true);
assert_equals('empty', is_string(""), true);
And I tested it against these versions:
- PHP 4.0.6
- PHP 4.1.0
- PHP 4.2.0
- PHP 4.3.0
- PHP 4.4.0
- PHP 5.0.3
- PHP 5.1.0
- PHP 5.2.0
- PHP 5.3.0
- PHP 5.4.0
- PHP 5.5.0
- PHP 5.6.0
- PHP 7.0
- PHP 7.0.33
- PHP 7.1.0
- PHP 7.1.33
- PHP 7.2.0
- PHP 7.2.34
- PHP 7.3.0
- PHP 7.3.33
- PHP 7.4.0
- PHP 7.4.33
- PHP 8.0
- PHP 8.1
- PHP 8.2
And I have not noticed any change.
So, again, your test cases for is_string()
can be assumed as really OK for any version of PHP.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论