|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-07-30 23:36 UTC] 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!";
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 10:00:01 2025 UTC |
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)); } /* }}} */