4

i have come to this strange issue.

i am getting values from array and trying to compare it but its not working.

Code-1

<?php
echo $data->item[0]['promocode'].'<br>';
echo $data->item[1]['promocode']; 
?>

Output-1

inhouse
inhouse 

Now lets try with if else condition if both values are same or not Code-2

<?php
if(($data->item[0]['promocode']) == ($data->item[1]['promocode'])){
echo "both values are same";
} else {
echo "both values are NOT same";
}?>

Output-2

both values are NOT same 

Very strange

i dont get it what i am doing wrong.

lets try above exaple with specifying variables Code-3

<?php
$data0=$data->item[0]['promocode'];
$data1=$data->item[1]['promocode'];
if($data0 == $data1){
echo "both values are same";
} else {
echo "both values are NOT same";
}?>

Output-3

both values are NOT same 

I am pulling my hairs now.


Now hard coding values in variables

Code-4

<?
$data0='inhouse';
$data1='inhouse';
if($data0 == $data1){
echo "both values are same";
} else {
echo "both values are NOT same";
}?>

Output-4

both values are same 

So my question is why is this happening ?

i have array of elements and i wanna check previous value with the current value if try then do something.

thanks for your time.

4
  • Hint: What is the result of if (strlen($data0=$data->item[0]['promocode']) == strlen($data0=$data->item[1]['promocode'])) Commented Jan 29, 2013 at 5:48
  • Try === instead of ==. Commented Jan 29, 2013 at 5:49
  • 2
    Isn't === even stronger and with more restrictions than ==, so when == gives false, === will surely give false, too? Just asking to see whether I understood them correctly. Commented Jan 29, 2013 at 5:54
  • 2
    There indeed seems to be a trailing whitespace as seen here i49.tinypic.com/1ffmrr.jpg Commented Jan 29, 2013 at 6:08

4 Answers 4

6

Assuming both entries are strings (as shown in your first code example) my guess would be that your entries have unequal leading and / or trailing whitespace. Try normalising them first, eg

if (trim($data->item[0]['promocode']) == trim($data->item[1]['promocode']))

To see what's going on, try modifying your first example to

<?php
    printf('<pre>"%s"%s"%s"</pre>',
        $data->item[0]['promocode'],
        PHP_EOL,
        $data->item[1]['promocode']);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

<?php
   if(($data->item[0]['promocode']) === ($data->item[1]['promocode'])){
     echo "both values are same";
   } else {
     echo "both values are NOT same";
   }
?>

or you can use

strcmp($data->item[0]['promocode'],$data->item[1]['promocode']);

5 Comments

The identical operator is only going to make the comparison more likely to fail. strcmp() is not going to yield any different result than ==
I think strcmp is more specified for string functioning right..??your trimming may delete spaces and may others but the questioner asked for comparission...
No, strcmp() will give you the difference between two strings as a number (0 for equal). Using it to determine equality is no different than $a == $b
Yah then you can use if(strcmp(str1,str2) == 0) then they are equal otherwise not....even it determine the 'cases' also
As pointed out already, strcmp($str1, $str2) == 0 is not going to yield a different result than $str1 == $str2
0

I use strlen($var) also for debugging...

<?php
if( strlen($data->item[0]['promocode']) == strlen($data->item[1]['promocode']) ){
  if(($data->item[0]['promocode']) == ($data->item[1]['promocode'])){
    echo "both values are same";
  }
  else {
    echo "both values are NOT same";
  }
}
else{
 echo 'ther are different because 
 strlen($data->item[0]["promocode"]='.strlen($data->item[0]['promocode']).' and 
 strlen($data->item[1][|promocode"]) = '. strlen($data->item[1]['promocode']);
}
?>

Comments

-1

Try using the non-type-casted conditional statement === to see if they are the same type. Or for debugging perposes display the variable type to make sure you don't accidentally have some NULL objects or other weird data types.

if($data0 !== $data1) echo gettype($data0).' !== '.gettype($data1);

That should help you find out what you're actually comparing. Another option is to use var_dump($data); to actually display the variables all together. See if theres some discrepancies in the data types. It should help you find out if your object is actually populating correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.