openfrog
openfrog

Reputation: 40735

Is it fine with PHP to assign NULL here?

Example:

public function prepare($sql, &$p = NULL) {

p is assigned by reference. I'm not sure if I can assign safely NULL here as default. Is that ok in this case?

Upvotes: 2

Views: 135

Answers (3)

Alex Weber
Alex Weber

Reputation: 2186

The code will run perfectly fine even with error_reporting set to E_ALL & E_STRICT.

And regarding the whole pass-by-reference with a default value of null, I just see it as an optional variable, and thats ok. You should definitely ask yourself whether you really need the reference because php is pretty smart about handling this type of stuff for you!

Upvotes: 1

newacct
newacct

Reputation: 122429

Why would you want to have a default value for a parameter that is passed by reference? Isn't the basic point of pass-by-reference that you can change the original variable in the caller's scope? So if it has a default value, then you are not changing anything in the caller's scope, so what's the point?

Upvotes: 3

GSto
GSto

Reputation: 42350

compile it and see :)

If the function can work properly with $p being NULL, then you don't have any problems.

Upvotes: 0

Related Questions