# RFC: strn(case)cmp supporting a negative length as its third paramter

- Status: Withdrawn
- Authors: Xinchen Hui laruence@php.net
- Date: 2011-07-28
- Version: 1.0
- Original: https://wiki.php.net/rfc/strncmpnegativelen
- Page: https://externals.io/rfc/strncmpnegativelen
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Do you want this feature in PHP 5.4 (closed)

yes: 11 · no: 5

Voters: arpad (yes), cataphract (yes), derick (no), felipe (yes), gwynne (yes), iliaa (no), klaussilveira (yes), laruence (yes), levim (no), pajoye (yes), pierrick (yes), rasmus (no), rdohms (yes), salathe (no), stas (yes), tyrael (yes)

## Discussions on the mailing list

- [\[RFC\]strn(case)cmp supporting negative length as third parameter](https://externals.io/message/54226.md) — started on 2011-07-28, 7 messages
- [\[VOTE\]strn(case)cmp supporting a negative length as its third paramter](https://externals.io/message/54275.md) — started on 2011-08-01, 33 messages

---

## Introduction

strncmp doesn't supporting negative length:

```php
<?php
   if (strncmp("prefix_num", "num", -3) === 0) {
        echo "they have same suffix\n";
   }
?> 
```

running the above script in PHP 5.3.6 will result:

```
warning: Length must be greater than or equal to 0 in /tmp/1.php 
```

We need to write some codes like following one to make it works as expect:

```php
<?php
   if (strncmp(substr("prefix_num", -3, 3), "num", 3) === 0) {
        echo "they have same suffix\n";
   }
?> 
```

## Proposal

base on feature request: <https://bugs.php.net/bug.php?id=36944> , I wrote a patch to make strn(case)cmp supporting negative length,

after patched, following script:

```php
<?php
   if (strncmp("prefix_num", "num", -3) === 0) {
        echo "they have same suffix\n";
   }
?> 
```

will work as expect.

and if the abs of the negative length is greater than any strlen of the first two parameters, then strn(case)cmp will work as strncmp in the reverse order with the abs value:

```php
<?php
var_dump(strncmp("prefix_num", "num", -10));
var_dump(strncmp("mun_xiferp", "mun", 10));
/** output:
int(7)
int(7)
*/
?> 
```

## BC Breaks

- Any strncmp call with a computed length which may end up being negative will now potentially return a match where it wouldn't before.

## Patches

- <http://www.laruence.com/php-dev/php-5.4-trunk-strncmp-supproting-negative-len.patch>

## Tests

- <http://www.laruence.com/php-dev/strncmpnegativelen.phpt>

## Vote

**Vote: Do you want this feature in PHP 5.4** — yes: 11 · no: 5 (closed)

## Changelog

- 2011-07-28 Xinchen Hui: Initial RFC creation
- 2011-07-28 Xinchen Hui: Updated patch to avoid making binary API changed
- 2011-08-01 Xinchen Hui: Start voting
- 2011-08-08 Xinchen Hui: Edited phpt
- 2011-08-13 Xinchen Hui: Stop voting
- 2011-08-14 Xinchen Hui: Open voting again, since there comes some new objections
- 2011-12-13 Xinchen Hui: Won't apply now
