PHP函数”func_get_arg”和”func_get_args”如何从PHP 5.x更改为PHP 7.x

除非你的项目中有很多不推荐使用的代码已在最新版本中删除, 否则升级项目的PHP版本通常并不复杂。许多开发人员不希望使用较小的版本(例如从5.20到5.29)更改版本的事情之一就是更改内置函数的行为, 但是对于较大的版本(即新的PHP 7), 可能期望某些事情可能会破裂。在这种情况下, func_get_arg函数略有变化。要了解功能行为如何变化, 请分析以下PHP代码段:

<?php

function printValue($value) {
    // Update value variable
    $value = "The value is: " . $value;

    // Print the value of the first argument
    echo func_get_arg(0);
}

// Run function
printValue(123);

由于func_get_arg函数, 函数printValue将打印发送给该函数的第一个参数。如果在不同的PHP版本上运行代码, 则输出将不同。在PHP <= 5.x中, 输出为:

123

但是, 在PHP> = 7.x中, 输出将是:

The value is: 123

但为什么?

从现在开始, 在PHP 7.x中, 函数func_get_arg和func_get_args不会返回作为参数提供的原始值, 而是将提供参数的当前值(显然, 它们可能已被你自己的代码故意修改了) 。如果函数的运行时没有修改参数的值, 那么你不必担心这个小问题。或者, 你可以通过在范围中创建一个新变量来解决此问题, 该变量存储了相同的参数值但有修改, 或者在将其原始值检索到另一个变量后进行了修改:

<?php

function printValue($value) {
    // Stores the modified value (2)
    $modifiedValue = $value + 1;
    
    echo func_get_arg(0);
}

// In any PHP prints: 1
printValue(1);

编码愉快!

来源:

https://www.srcmini02.com/60089.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37862.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?