Jump to content

Same string not testing for equality


Paid8x

Recommended Posts

So I'm working on a shopping cart and basically, all the item's details are put into an array and stored in a session. When another product is $_POST'ed and compared to the existing $_SESSION variables to check if I need to update the quanity, I can't seem to get two of the same strings to equal each other. I tried trim, strcmp, == and === to compare. I've echoed the two variables and they look exactly the same but it's driving me crazy because I can't get them to match. What am I doing wrong?

<?php
if (isset($_POST['cut']))
   {$itemToAdd = array($_POST['cut'], $_POST['size'], $_POST['color'], $_POST['design'], $_POST['style'], 1);
   $addOrNot = true;
   foreach ($_SESSION as $akey => $avalue)
      {
      if (strcmp( '$avalue[0]' , '$itemToAdd[0]' ) == 0)
         {$avalue[5]++;
         $addOrNot = false;
                        };
      };
   if ($addOrNot = true)
      {$len = count($_SESSION);
                $m = "item".$len;
      $_SESSION[$m]=$itemToAdd;
      };
   };
  

 

Thanks!

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/
Share on other sites

oops... sorry didn't see the code you provided.

 

Array

(

    [item0] => Array

        (

            [0] => Men

            [1] => Small

            [2] => Organic Natural

            [3] => Eat

            [4] => Short Sleeve

            [5] => 1

        )

 

    [item1] => Array

        (

            [0] => Men

            [1] => Small

            [2] => Organic Natural

            [3] => Eat

            [4] => Short Sleeve

            [5] => 1

        )

 

    [item2] => Array

        (

            [0] => Men

            [1] => Small

            [2] => Organic Natural

            [3] => Eat

            [4] => Short Sleeve

            [5] => 1

        )

 

    [item3] => Array

        (

            [0] => Men

            [1] => Small

            [2] => Organic Natural

            [3] => Eat

            [4] => Short Sleeve

            [5] => 1

        )

 

)

That's not it either. I can even echo the two values surrounded by periods to see if it's any space or capitalization issues and they look exactly the same. It must have something to do with the utf-8 or ansi coding and how the session and post return strings differently coded?

So I still don't know what's happening exactly so I'm going to leave this open in hopes that someone more knowledgeable than myself can shed some light and correct me if I'm wrong, but I came to this conclusion: When I tried to compare two array items even after trimming, turning to lowercase, using strcmp or double or triple equal signs to compare, they're not the same. You're comparing two objects with other properties, not just their values? Maybe? When you create two new variables and make them equal to the values of the array items the two new vars match up just fine. But even better, scrap using the string comparison altogether and use in_array to see if the string you're looking for is in the session array.

 

 

len = count($_SESSION);	
if (isset($_POST['cut']))
{$addItem = array($_POST['cut'], $_POST['size'], $_POST['color'], $_POST['design'], $_POST['style'], 1);
$addOrNot = True;
foreach ($_SESSION as $akey => $avalue)
	{
	if (in_array($avalue[0], $addItem) && in_array($avalue[1], $addItem))
		{
		$_SESSION[$akey][5]++;
		$addOrNot = False;
		};
	};
if ($addOrNot == True)
	{$m = "item".$len;
	$_SESSION[$m]=$addItem;
	};
};

Get rid of the quotes in the strcmp() function; they're making it compare the variable names as string literals.

 

if (strcmp( $avalue[0] , $itemToAdd[0] ) == 0)

// NOT
if (strcmp( '$avalue[0]' , '$itemToAdd[0]' ) == 0)

Thanks for the help revraz! That smack talking about my poor design instead of offering actual insight into the language is totally helpful!!! It's not an issue that there are over four hundred possible combinations of values and there are price adjustments for each possible value... obviously, it's that my design sucks and I should be trying to work with non-semantic id numbers instead of easily understandable product descriptions!

 

On a serious note: Good catch, Pikachu. Thanks! Totally works. Can't believe I missed that. That's what I get for not taking a break, I guess.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.