lua 判斷為空的邏輯


在查詢數據庫時,有可能字段數據為空,所以自然想到這樣的邏輯

 ....

  local publish_total_count = rows[i].PUBLISH_COUNT;

....
if publish_total_count ==nil then
yx_new_table.submit_info = ngx.encode_base64("0/0");
else
yx_new_table.submit_info = ngx.encode_base64(submit_count.."/"..publish_total_count);
end

但是總是在publish_total_count 為空時 執行第二句。

問了一下同事,要修改為
if not publish_total_count then
yx_new_table.submit_info = ngx.encode_base64("0/0");
else
yx_new_table.submit_info = ngx.encode_base64(submit_count.."/"..publish_total_count);
end

其中 not publish_total_count 表示publish_total_count 為空的時候才執行 yx_new_table.submit_info = ngx.encode_base64("0/0");
原因在lua中 nil和false返回值都是假 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM