note 25168 added to function.strncmp

From: Date: Thu, 12 Sep 2002 22:03:58 +0000
Subject: note 25168 added to function.strncmp
Groups: php.notes 
Request: Send a blank email to php-notes+get-36605@lists.php.net to get a copy of this message
Equivalent (simplified) function for older PHP3: function strncmp($s1, $s2, $len) { return strcmp(substr($s1, 0, $len), substr($s2, 0, $len)); } This function is efficient in PHP3 if $len is short (below 1KB). For larger values of $len, when both strings may be arbitrarily large, this may not be efficient because substr will allocate space for each substring used in the comparaison. In that case, a for loop will avoid this duplication and may run faster, depending whever you have a Zend optimizer or not. Hope this also helps understanding the semantic of this function, because the description is somewhat wrong. In fact this function compares AT MOST the first $len characters of each string, and ignores all characters after this limit. If a string is shorter than $len, it won't be truncated or padded with any NUL character prior to comparing it. So: strncmp("sample", sam", 4) is equivalent to: strcmp("samp", sam") -- http://www.php.net/manual/en/function.strncmp.php http://master.php.net/manage/user-notes.php?action=edit+25168 http://master.php.net/manage/user-notes.php?action=delete+25168 http://master.php.net/manage/user-notes.php?action=reject+25168

« previous php.notes (#36605) next »