3

In php, there is a built-in function strcmp to compare if two strings are same or not.

Returning value is integer number that if the first parameter is greater than the second I get > 0, if not < 0 and if the same 0.

So the part I don't get is comparing string as number. Does PHP convert string to number and if so how's PHP converting?

$a = 'acorn';
$b = 'zebra';
var_dump( strcmp($a, $b) ); // -25 <- what's this number? seems like alphabetical position...nnn

Doesn't it really matter what number I get, shall I just take what it is?

norixxx
  • 2,549
  • 2
  • 19
  • 26

2 Answers2

2

Looking at the PHP: strcmp doc :

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

So yes, you can use it as it is to compare your string.

But if you want to understand the number returned by the function, it depend on the characters that makes the strings.

In ASCII :

A=65 < B=66 < C=67 ....

So if the string are different, one is gonna be greater than the other.

So you can also test it easily with a short script :

<?php

$a='A';
$b='B';
$c='C';

//Return -1 because $a is smaller than $b by one (65 < 66 )
echo strcmp($a,$b);

//Return -2 because $a is smaller than $c by two (65 < 67 )
echo strcmp($a,$c);


//Return -1 because $b is smaller than $c by one (66 < 67 )
echo strcmp($b,$c);

//Return 1 because $c is greater than $b by one (67 > 66 )
echo strcmp($c,$b);

//Return 2 because $c is greater than $a by two (67 > 65 )
echo strcmp($c,$a);
Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • That may be right for your and many other implementations, but it is still undefined, depends on implementation and may change in the future. I remember hardware with assembler commands to compare strings, the modify only flags in status register. And here values are created by flags and not by difference. Qnd what is the result if comparing `strcmp("a","ac")`? `-1` at my pc. – Wiimm Apr 24 '19 at 10:14
  • I do get -1 as well, but 'a' is indeed smaller than 'ac', though the way it has been calculated is beyond me – Dylan KAS Apr 24 '19 at 12:14
  • My comment was only for the absolute value of the result, So don't expect that -3 has another meaning than -1. And yes, `"a"` is smaller than `"ac"`, and the difference between ASCII `c` and `NUL` is 99. – Wiimm Apr 24 '19 at 13:59
0

strcmp is using for comparing two strings in PHP. If your result is greater the 0 its mean variable one ($a) is greater then Var2($b) if return result is less then 0 its mean $b is greater and If result is equal to 0 its mean both strings are equal to each other.

$a="Hello world!";
$b= "Hello world!";
var_dump(strcmp($a, $b)); // result is 0

$a = "Hello";
$b = "Hello World";
var_dump(strcmp($a, $b)); // Outputs: -6

and If you want to see difference then you can use this

$a = 'some-content-here-example';
$b = 'some-content-example';
$asp = preg_split('//', $a, -1);
$bsp = preg_split('//', $b, -1);

$l1 = count($asp);
$l2 = count($bsp);

$length = $l1;
if ($l2 > $l1) {
    $length = $l2;
}
$record = null;
$x = null;
for ($x = 0; $x < $length; $x++) {
    if ($a[$x] != $b[$x]) {
        if (!isset($a[$x])) {
            $record.= $b[$x];
        } else {
            $record.= $a[$x];
        }
    }
}

echo $record;
raziii
  • 16
  • 1