MySQLi - declaring variable after bind_param?

2019-08-07 02:45发布

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?

标签: php mysql mysqli
1条回答
一纸荒年 Trace。
2楼-- · 2019-08-07 03:19

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.

查看更多
登录 后发表回答