php - PHP 中的静态函数变量和连接

标签 php string concatenation static-variables

考虑以下几点:

$var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.

一旦我将 $var 标记为 static,但是:

static $var = 'foo' . 'bar';

PHP(WAMP 设置上的 5.3.1)提示以下错误:

Parse error: syntax error, unexpected '.', expecting ',' or ';'

似乎字符串连接是这里的罪魁祸首。


这是怎么回事?谁能给我解释一下静态变量的规则?

最佳答案

手册指出, Variables scope :

Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.

Static keyword 中也提到了它:

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.

尽管应该注意,无论是否为静态属性,都不能使用表达式进行初始化。

关于php - PHP 中的静态函数变量和连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4976717/

相关文章:

php - 检查表中是否存在逗号分隔值

php - 在网页上创建动态网格系统

java - 如何从 String2 中删除 String1 并用剩余的字符串构建新的 String2?

javascript - 从字符串 javascript 中删除选项卡 ('\t' )

php - 在 php 中使用正则表达式来选择并回显波斯字符

MySQL:多遍字符串连接

php - 比较 PHP 服务器时间与 JavaScript 时间(客户端浏览器时间)

php - 如果表只有一列,如何检查 sql?

c# - SQL Server 中串联的最佳实践?

sql - 如何在 BigQuery 中将 CONCAT 与 + 结合使用