简体   繁体   中英

Why am I getting a syntax error?

Why is this php statement $var1 += & $var2 wrong?
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?

& , 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. They are not like C pointers; 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.

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.

  1. If you wanted to add the value of a reference to $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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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