本文介绍了没有$的变量,有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在不使用$的情况下引用了变量?

is it possible that a variable is referenced without using the $?

例如:

if ($a != 0 && a == true) {
...
}

我不这么认为,但是代码(不是我写的)没有显示错误,我认为这很奇怪.我忽略了代码,并且a也不是常量.

I don't think so, but the code (not written by me) doesn't show an error and I think it's weird. I've overlooked the code and a is not a constant either.

推荐答案

在PHP中,可以定义一个常量,该常量将不具有$,但是一个变量必须具有一个.但是,这不是变量,也不能替代变量.常量只应定义一次,并且在脚本的整个生命周期中都不得更改.

In PHP, a constant can be defined, which would then not have a $, but a variable must have one. However, this is NOT a variable, and is not a substitute for a variable. Constants are intended to be defined exactly once and not changed throughout the lifetime of the script.

define('a', 'some value for a');

此外,您不能在双引号或HEREDOC字符串内插值常量的值:

Additionally, you cannot interpolate the value of a constant inside a double-quoted or HEREDOC string:

$a = "variable a"
define('a', 'constant a');

echo "A string containing $a";
// "A string containing variable a";

// Can't do it with the constant
echo "A string containing a";
// "A string containing a";

最后,PHP可能发布Use of undefined constant a - assumed 'a'的通知,并将其解释为错误引用的字符串"a".查看您的错误日志以查看是否正在发生.在这种情况下,"a" == TRUE是有效的,因为字符串"a"是非空的,并且将其与布尔TRUE进行松散比较.

Finally, PHP may issue a notice for an Use of undefined constant a - assumed 'a' and interpret it as a mistakenly unquoted string "a". Look in your error log to see if that is happening. In that case, "a" == TRUE is valid, since the string "a" is non-empty and it is compared loosely to the boolean TRUE.

echo a == TRUE ? 'true' : 'false';
// Prints true
// PHP Notice:  Use of undefined constant a - assumed 'a'

这篇关于没有$的变量,有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:10