简体   繁体   English

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

[英]What's the difference between if(!$variable) and if(isset($variable))?

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

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). isset($var)返回变量是否存在且不为null,其中!$var告诉您该变量是否为true ,或者是否为任何计算结果为true变量(例如非空字符串)。 This is summarized in the first table of this documentation page . 这在本文档页面的第一个表中进行了总结。

Also, using !$var will output a notice that you're using an undefined variable, whereas isset($var) won't do that. 另外,使用!$var将输出一个通知,表示您正在使用未定义的变量,而isset($var)将不会这样做。

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)时将收到警告。

They are two different statements 它们是两种不同的陈述

  1. in the first you check if a variable is false hence the '!' 在第一个你检查变量是否为false因此'!'
  2. here you check if a variable is actually set to some value other then null. 在这里检查一个变量是否实际设置为某个值,然后是null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 if($ variable)和if(isset($ variable))之间的区别 - Difference between if($variable) and if(isset($variable)) PHP中“ true == isset($ variable)”和“ isset($ variable)== true”之间的区别 - Difference between “true == isset($variable)” and “isset($variable) == true” in php isset()和__isset()有什么区别? - What is the difference between isset() and __isset()? PHP中的'isset()'和'?empty()'有什么区别? - What's the difference between 'isset()' and '!empty()' in PHP? $ variable和%$ variable%之间有什么区别? - What is the difference between $variable and %$variable%? isset和empty之间有什么区别? - What is the difference between isset and empty? file_get_contents变量和字符串之间有什么区别? - What's the difference between file_get_contents a variable and a string? PHP中“ Reflection”和“ Variable variables”有什么区别 - What's the difference between 'Reflection' vs 'Variable variables' in PHP isset($ var)==“ Test”和isset($ var)&amp;&amp; $ var ==&#39;Test“和有什么不一样 - What is the difference between isset($var) == “Test” and isset($var) && $var == 'Test" PHP:for循环中的静态变量和for循环外的非静态变量之间有什么区别? - PHP: What's the difference between static variable inside a for loop and a non-static variable outside the for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM