-4

I was asked to create simulation array_keys function, but check equality "==" returns false. But with "strcmp ($a, $b) == 0" return true.

class Utility
{
    public function convertArrayKeys(array $array)
    {
        $i = 0;
        $elements = [];
        foreach ($array as $element=>$value) {
            $elements[] = '  ' . $i++ . " => '" . $element . "'";
        }

        return 'array ( ' . implode(', ', $elements) . ', )';
    }

    public function testStrings(array $array)
    {
        $arrayOur = $this->convertArrayKeys($array);
        $arrayPhp = var_export(array_keys($array),true);
        if ($arrayOur == $arrayPhp){
            echo 'They are identical :)';
        } else {
            echo 'They are not identical :(';
            echo '<br>';
            print_r(str_split($arrayOur));
            echo '<br>';
            print_r(str_split($arrayPhp));
        }
    }
}

View:

$repository = array('box'=>'blue', 'cube'=>'red', 'ball'=>'green');
$utility = new Utility();

echo "OUr array_keys: ";
echo $utility->convertArrayKeys($repository);
echo "<br />";
echo "PHP array_keys: ";
print_r (var_export(array_keys($repository)));

echo "<hr >";
echo "<br />";

echo $utility->testStrings($repository);

I would appreciate to know because

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
David
  • 15
  • 4

3 Answers3

4

Edit: The reason that the two don't work in THIS instance is that your functions dont produce identical outputs: yours produces:

array ( 0 => 'box', 1 => 'cube', 2 => 'ball', )

where the php function produces:

array ( 0 => 'box', 1 => 'cube', 2 => 'ball', )

If you were to view that in the web browser i think the web browser renderer does whitespace trickery. However try putting uhh <pre> tags around it (or run in command line to check).


Basically == does more then compare the two values - the documentation suggests "after type juggling". You can get some weird things by comparing strings using ==. One good example is: '1e3' == '1000'. It is useful to use ==at times, but possibly not in conjunction with strings.

Strcmp also though doesn't return a true/false answer, but a -1, 0, 1 answer indicating which string is alphabetically in front of the other.

You should also look at === which can also have helpful uses, but personally I would stick with strcmp with strings.

Jmons
  • 1,766
  • 17
  • 28
  • 1
    I agree to use strcmp, but I don't understand why in this case in particular "==" does not give true – David Jul 15 '16 at 10:15
  • Ahara, okay, because the one is adding new lines in, and the other isn't. The php version adds (in position 7 for insatnce) a '\n'. I didnt run it in a web context (i.e. on command line) and saw almost immediately. Updating my answer. – Jmons Jul 15 '16 at 12:35
  • Thanks @james-taylor – David Jul 17 '16 at 08:48
0

Hi never use == in PHP. It will not do what you expect. Even if you are comparing strings to strings, PHP will implicitly cast them to floats and do a numerical comparison if they appear numerical.

try these you will know the reason

$something = 0;
echo ('password' == $something) ? 'true' : 'false';// true

$something = 0;
echo ('password' === $something) ? 'true' : 'false'; // false

echo strcmp('password123',$something); // 1
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0

Because they are not arrays, rather they are strings. Arrays are not created like this. you are doing it wrong. Were they arrays then

if ($arrayOur == $arrayPhp)

would have evaluated to true. But they are just strings and

"strcmp ($a, $b) == 0"

Does not return true, because there are whitespaces in the first string,

 return 'array ( ' . implode(', ', $elements) . ', )';

You are doing it completely wrong. You need to correct your approach.

Adeel Raza
  • 628
  • 5
  • 17