Reputation:
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
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.
Upvotes: 4
Views: 2302
Reputation:
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']);
}
?>
Upvotes: 0
Reputation: 164897
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']);
?>
Upvotes: 6
Reputation: 2272
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.
Upvotes: -1
Reputation: 28753
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']);
Upvotes: 0