Edit report at https://bugs.php.net/bug.php?id=65362&edit=1
ID: 65362
Updated by: yohgaki@php.net
Reported by: atli dot jonsson at ymail dot com
Summary: strcmp null return missing from docs.
Status: Open
-Type: Documentation Problem
+Type: Bug
-Package: Documentation problem
+Package: Scripting Engine problem
PHP Version: 5.5.1
Block user comment: N
Private report: N
New Comment:
I changed bug type since this is in Zend/zend_builtin_functions.c.
Shouldn't it raise error for arrays? Currently, it simply returned.
/* {{{ proto int strcmp(string str1, string str2)
Binary safe string comparison */
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));
}
/* }}} */
Previous Comments:
------------------------------------------------------------------------
[2013-07-30 23:36:30] atli dot jonsson at ymail dot com
Description:
------------
strcmp, strncmp, strcasecmp and strncasecmp will all return NULL when either
string parameter is of a type that is invalid for string conversions, like Arrays,
Objects and Resources.
However, the docs make no mention of this fact. (Aside from a comment.) As the 0
value returned for equal strings, and NULL returned for invalid comparisons, are
equal when compared in a non-strict manner, this can lead to unexpected behaviour.
There is a warning issued, but without clarification the above is still in no way
obvious.
Test script:
---------------
<?php
$arr = [];
$str = "PHP is awesome!";
if (strcmp($arr, $str) == 0) {
echo "Equal!"; // Ends up here.
}
else {
echo "Not equal!";
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65362&edit=1