开发者

does ""===null?

开发者 https://www.devze.com 2023-03-21 04:57 出处:网络
when comparing string to see if it is empt开发者_如何学JAVAy, is there any difference between: if($string==NULL){

when comparing string to see if it is empt开发者_如何学JAVAy, is there any difference between:

if($string==NULL){
    //do work
}

and

if($string==""){
    /do work
}

Just wondering beacuse I want to know which one is more effective in detecting blank input.


You're kind of asking several vaguely-related questions here. PHP and JavaScript aren't the same language, and you're referencing different operators in the question title and body. In any event:

PHP:

'' == null          true
'' === null         false

JavaScript:

'' == null          false
'' === null         false

You might want to consider these tests for general "did I get something in this string variable":

PHP:

if(!empty($string)) {
    // do work
}

JavaScript:

if($string) {
    // do work
}


Yes, there is a difference. Checking if $string==Null will actually check to see if the variable has been initialized at all, and $string=="" looks to see that the string actually exists, but that it just holds a 0-length string


To test in PHP:

<?php echo var_dump("" === NULL); ?>

To test in JavaScript:

console.log("" === null)

Both produce false, so you can't do that in either language.

Even if it worked, it is not obvious what you mean by comparing with null; this isn't C where it's constantly used for missing values. If you're going to get a string as input, comparing to the empty string is more clear.


I`am using empty() function in PHP. It is not depends on type of the variable. However, when comparing with "==" (not "==="!), NULL becomes empty string ("") when comparing to string.


does “”===null?

No.

Behold the power of testing... for javascript anyway.

alert("" === null);


In JavaScript, the answer is no. An empty string does not equal null, though both are falsey values.


Check the manual, "" is not identical to null because the former is a string and the latter is null, and === checks for equal types as well as equal values.


Take a look at this: http://php.net/manual/en/language.operators.comparison.php

0

精彩评论

暂无评论...
验证码 换一张
取 消