English 中文(简体)
类型投放和与“=”
原标题:Type casting and Comparison with Loose Operator "=="

I have a problem baffling me terribly. I noticed this before but didn t give it any heed until today. I was trying to write my own check for integer strings. I know of is_numeric() but it does not suffice since it counts float as numeric not only integers and is_int() which does not work on string numbers.

I did something similar to this


$var1 =  string ;
$var2 =  123 ; 

var_dump( (int)$var1 == $var1);// boolean true 
var_dump((int)$var2 == $var2);// boolean true

 var_dump((int)$var1);//int 0
 var_dump($var1);//string  string  (length=6)

正如第二批var倾销产出所预期的那样,因为我期望有php的松散比较表明,str和 in化版本是平等的。

然而,首先,我不了解为什么如此。 我已尝试用<条码>bool,但结果仍然给我。

我试图将 cast子分配到新的 v子里,对两者进行比较,结果仍然相同。

这是我错做的吗?

***Note I am not comparing types here. I m actually trying to take advantage of the fact that int 0 is not equal to string string .

我写道,我的愤怒检查方式不同,因此我实际上不需要其他选择。

***Edit I did some extra checking and it turns out that 0 == string is true as well. How is that possible?

***Edit 2 There are multiple correct answers below to the question. Thanks to everyone who answered.

最佳回答

它不是ug,而是feature。 任何扼杀均可投给愤怒者,但。 如果扼杀物没有开始产生分类值,则投放器将返回。 此外,在比较愤怒和扼杀时,strcmp,因为它通过将任何东西传递给扼杀,进行了明确的描述性比较。)

为保证处理星体的Im,我将首先使用is_vidic ,然后将体格转换为int,并核实所加固的物体与投入价值相符。

if (is_numeric($value) && strcmp((int)$value, $value) == 0)
{
    // $value is an integer value represented as a string
}
问题回答

http://php.net/manual/en/English.operators.comparison.php rel=“nofollow”http://php.net/manual/en/English.operators.comparison.php:

var_dump(0 == "a"); // 0 == 0 -> true

因此,我认为,它正在计算这些类型,实际上使双方陷入困境。 然后,在插图中,将二元数值或各指数的二元数值加以比较。

首先,数学=中转/c(A=B和B=C=>A=C)有效。

This is not the case with PHPs "=="!

(int)$var1 == $var1

在这种情况下,PHP将引伸到0号——该公约。

然后 = 操作器将含蓄地将第二部操作体也投放到星体——和0。

这导致这种情况。

你与你的职位相差错,正确的产出是:

bool(true)
bool(true)
int(0)
string(6) "string"

What happens is this:

  1. Because you cast the variable to an integer, and you compare it to an integer with a loose comparison ==, PHP will first implicitely cast the string to an integer, a more explicit but 100% equivalent form would be: if((int)$var1 == (int) $var1)
  2. See 1), the same thing applies here
  3. It prints int(0), as it should, because it fails to parse the number, it will return 0 instead.
  4. Prints string(6) "string" - as expected

<>说明: 回答是。 有关Twig模板发动机的相关问题,该模板被标为重复,此处改头。

Because the context is different, this answer is provided to those members of the SO community who may benefit from additional details specifically related to exclusively.

TL;DR: see this post How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

Problem

Context

  • Twig template engine (latest version as of Fri 2017-01-27T05:12:25)

Scenario

  • DeveloperYlohEnrohK uses comparison operator in twig expression
  • DeveloperYlohEnrohK notices unexpected results when using equality comparison operator

Questions

  • Why does the equality comparison operator (==) produce unexpected results in Twig?
  • 以下各点是否产生了不同的结果?

    {{ dump(0 ==  somekey ) }}          ==> true
    {{ dump(0|lower ==  somekey ) }}    ==> false
    

Solution

  • Since Twig is based on PHP, the , implicit and semantics of PHP apply to Twig templates as well.
  • Unless DeveloperYlohEnrohK is intentionally and specifically leveraging the behavior of implicit type-conversion in PHP, the comparison expression will almost certainly produce counterintuitive and unexpected results.
  • This is a well-known circumstance that is described in detail in this SO post on PHP equality.
  • Solution: Just as is the case with standard PHP, unless the well-known circumstance is accounted for, using === in Twig is much less likely to produce unexpected results.

Pitfalls

  • As of this writing, the Twig template engine does not support === in the same way as standard PHP
  • Twig does have a substitute for === using same as
  • Because of this, the treatment of this well-known circumstance differs slightly between PHP and Twig.

See also

如果你想要比较变量类型,那么你也应使用=

这里的职能是更严格地检测胎体或机体。

function isIntegerNumeric($val) {
  return (
    is_int($val)
      || (
        !is_float($val)
          && is_numeric($val)
          && strpos($val, ".") === false
      )
  );
}

它相对较快,避免进行任何严格检查。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签