PHP: is_string() 函数的返回值是否随时间变化而有所不同?

huangapple go评论102阅读模式
英文:

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

相关文档:

英文:

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:

答案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.):

&lt;?php
// Report all PHP errors
error_reporting(E_ALL); 

function assert_equals($msg, $v, $expected) {
    if( $v !== $expected ) {
        echo &quot;Fail test: $msg\n&quot;;
        exit(1);
    }
}

assert_equals(&#39;null&#39;,        is_string(null),       false);
assert_equals(&#39;array&#39;,       is_string(array(&quot;a&quot;)), false);
assert_equals(&#39;zero string&#39;, is_string(&quot;0&quot;),        true);
assert_equals(&#39;empty&#39;,       is_string(&quot;&quot;),         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.

huangapple
  • 本文由 发表于 2023年3月31日 20:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定