I am comparing two string by
strcmp(1,2)
and i am getting "-1 " insted of 1 my code is below
<?php
echo strcmp(1,2);
?>
output:-1
please let me know why i am getting -1 for false and how to handle it ?
I am comparing two string by
strcmp(1,2)
and i am getting "-1 " insted of 1 my code is below
<?php
echo strcmp(1,2);
?>
output:-1
please let me know why i am getting -1 for false and how to handle it ?
1 is less than 2, strcmp is defined as returning:
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
Thus the behaviour is expected.
Note the following caveat:
If you rely on strcmp for safe string comparisons, both parameters must be strings, the result is otherwise extremely unpredictable. For instance you may get an unexpected 0, or return values of NULL, -2, 2, 3 and -3.
From a comment on the documentation page for strcmp
.
PHP is full of this sort of unexpected behaviour. In your situation, I would ensure both arguments were cast as strings to avoid any confusion:
$first = 1;
$second = 2;
echo strcmp((string)$first, (string)$second);
When you pass in an int, it is casted to a string as evidenced by:
var_dump(strcmp('1', 2)); //-1
The reason it's -1 is because the most widely used implementation of strcmp finds the first non-equal character and returns the difference.
In other words, it's essentially '1' - '2' which is 49 - 50 (ASCII codes for '1' and '2').
49-50 is -1.
Edit: Not strictly relevant, but out of curiosity, did some digging. This is by no means guaranteed behavior in the future, but from PHP 5.4:
ZEND_FUNCTION(strcmp)
{
char *s1, *s2;
int s1_len, s2_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &s1, &s1_len, &s2, &s2_len) == FAILURE) {
return;
}
RETURN_LONG(zend_binary_strcmp(s1, s1_len, s2, s2_len));
}
ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */
{
int retval;
if (s1 == s2) {
return 0;
}
retval = memcmp(s1, s2, MIN(len1, len2));
if (!retval) {
return (len1 - len2);
} else {
return retval;
}
}
It does indeed cast to a string.