duange051858 2015-03-10 09:31
浏览 33

php中两个和三个等号的区别......或者只是我编码错了? [重复]

Thanks to those who chimed in...But I modified my original program and now am even more perplexed:

Here's the new code:

        if ($_SESSION["cart_array"][$key]["Kit"]==true) {echo "<br>1 True<br>";}
        if ($_SESSION["cart_array"][$key]["Kit"]=="true") {echo "<br>1b True<br>";}
        if ($_SESSION["cart_array"][$key]["Kit"]==false){echo "2 True<br>";}
        if ($_SESSION["cart_array"][$key]["Kit"]=="False"){echo "3 True<br>";}
        if ($_SESSION["cart_array"][$key]["Kit"]==True){echo "4 True<br>";}
        if ($_SESSION["cart_array"][$key]["Kit"]==False){echo "5 True<br>";}


    ;}
    ?>

For simplicity I created an array with just one line. The Var_dump follows: array(1) { [0]=> array(11) { ["groupId"]=> string(5) "25778" ["GroupName"]=> string(9) "'1canoe2'" ["StylePatternColor"]=> string(30) "A-7557-C " ["Price"]=> string(4) "5.25" ["StandardPutUp"]=> string(2) "15" ["Discount"]=> string(3) ".25" ["ListPrice"]=> string(3) "5.5" ["Quantity"]=> string(1) "1" ["PromiseDate"]=> string(10) "07/01/2014" ["DoNotShipBefore"]=> string(10) "07/01/2014" ["Kit"]=> string(5) "False" } }

See that Kit is "False". Yet the results when I run the program are as follows: 1 True 3 True 4 True

How can this be? 1 is supposed to match if the value is true (boolean), three is supposed to match if the value is false (boolean) and four is supposed to match if True (boolean, I wanted to test for case sensitivity). Anyway, "False" matched but False (boolean) did not. And, note, these were all with ==, not ===. How come? Thanks again for your input. I'm tearing my hair out.

I am having trouble understanding how to evaluate array variables in php.

<?php
session_start(); 
print_r($_SESSION["cart_array"]);
$NumberOfLineItems = count($_SESSION["cart_array"]);
for ($key=0; $key<$NumberOfLineItems; $key++) {

    if ($_SESSION["cart_array"][$key]["Kit"]==true) {echo "<br>1 <br>";}
    if ($_SESSION["cart_array"][$key]["Kit"]==false){echo "2 <br>";}
    if ($_SESSION["cart_array"][$key]["Kit"]===true){echo "3 <br>";}
    if ($_SESSION["cart_array"][$key]["Kit"]===false){echo "4 <br>";}
;}
?>

    session_start(); Print_r($_SESSION["cart_array"]); 

I get: Array ( [0] => Array ( [groupId] => 255... [Kit] => True ) )

1

In short, the double equals does find the match, but the triple does not? I would have thought it would have been exactly the opposite. From what I've read here it seems like the more specific should have the least ambiguity.

Why does the exact equality fail but the double= find a match?

I've read about the differences, but I am obviously missing something basic. Thanks in advance.

The last bit, Kit, has had me up all night.

</div>
  • 写回答

2条回答 默认 最新

  • douraoyw194498 2015-03-10 09:39
    关注

    Your variable ($_SESSION["cart_array"][$key]["Kit"]) seems to hold a value that converts to true (like 1, "true"[string] etc.).

    === checks if the type is identical while == only checks the values.

    More clarification after seeing your var_dump: ["Kit"]=> string(5) "False"

    Your variable "Kit" is from type string. Which means it is text. With === you check if it is of boolean type. Which is is not. Therefore the result => false. With == however, the to variables to compare are first converted into whatever suitable and then compared. And if you convert the string "false" into a boolean value, it becomes false, which then equals false => true.

    Do you know about data types? If not, for this case it would be worth a read.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b