2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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 -- 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.