简体   繁体   中英

if else condition not working with array values in php

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.

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']);
?>

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']);

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']);
}
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM