php - 如何访问有空格的变量?

标签 php variables variable-variables

我很好奇如何访问包含空格或特殊字符的变量。

有什么办法吗?

<?php
$first_day = "monday";
$$first_day = "first day of the week";
echo $monday." <br />";
$days = 'saturday sunday';
$$days = 'days of the weekend';
// how to echo $days of the weekend ???
print_r(get_defined_vars());

最佳答案

根据PHP docs :

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

也就是说,严格来说,包含空格或特殊字符的变量名称不是有效的变量名称。

话虽这么说,@mario 的评论提供了解决方法:

echo ${'saturday sunday'}; // echoes 'days of the weekend'

关于php - 如何访问有空格的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32339411/

相关文章:

php - 如何更改 WooCommerce 缩略图裁剪位置?

delphi - 为什么我的全局变量在调试时是 "inaccessible"?

php - 尝试从字符串名称访问 $_SERVER(或任何全局)变量

java - 如何访问基于数组中字符串的方法?

php - 通过更改查询字符串参数在 Symfony2 中进行简单重定向,与路由无关

javascript - 根据下拉响应更新div而不刷新

php - 使用 OOP 在 PHP 中组合大量函数(某种 namespace )?

mySQL 中包含 mySQL 方程的 PHP 变量

variables - 为什么我可以使用可变变量,使其生命周期与不可变引用重叠,但我不能以相同的方式使用可变引用?