根据数据库记录检查 NULL ->isset 与 is_null [英] Checking NULL against database record -> isset vs. is_null

查看:46
本文介绍了根据数据库记录检查 NULL ->isset 与 is_null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了代码,其中第一个参数是图像的路径或 MySQL 数据库中的默认 NULL,

I provided code, where first argument is path to image or default NULL in MySQL database,

function formatImage($img = NULL, $alt = NULL)
    {
        // if(isset($img)) DOESN'T WORK
        if(!$img == NULL)
        {
            return '<img src="' . $img . '" alt="' . $alt .'" />';
        }
        else
        {
            return NULL;
        }
    }

为什么 isset 不起作用?相反,我必须检查是否是 (!$img == NULL).

Why isset doesn't work? Instead I have to check if is (!$img == NULL).

如果使用带 isset 的检查参数,并且 $img 在数据库中为 NULL.作为输出,我使用空的 src 属性获取整个 HTML IMG 标签,以及从数据库中获取的实际标题的 alt 属性.将 display 属性设置为 none 给没有 src 属性的图像是不可接受的.

In case of using checking argument with isset, and that $img is NULL in database. as output im getting whole HTML IMG tag with empty src attibutes, and alt attribute which is actaully title fetched from database. Setting display property to none to images without src attribute isn't acceptable.

推荐答案

试试这个:

if($img != NULL) {
    return '<img src="' . $img . '" alt="' . $alt .'" />';
}

说明:

如果您将 !$imgNULL 进行比较,您基本上会在 $img 上进行布尔反转,然后查看是否与 NULL 进行比较 -这不是你想要的.

If you compare !$img against NULL you basically do a boolean reversal on $img and then seeing if that compares to NULL - which is not what you want.

另外,请注意你应该使用

Also, please note that you should use

if($img !== NULL)

相反.!= 将比较提供的变量的值,而 !== 将比较值和数据类型(在 NULL 的情况下可能是您最感兴趣的).

instead. != will compare the values of the variables provided, while !== will compare the values AND the datatype (which in case of NULL might be your best interest).

这篇关于根据数据库记录检查 NULL ->isset 与 is_null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆