問題描述
這可能是一個完全愚蠢的問題,但我看過幾個例子,在將變量放入 bind_param 后聲明變量:
http://devzone.zend.com/article/686
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?
推薦答案
這是可能的,因為綁定的是對相關變量的引用,但我發現它的風格非常糟糕:
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.
在聲明之前綁定一個變量會拋出一個(不,它沒有.干杯@webbiedave)E_NOTICE
消息
Binding a variable before declaring it will throw an (No it doesn't. Cheers @webbiedave)E_NOTICE
message
如果您問我,為了將來的可讀性,查詢應該在一個地方構建,然后立即執行.
If you ask me, a query should be built in one place, and then executed straight away, for the sake of future readability.
這篇關于MySQLi - 在 bind_param 之后聲明變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!