strcmp / strncmp

strcmp(string $string1, string $string2): int — strncmp(string $string1, string $string2, int $length): int

Binary-safe string comparison: returns the sign of the difference (<0/0/>0), not a bool - only the sign is portable (PHP 8.2+ no longer guarantees the strlen difference); strncmp compares only the first n characters.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    // strcmp returns the sign of the difference; only the sign is portable
    printf("%d\n", strcmp("apple", "apple"));
    printf("%s\n", strcmp("apple", "apricot") < 0 ? "lt" : "ge");
    printf("%s\n", strncmp("photo.jpg", "photo.png", 6) == 0 ? "same prefix" : "different");
    return 0;
}
Output
0
lt
same prefix

C++ C++26

C++26
#include <iostream>
#include <string>

int main() {
    // std::string::compare returns the same sign convention as strcmp
    std::string a = "apple";
    std::string p = "photo.jpg";
    std::string q = "photo.png";
    std::cout << a.compare("apple") << "\n";
    std::cout << (a.compare("apricot") < 0 ? "lt" : "ge") << "\n";
    std::cout << (p.compare(0, 6, q, 0, 6) == 0 ? "same prefix" : "different") << "\n";
}
Output
0
lt
same prefix

C# v11.0.100

v11.0.100
// string.CompareOrdinal gives the sign; there is no strncmp - substring compare is the equivalent
Console.WriteLine(string.CompareOrdinal("apple", "apple"));
Console.WriteLine(string.CompareOrdinal("apple", "apricot") < 0 ? "lt" : "ge");
Console.WriteLine(string.CompareOrdinal("photo.jpg", 0, "photo.png", 0, 6) == 0 ? "same prefix" : "different");
Output
0
lt
same prefix

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	// strings.Compare normalises to -1/0/+1
	fmt.Println(strings.Compare("apple", "apple"))
	if strings.Compare("apple", "apricot") < 0 {
		fmt.Println("lt")
	} else {
		fmt.Println("ge")
	}
	if "photo.jpg"[:6] == "photo.png"[:6] { // no strncmp - slice then compare
		fmt.Println("same prefix")
	} else {
		fmt.Println("different")
	}
}
Output
0
lt
same prefix

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // String.compareTo is the strcmp equivalent; regionMatches is strncmp
        System.out.println("apple".compareTo("apple"));
        System.out.println("apple".compareTo("apricot") < 0 ? "lt" : "ge");
        System.out.println("photo.jpg".regionMatches(0, "photo.png", 0, 6) ? "same prefix" : "different");
    }
}
Output
0
lt
same prefix

JavaScript v26.13.1

v26.13.1
// JS has no strcmp: === gives equality, < / > give the lexicographic sign
const a = "apple";
console.log(a === "apple" ? 0 : a < "apple" ? -1 : 1);
console.log(a < "apricot" ? "lt" : "ge");
console.log("photo.jpg".slice(0, 6) === "photo.png".slice(0, 6) ? "same prefix" : "different");
Output
0
lt
same prefix

PHP v8.6.0

v8.6.0
// strcmp returns the SIGN of the difference - never test the exact value;
// only the sign is portable (PHP 8.2+ may return -1/1 instead of the strlen gap)
echo strcmp("apple", "apple"), "\n";
echo strcmp("apple", "apricot") < 0 ? "lt" : "ge", "\n";
echo strncmp("photo.jpg", "photo.png", 6) == 0 ? "same prefix" : "different", "\n";
Output
0
lt
same prefix

Python v3.15.2

v3.15.2
# Python: == and < on str are the sign-free equivalents; no strncmp - slice then compare
print(0 if "apple" == "apple" else 1)
print("lt" if "apple" < "apricot" else "ge")
print("same prefix" if "photo.jpg"[:6] == "photo.png"[:6] else "different")
Output
0
lt
same prefix

Rust v1.99.1

v1.99.1
use std::cmp::Ordering;

fn main() {
    // Ord::cmp returns a typed Ordering - the sign, made explicit
    let ord = "apple".cmp(&"apple");
    println!("{}", match ord {
        Ordering::Equal => 0,
        Ordering::Less => -1,
        Ordering::Greater => 1,
    });
    println!("{}", if "apple" < "apricot" { "lt" } else { "ge" });
    println!("{}", if &"photo.jpg"[..6] == &"photo.png"[..6] { "same prefix" } else { "different" });
}
Output
0
lt
same prefix

TypeScript v26.13.1

v26.13.1
// TS has no strcmp: === gives equality, < / > give the lexicographic sign
const a: string = "apple";
console.log(a === "apple" ? 0 : a < "apple" ? -1 : 1);
console.log(a < "apricot" ? "lt" : "ge");
console.log("photo.jpg".slice(0, 6) === "photo.png".slice(0, 6) ? "same prefix" : "different");
Output
0
lt
same prefix

Language differences

C: strcmp/strncmp are the originals; only the sign of the return value is portable - the exact gap is an implementation detail.

C++: string::compare follows the same sign convention.

C#: CompareOrdinal is ordinal (no locale); the index-based overload is strncmp.

Go: strings.Compare normalises to -1/0/+1; slicing is the idiomatic strncmp.

Java: compareTo is strcmp; regionMatches is strncmp.

JavaScript: no standalone function - === plus < is the equivalent.

PHP: == is a loose comparison (numeric strings compare numerically since PHP 8) - use strcmp for content comparison; NEVER compare passwords/hashes with strcmp - its early-exit timing is a side channel, use hash_equals for hash comparison (see the hash_equals entry); strncmp's length argument bounds the read.

Python: < and == work directly; slice when you need a prefix comparison.

Rust: Ord::cmp returns a typed Ordering - the compiler forces handling all three cases.

TypeScript: same as JavaScript.