如何确定false和0之间的差异?

时间:2016-04-18 11:52:10

标签: php if-statement

我有一个变量,它总是包含以下两种情况之一:

  • 'date can be in any form and this method worked for either US or UK locale 'this method always solve the problem of date settings e.g 9/24/2016 or 09/24/2016 Dim age as string = "07/30/2010" 'age Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "MM-dd-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex As Exception Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "M-dd-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex2 As Exception Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "M-d-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex3 As Exception Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "MM-d-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex4 As Exception Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "dd-MM-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex5 As Exception Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "dd-M-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex6 As Exception Try Dim birthDate = Date.ParseExact(age.replace("/", "-"), "d-M-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") Catch ex7 As Exception Dim birthDate = Date.ParseExact(age.replace("/", "-"), "d-MM-yyyy", Nothing) Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs") End Try End Try End Try End Try End Try End Try End Try
  • 一个数字(也可以是false

我的变量实际上是这样的:

0

现在我想检测$index = array_search(1, array_column($var, 'box')); //=> the output could be something like these: false, 0, 1, 2, ... $index还是其他任何内容。我怎样才能实现这样的条件?

应该注意的是,这些都不起作用:

false

我想要这个条件:

if ( empty($index) )   { }
if ( !isset($index) )  { }
if ( is_null($index) ) { }

2 个答案:

答案 0 :(得分:1)

使用===运算符...

if(false == 0)

那会将0转为false而你会得到false == false

if(false === 0)

与零相比,这不会被施放,你会得到假,这是不一样的。

如果不等于,请使用!==而不是===

答案 1 :(得分:1)

您可以使用PHP相同的运算符===

if (false === 0)

如果它们属于同一类型,则返回false。与发生类型杂耍的==不同,false0不同。

if ($index === false) { 
    // $index is false
} else {
    // $index is a number (even 0)
}

有关相同运算符===与比较运算符==之间差异的详细信息:How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?