检查变量是否存在并且 === 真 [英] Check if Variable exists and === true

查看:27
本文介绍了检查变量是否存在并且 === 真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否:

  • 数组isset中的一个字段
  • 字段 === 真

是否可以用一个 if 语句来检查这一点?

检查 === 是否可以解决问题,但会抛出 PHP 通知.我真的必须检查该字段是否已设置,然后是否为真?

解决方案

如果你想在一个声明中得到它:

if (isset($var) && ($var === true)) { ... }


如果您希望它在单一条件:

好吧,您可以忽略该通知(也就是使用 error_reporting() 函数).

或者你可以用邪恶的 @ 字符:

if (@$var === true) { ... }

<块引用>

此解决方案不推荐

I want to check if:

  • a field in the array isset
  • the field === true

Is it possible to check this with one if statement?

Checking if === would do the trick but a PHP notice is thrown. Do I really have to check if the field is set and then if it is true?

解决方案

If you want it in a single statement:

if (isset($var) && ($var === true)) { ... }


If you want it in a single condition:

Well, you could ignore the notice (aka remove it from display using the error_reporting() function).

Or you could suppress it with the evil @ character:

if (@$var === true) { ... }

This solution is NOT RECOMMENDED

这篇关于检查变量是否存在并且 === 真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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