简体   繁体   English

为什么会出现语法错误?

[英]Why am I getting a syntax error?

Why is this php statement $var1 += & $var2 wrong? 为什么这个php语句$var1 += & $var2错误?
response to comments : i'm using it to store a smart database query, in fact $var2 is my database connection, i can't drop the reference because it would mean making too many copies of my database, as for the bitwise response &= will do the increment also? 对评论的响应 :我正在使用它来存储智能数据库查询,实际上$ var2是我的数据库连接,我无法删除该引用,因为这将意味着对数据库进行过多的复制,对于按位响应&=也会增加吗?

& , in the context of your statement, is the reference operator . & ,在发言的背景下,是参考操作

From the manual: 从手册中:

References in PHP are a means to access the same variable content by different names. PHP中的引用是通过不同名称访问相同变量内容的一种方式。 They are not like C pointers; 它们不像C指针。 for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. 例如,您不能使用它们执行指针算术,它们不是实际的内存地址,依此类推。 Instead, they are symbol table aliases. 相反,它们是符号表别名。 Note that in PHP, variable name and variable content are different, so the same content can have different names. 请注意,在PHP中,变量名称和变量内容不同,因此相同的内容可以具有不同的名称。

You cannot add a reference to something, which is why your code isn't working. 您无法添加对某些内容的引用,这就是您的代码无法正常工作的原因。

Since & has two meanings in PHP depending, and it is unclear from your question, here are two possibilities for fixing your code, depending on what you were trying to do. 由于&在PHP中具有两种含义,取决于您的问题,目前尚不清楚,因此有两种方法可以修复代码,具体取决于您要执行的操作。

  1. If you wanted to add the value of a reference to $var1 : 如果要将引用的值添加到$var1

     $var2 = &$reference; // whatever it's supposed to be a reference to $var1 += $var2; 
  2. If you wanted to shortcut a bitwise and : 如果您想按位快捷键

     $var1 &= $var2; 

The & means to pass a variable by reference. &表示通过引用传递变量。 It's used in function signatures. 在函数签名中使用。

Also, this just gives a syntax error. 另外,这只会产生语法错误。

Sorry to dissapoint you, but the correct answer to my problem was that my connection was returning an array, and the operation was not allowed. 抱歉让您失望,但对我的问题的正确答案是我的连接正在返回一个数组,并且不允许该操作。 Sorry for not sharing all my code, that must have misguided you... 对不起,我没有分享我的所有代码,这肯定会误导您...

您有两个运算符彼此相邻尝试:

$var1 = $var1 + ($var1 & $var2) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM