note 25168 deleted from function.strncmp by didou

From: Date: Fri, 03 Oct 2003 11:23:08 +0000
Subject: note 25168 deleted from function.strncmp by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57700@lists.php.net to get a copy of this message
Note Submitter: ---- 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")

« previous php.notes (#57700) next »