Code in PHP :
在PHP代码:
<?php
$key = 0;
if($key == 'monty' && $key == 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
OR
或
<?php
$key = 2;
if($key == '2abc' || $key == 'abc2'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
OUTPUT: Mackraja
输出:Mackraja
7 个解决方案
#1
9
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. - Source
如果您将一个数字与一个字符串进行比较,或者比较涉及数字字符串,那么每个字符串都被转换为一个数字,并以数字方式执行比较。这些规则也适用于switch语句。——源
This means that
这意味着
<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
So when you're comparing $key
(with value 0
) to the strings, the strings have value 0
and they both return true. This way, it will always output "Mackraja".
所以当你将$key(值0)与字符串进行比较时,字符串的值为0,它们都返回true。这样,它将始终输出“Mackraja”。
Changing the value of $key
to any other integer will return false.
将$key值更改为任何其他整数将返回false。
Note that
请注意,
The type conversion does not take place when the comparison is
===
or!==
as this involves comparing the type as well as the value. - Source当比较是===或!=时,类型转换不会发生,因为这涉及到比较类型和值。——源
This means that changing the comparison operator to ===
will mean that the values have to match completely - i.e., a string 1
will not be equal to an integer 1: they have to match formats:
这意味着将比较运算符更改为===将意味着值必须完全匹配—即。,字符串1将不等于整数1:它们必须匹配格式:
echo $key == 'monty' && $key == 'anil' ? 'Mackraja' : 'Nothing';
//will echo "Mackraja"
echo $key === 'monty' && $key === 'anil' ? 'Mackraja' : 'Nothing';
//will echo "Nothing"
#2
3
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. Reference
如果您将一个数字与一个字符串进行比较,或者比较涉及数字字符串,那么每个字符串都被转换为一个数字,并以数字方式执行比较。参考
Now check
现在检查
<?php
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
you will get output "Nothing".
你将得到“无”输出。
now integer value of those strings are 0 you can observe that here.
这些字符串的整数值是0你可以在这里看到。
<?php
$a = 'monty';
echo (int)$a; //0
How ? / Why ?
如何?/为什么?
Taking the situation to out of programming little bit to explain it, how ever better way can be posted, I am trying my way to explain the concept to you.
把这种情况从编程的角度解释出来,如何更好的方式发布,我正在尝试向你解释这个概念。
If you have come across principle of homogeneity it states that you can not compare two quantity if their dimensions are different.
如果你遇到了同质性的原理,那么你就不能比较两个量如果它们的维度是不同的。
Here in programming you can not compare a string and integer directly, it needed to be converted to any one to be compared.
在编程中,您不能直接比较字符串和整数,需要将它转换为任何一个来进行比较。
In PHP, if you will go through manual you can notice or that is a basic idea of PHP that:-
在PHP中,如果您要阅读手册,您可以注意到或者这是PHP的一个基本概念:-
PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used.
PHP不需要(或支持)变量声明中的显式类型定义;变量的类型由使用变量的上下文决定。
For comparison operator when you are even comparing two string (with loose comparison) those are converted to integer and then they are compared You can notice it on the reference page "Comparison with Various Types" table.
对于比较运算符,当您正在比较两个字符串(使用松散比较)时,它们被转换成整数,然后进行比较,您可以在参考页面“与各种类型的比较”表中注意到它。
So, I can say comparison operator itself needs integer type variable to comapre, that is why both operators are compared to integers.
所以,我可以说比较运算符本身需要整数类型变量来组合,这就是为什么两个运算符都要与整数进行比较。
$key = 2;
if($key == '2abc' || $key == 'abc2'){
echo '<br/>Mackraja';//outputs
}else{
echo '<br/>nothing';//does not output
}
now,
现在,
$key = 200;
if($key == '2a2' ){
echo '<br/>' . $key . '== 2a2' . ' Equal';//does not output
}else{
echo '<br/>' . $key . '== 2a2' . ' Not Equal'; //outputs
}
and
和
$key = 200;
if($key == '2e2' || $key == 'abc2'){
echo '<br/>' . $key . '== 2e2' . ' Equal';//outputs Now it does not convert it to integer, it converts it to
}else{
echo '<br/>' . $key . '== 2e2' . " Not Equal";//does not output
}
now
现在
echo '<br/>'.'1E1'*1; //it outputs 10 why ??
echo '<br/>';
echo '<br/>1E1'*1; //it outputs 10 why ??
see below:-
见下文:
echo '<br/>cast to int 1E1 = ' . (int)1E1;//10
echo '<br/>Cast to int 1E2 = ' . (int)1E2;//100
//echo '<br />Cast to int 1A1 = ' . (int)1A1;//gives you error as can not be cast
//echo '<br />Cast to int 1A2 = ' . (int)1A2;//gives you error as can not be cast
echo '<br />Cast to int 1A1 = ' . (int)('1A1');//1
echo '<br />Cast to int 1A2 = ' . (int)('1A2');//1
echo '<br/>get int value 1E1 = ' . intval('1E1');//1
echo '<br/>get int value 1E2 = ' . intval('1E2');//1
echo '<br />get int value 1A1 = ' . intval('1A1');//1
echo '<br />get int value 1A2 = ' . intval('1A2');//1
"This is not about getting the intvalue from a variable it is about how a variable is cast."
“这不是关于从变量中获取intvalue,而是关于如何对变量进行转换。”
I am giving you another clean table, taht you have already seen from my previous reference link,how different types of variable are casted in PHP when compared you can notice that from here.
我给你的是另一个干净的表,你已经从我之前的参考链接中看到过,PHP中变量的不同类型是如何被赋值的,你可以从这里看到。
If you need more resource, the best idea is to go for source code how it is written you can learn a lot from that, and why they did like this can be better answered by PHP group.
如果您需要更多的资源,最好的方法是寻找源代码的编写方式,您可以从中学到很多东西,PHP组可以更好地回答他们为什么这样做。
May be you may get something like can not becompared for such situation if PHP will not convert these variable implicitly, if you want your PHP to behave like this, you can make your own version .
如果PHP不隐式地转换这些变量,如果你想让PHP像这样运行,你可以创建自己的版本。
I am adding the table how conversion happens when loosely variable are compared:-
我正在添加表如何转换发生时,比较松散的变量:-
#3
2
check the type conversion rules for loose comparison (Loose comparisons with ==): php type conversion rules
检查松散比较的类型转换规则(松散比较==):php类型转换规则。
You'll see that with using loose type comparison, integer 0 equals to any string...
您将看到,使用宽松类型比较,整数0等于任何字符串…
#4
2
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.When you provide $key as 0 then comparison will take place numerically ie,
(int)'monty' = 0
and(int)'anil' = 0
.如果您将一个数字与一个字符串进行比较,或者比较涉及数字字符串,那么每个字符串都被转换为一个数字,并以数字方式执行比较。这些规则也适用于switch语句。当比较是===或!=时,类型转换不会发生,因为这涉及到比较类型和值。当您提供$key为0时,将进行数值比较,即(int)'monty' = 0, (int)'anil' = 0。
Try this if you want correct result
如果你想要正确的结果,试试这个
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
#5
2
if a string is compared with number, string will be converted t number and the check / comparition is done numerically
如果一个字符串与数字进行比较,字符串将被转换为t号,检查/比较是用数字进行的
if you still need to get the correct output with data type check you can use "===" in if as below will result the output u expect as "Nothing"
如果您仍然需要通过数据类型检查获得正确的输出,您可以使用“===”,如果如下所示将导致输出u预期为“无”
<?php
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
this will output: Nothing
这将输出:没有
#6
1
Just do like this..
只是这样做. .
<?php
$key = 0;
if((string)$key == 'monty' && (string)$key == 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
Output:
输出:
Nothing
#7
1
After a long discussion with my colleagues : We Found Solution
在与同事们长时间的讨论后,我们找到了解决方案
Examples :
例子:
#1 var_dump("00005ab" == 5); // true
#2 var_dump("abc" == 0); // true
#3 var_dump(2 == "ab2"); // false
#4 var_dump(2 == "2ab"); // true
Explanation :
解释:
In the condition left operand (2) match with right operand (ab2), if it does not found at the beginning then output will be false else true.
在左操作数(2)与右操作数(ab2)匹配的条件下,如果在开始时没有找到,则输出将为false else true。
#1 : we found 5 at the begining output will be true. 0 will be eliminated.
#2 : string + int = int output will be true.
Priority of int is higher, 0 == 0 output will be true,
#3 : 2 is not found at the beginning of match value (ab2), output will be false.
#4 : 2 is found at the beginning of match value (2ab), output will be true.
Reason :
原因:
1.) In PHP, its automatically convert Type Casting.
2.) Rules of Type Casting :
2.1) int + int = int
2.2) string + string = string
2.3) float + float = double
2.4) int + string = int
2.5) float + string = double
2.6) int + float = double
#1
9
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. - Source
如果您将一个数字与一个字符串进行比较,或者比较涉及数字字符串,那么每个字符串都被转换为一个数字,并以数字方式执行比较。这些规则也适用于switch语句。——源
This means that
这意味着
<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
So when you're comparing $key
(with value 0
) to the strings, the strings have value 0
and they both return true. This way, it will always output "Mackraja".
所以当你将$key(值0)与字符串进行比较时,字符串的值为0,它们都返回true。这样,它将始终输出“Mackraja”。
Changing the value of $key
to any other integer will return false.
将$key值更改为任何其他整数将返回false。
Note that
请注意,
The type conversion does not take place when the comparison is
===
or!==
as this involves comparing the type as well as the value. - Source当比较是===或!=时,类型转换不会发生,因为这涉及到比较类型和值。——源
This means that changing the comparison operator to ===
will mean that the values have to match completely - i.e., a string 1
will not be equal to an integer 1: they have to match formats:
这意味着将比较运算符更改为===将意味着值必须完全匹配—即。,字符串1将不等于整数1:它们必须匹配格式:
echo $key == 'monty' && $key == 'anil' ? 'Mackraja' : 'Nothing';
//will echo "Mackraja"
echo $key === 'monty' && $key === 'anil' ? 'Mackraja' : 'Nothing';
//will echo "Nothing"
#2
3
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. Reference
如果您将一个数字与一个字符串进行比较,或者比较涉及数字字符串,那么每个字符串都被转换为一个数字,并以数字方式执行比较。参考
Now check
现在检查
<?php
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
you will get output "Nothing".
你将得到“无”输出。
now integer value of those strings are 0 you can observe that here.
这些字符串的整数值是0你可以在这里看到。
<?php
$a = 'monty';
echo (int)$a; //0
How ? / Why ?
如何?/为什么?
Taking the situation to out of programming little bit to explain it, how ever better way can be posted, I am trying my way to explain the concept to you.
把这种情况从编程的角度解释出来,如何更好的方式发布,我正在尝试向你解释这个概念。
If you have come across principle of homogeneity it states that you can not compare two quantity if their dimensions are different.
如果你遇到了同质性的原理,那么你就不能比较两个量如果它们的维度是不同的。
Here in programming you can not compare a string and integer directly, it needed to be converted to any one to be compared.
在编程中,您不能直接比较字符串和整数,需要将它转换为任何一个来进行比较。
In PHP, if you will go through manual you can notice or that is a basic idea of PHP that:-
在PHP中,如果您要阅读手册,您可以注意到或者这是PHP的一个基本概念:-
PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used.
PHP不需要(或支持)变量声明中的显式类型定义;变量的类型由使用变量的上下文决定。
For comparison operator when you are even comparing two string (with loose comparison) those are converted to integer and then they are compared You can notice it on the reference page "Comparison with Various Types" table.
对于比较运算符,当您正在比较两个字符串(使用松散比较)时,它们被转换成整数,然后进行比较,您可以在参考页面“与各种类型的比较”表中注意到它。
So, I can say comparison operator itself needs integer type variable to comapre, that is why both operators are compared to integers.
所以,我可以说比较运算符本身需要整数类型变量来组合,这就是为什么两个运算符都要与整数进行比较。
$key = 2;
if($key == '2abc' || $key == 'abc2'){
echo '<br/>Mackraja';//outputs
}else{
echo '<br/>nothing';//does not output
}
now,
现在,
$key = 200;
if($key == '2a2' ){
echo '<br/>' . $key . '== 2a2' . ' Equal';//does not output
}else{
echo '<br/>' . $key . '== 2a2' . ' Not Equal'; //outputs
}
and
和
$key = 200;
if($key == '2e2' || $key == 'abc2'){
echo '<br/>' . $key . '== 2e2' . ' Equal';//outputs Now it does not convert it to integer, it converts it to
}else{
echo '<br/>' . $key . '== 2e2' . " Not Equal";//does not output
}
now
现在
echo '<br/>'.'1E1'*1; //it outputs 10 why ??
echo '<br/>';
echo '<br/>1E1'*1; //it outputs 10 why ??
see below:-
见下文:
echo '<br/>cast to int 1E1 = ' . (int)1E1;//10
echo '<br/>Cast to int 1E2 = ' . (int)1E2;//100
//echo '<br />Cast to int 1A1 = ' . (int)1A1;//gives you error as can not be cast
//echo '<br />Cast to int 1A2 = ' . (int)1A2;//gives you error as can not be cast
echo '<br />Cast to int 1A1 = ' . (int)('1A1');//1
echo '<br />Cast to int 1A2 = ' . (int)('1A2');//1
echo '<br/>get int value 1E1 = ' . intval('1E1');//1
echo '<br/>get int value 1E2 = ' . intval('1E2');//1
echo '<br />get int value 1A1 = ' . intval('1A1');//1
echo '<br />get int value 1A2 = ' . intval('1A2');//1
"This is not about getting the intvalue from a variable it is about how a variable is cast."
“这不是关于从变量中获取intvalue,而是关于如何对变量进行转换。”
I am giving you another clean table, taht you have already seen from my previous reference link,how different types of variable are casted in PHP when compared you can notice that from here.
我给你的是另一个干净的表,你已经从我之前的参考链接中看到过,PHP中变量的不同类型是如何被赋值的,你可以从这里看到。
If you need more resource, the best idea is to go for source code how it is written you can learn a lot from that, and why they did like this can be better answered by PHP group.
如果您需要更多的资源,最好的方法是寻找源代码的编写方式,您可以从中学到很多东西,PHP组可以更好地回答他们为什么这样做。
May be you may get something like can not becompared for such situation if PHP will not convert these variable implicitly, if you want your PHP to behave like this, you can make your own version .
如果PHP不隐式地转换这些变量,如果你想让PHP像这样运行,你可以创建自己的版本。
I am adding the table how conversion happens when loosely variable are compared:-
我正在添加表如何转换发生时,比较松散的变量:-
#3
2
check the type conversion rules for loose comparison (Loose comparisons with ==): php type conversion rules
检查松散比较的类型转换规则(松散比较==):php类型转换规则。
You'll see that with using loose type comparison, integer 0 equals to any string...
您将看到,使用宽松类型比较,整数0等于任何字符串…
#4
2
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.When you provide $key as 0 then comparison will take place numerically ie,
(int)'monty' = 0
and(int)'anil' = 0
.如果您将一个数字与一个字符串进行比较,或者比较涉及数字字符串,那么每个字符串都被转换为一个数字,并以数字方式执行比较。这些规则也适用于switch语句。当比较是===或!=时,类型转换不会发生,因为这涉及到比较类型和值。当您提供$key为0时,将进行数值比较,即(int)'monty' = 0, (int)'anil' = 0。
Try this if you want correct result
如果你想要正确的结果,试试这个
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
#5
2
if a string is compared with number, string will be converted t number and the check / comparition is done numerically
如果一个字符串与数字进行比较,字符串将被转换为t号,检查/比较是用数字进行的
if you still need to get the correct output with data type check you can use "===" in if as below will result the output u expect as "Nothing"
如果您仍然需要通过数据类型检查获得正确的输出,您可以使用“===”,如果如下所示将导致输出u预期为“无”
<?php
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
this will output: Nothing
这将输出:没有
#6
1
Just do like this..
只是这样做. .
<?php
$key = 0;
if((string)$key == 'monty' && (string)$key == 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
Output:
输出:
Nothing
#7
1
After a long discussion with my colleagues : We Found Solution
在与同事们长时间的讨论后,我们找到了解决方案
Examples :
例子:
#1 var_dump("00005ab" == 5); // true
#2 var_dump("abc" == 0); // true
#3 var_dump(2 == "ab2"); // false
#4 var_dump(2 == "2ab"); // true
Explanation :
解释:
In the condition left operand (2) match with right operand (ab2), if it does not found at the beginning then output will be false else true.
在左操作数(2)与右操作数(ab2)匹配的条件下,如果在开始时没有找到,则输出将为false else true。
#1 : we found 5 at the begining output will be true. 0 will be eliminated.
#2 : string + int = int output will be true.
Priority of int is higher, 0 == 0 output will be true,
#3 : 2 is not found at the beginning of match value (ab2), output will be false.
#4 : 2 is found at the beginning of match value (2ab), output will be true.
Reason :
原因:
1.) In PHP, its automatically convert Type Casting.
2.) Rules of Type Casting :
2.1) int + int = int
2.2) string + string = string
2.3) float + float = double
2.4) int + string = int
2.5) float + string = double
2.6) int + float = double