tags:

views:

34

answers:

1

This may be a completely dumb question, but I've seen a couple examples declaring the variables AFTER putting them in bind_param:
http://devzone.zend.com/article/686

I've never seen this done before and all my programming knowledge says I should define them before hand. Is this a valid/preferred way?

+2  A: 

This is possible, because what gets bound is a reference to the variable in question, but I find it horribly bad style:

  • It makes code harder to read, maintain and debug - the variable could be changed further down the line, or even in other functions called in between the binding and the query.

  • Binding a variable before declaring it will throw an E_NOTICE message (No it doesn't. Cheers @webbiedave)

If you ask me, a query should be built in one place, and then executed straight away, for the sake of future readability.

Pekka
@Kerry: Also, just for clarity, the variable is not being *declared* after bounding, but rather, *assigned* a value after bounding. It's declaration occurs in the function call (because it's passed by reference).
webbiedave
@webbiedave -- thanks. That makes more sense, which is why if it is undeclared it throws the E_NOTICE warning, but as its passed by reference it doesn't matter when it's declared as long as its before the execute.
Kerry
@webbiedave you're right, corrected my answer.
Pekka