1

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.

  • 1
    The array appears to hold the text "True", rather than a boolean "true". You can tell this because print_r will actually show a 1 for true, rather than the text. If you use var_dump instead, it will also show the type. – rjdown Mar 10 '15 at 09:41
  • it's not about your array. It's only about comparing text variables to boolean variables. – Teetrinker Mar 10 '15 at 18:21
  • "I am having trouble understanding how to evaluate array variables in php." you would not be alone, search PHP Truth tables - pin it on your wall. Then muse on the fact that everything coming from GET/POST is a string. Then start temporarily doing a var_dump() on every var before every conditional fork. Do that for a few weeks. – Cups Mar 10 '15 at 18:54

2 Answers2

1

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.

Teetrinker
  • 850
  • 1
  • 15
  • 30
  • So, if a var_dump shows: ["Kit"]=> string(5) "False", why does "$_SESSION["cart_array"][$key]["Kit"]==true" and ==False both return as True? Whereas, =="False" is false? Sorry to be dense...thanks for your help. – riddley walker Mar 10 '15 at 11:43
  • I am not sure. But it could be because only "false" and "FALSE" (not even sure about the second one) are converted to boolean values, while "False" is not converted, therefor it is true, because a string is true. – Teetrinker Mar 11 '15 at 21:47
1

It will be == false if $_SESSION["cart_array"][$key]["Kit"] is one of the following:

NULL, 
'', 
0,
false

Only when it contains true, it will === true

n-dru
  • 9,285
  • 2
  • 29
  • 42