Skip to content

Unexpected return values: strcmp(), strncmp(), strcasecmp(), strncasecmp #3223

@sheinrich177

Description

@sheinrich177

Description

Disclaimer: This is my first bug report, I researched but didn't find anything related. Please request if any further information is required or close this report if it isn't valid.

Description:

During PHP 8.1 to 8.3 update check for a project I found change information regarding return values of strcmp(), strncmp(), stcasecmp() and strncasecmp() in PHP 8.2:

The new return values are also mentioned on each of the documentation pages.

Expected behavior:

  • return values: 0, -1 and 1

Actual behavior:

  • different return values (see below)

Environment:

  • Linux Mint 21.3 Virginia
  • different PHP versions by: ppa:ondrej/php

test.php:

<?php

    var_dump(
            strcmp('bar', 'foo'),
            strcmp('foo', 'bar'),
            strcasecmp('bar', 'foo'),
            strcasecmp('foo', 'bar'),
            strncmp('bar', 'foo', 2),
            strncmp('foo', 'bar', 2),
            strncasecmp('bar', 'foo', 2),
            strncasecmp('foo', 'bar', 2),
    );

Output:

  • 8.1:
$ /usr/bin/php8.1 -v && /usr/bin/php8.1 -f test.php 

PHP 8.1.27 (cli) (built: Dec 21 2023 20:19:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.27, Copyright (c), by Zend Technologies
    
int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
  • 8.2:
$ /usr/bin/php8.2 -v && /usr/bin/php8.2 -f test.php 

PHP 8.2.15 (cli) (built: Jan 20 2024 14:17:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.15, Copyright (c), by Zend Technologies

int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
  • 8.3:
$ /usr/bin/php8.3 -v && /usr/bin/php8.3 -f test.php 

PHP 8.3.3-1+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Feb 15 2024 18:38:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.3-1+ubuntu22.04.1+deb.sury.org+1, Copyright (c), by Zend Technologies

int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)

Additionally (better: confusingly), I get a different output here: https://3v4l.org/C1i18 (please check current 8.2 and 8.3 versions separately - all supported versions include version 8.1).

int(-265725)
int(265725)
int(-4)
int(4)
int(-265742)
int(265742)
int(-4)
int(4)

PHP Version

PHP 8.2 / PHP 8.3

Operating System

Linux Mint 21.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions