首页 > 其他 > 详细

lua 判断为空的逻辑

时间:2016-02-23 10:56:56      阅读:268      评论:0      收藏:0      [点我收藏+]

在查询数据库时,有可能字段数据为空,所以自然想到这样的逻辑

 ....

  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返回值都是假 

lua 判断为空的逻辑

原文:http://www.cnblogs.com/wangzhitie/p/5208917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!