php - Avoid cannot pass reference error in function -


I have a function where arguments with a reference (& amp;) are argued, however, If I set it to zero, I get an error:

"Parameter 1 can not be passed in context ..."

This script is:

< & lt ;? Php function doSomething (and $ reference, $ arg1, $ arg2) {if ($ reference! == NULL) {// do something with reference} else {// do something else}} // Call with reference Something ($ A, $ b, $ c); // call without reference / something / (zero, $ B, $ C); ? & Gt;

It seems to me that due to this fact it seems that the zero has not been declared as a variable, but it was immediately declared in the function. This works when I do this:

  $ nullVar = NULL; DoSomething ($ nullVar, $ b, $ c);  

How do I edit my function, so when I tap into the function, the reference will not show error, and reference only when possible (since I do not use context It is set to NULL)

If you swap the position of arguments then you are still an alternative Can create references:

  function doSomething ($ arg1, $ arg2, and $ reference = null) {if ($ reference! == NULL) {// do something with reference} else {// Do Uc and}} $ a = 'a'; $ B = 'B'; DoSomething ($ a, $ b);  

But going through null for that third argument will still be an error.

  doSomething ($ a, $ b, null); // error  

In addition, the references can be satisfied with newly created variables. Since the variable with no specified values ​​is by default null , you can provide a variable later without any intent to use it.

  doSomething ($ a, $ b, $ unused_var);  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -