PHP and variable variables ($$) syntax

一曲冷凌霜 提交于 2020-01-25 18:26:34

问题


Before upgrading to PHP 7, I had this code and it returned true

var_dump(isset($$_SESSION['payment']) );
var_dump(is_object($$_SESSION['payment'])); 
var_dump($_SESSION['payment']); // string 'moneyorder'

After upgrading to PHP 7, I rewrote the same code inside a class, but now it returns false

var_dump(isset(${$_SESSION['payment']})); 
var_dump(is_object(${$_SESSION['payment']}));
var_dump($_SESSION['payment']); // string 'moneyorder'

Do you have an idea why ?

Thank you


回答1:


Note the PHP documentation for superglobals contains this warning:

Note: Variable variables

Superglobals cannot be used as variable variables inside functions or class methods.

Save it to a local variable instead:

$payment = $_SESSION['payment'];
var_dump(isset(${$payment})); 
var_dump(is_object(${$payment}));


来源:https://stackoverflow.com/questions/37622919/php-and-variable-variables-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!