|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2006-04-02 00:33 UTC] ms419 at freezone dot co dot uk
 Description:
------------
strncmp returns incorrect result given negative len -
fis% php
<?php
var_dump(strncmp('foo_wrapper', '_wrapper', -8));
?>
int(7)
fis% 
expected - int(0)
returned - int(7)
Thanks - Jack
Patchesphp-5-3-trunk-strncmp-supporting-negative-length (last revision 2011-07-26 11:21 UTC by laruence@php.net)bug36944.phpt (last revision 2011-07-25 11:40 UTC by laruence@php.net) php-5-3-strn-case-cmp-supporting-negative-length (last revision 2011-07-25 11:37 UTC by laruence@php.net) php-5-3-strncasecmp-supporting-negative-length (last revision 2011-07-25 11:06 UTC by laruence@php.net) Pull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 07:00:01 2025 UTC | 
I wish to change this bug report to a feature request. I frequently need to check if a string begins or ends with another string. To check if a string begins with another string, I often use strncmp(): ket% php -r 'var_dump(strncmp("HTTP_ACCEPT", "HTTP_", 5));' int(0) ket% - but strncmp() is currently not able to check if a string ends with another string: ket% php -r 'var_dump(strncmp("foo_wrapper", "_wrapper", -8));' Warning: Length must be greater than or equal to 0 in Command line code on line 1 Call Stack: 0.0002 62840 1. {main}() Command line code:0 0.0002 62952 2. strncmp() Command line code:1 bool(false) ket% I wish strncmp() were able to check if a string ends with another string.