if(!$ variable)和if(isset($ variable))之间有什么区别? [英] What's the difference between if(!$variable) and if(isset($variable))?

查看:107
本文介绍了if(!$ variable)和if(isset($ variable))之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(!$variable)if(isset($variable))有什么区别?

推荐答案

嗯,答案很简单. isset($var)返回变量是否存在且不为null,其中!$var告诉您该变量是true还是计算为true的任何值(例如非空字符串). 此文档页面的第一张表中对此进行了总结.

Well, the answer is pretty simple. isset($var) returns whether or not a variable exists and is not null, where !$var tells you if that variable is true, or anything that evaluates to true (such as a non-empty string). This is summarized in the first table of this documentation page.

此外,使用!$var会输出一条通知,说明您正在使用未定义的变量,而isset($var)不会这样做.

Also, using !$var will output a notice that you're using an undefined variable, whereas isset($var) won't do that.

请记住,他们是两回事:

Mind you, they are two different things:

<?php
var_dump( isset($foo) ); // false.
var_dump( !$foo );       // true, but with a warning.

$foo = false;
var_dump( isset($foo) ); // true
var_dump( !$foo );       // true.

这篇关于if(!$ variable)和if(isset($ variable))之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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