Current Location: Home> Latest Articles> PHP strncmp() Function: How to Compare Strings, Files, and Arrays

PHP strncmp() Function: How to Compare Strings, Files, and Arrays

gitbox 2025-06-17

What is the `strncmp()` Function?

In PHP, the `strncmp()` function is used to compare the first N characters of two strings and return the comparison result. This is especially useful when you need to match string prefixes exactly. Unlike `strstr()`, which performs partial matching, `strncmp()` compares the full beginning of two strings up to the specified length.

The function accepts three parameters: the first string to compare, the second string, and the number of characters to compare.


$string1 = 'apple';
$string2 = 'banana';
$result = strncmp($string1, $string2, 3);
echo $result;

In the above code, we compare the first three characters of the strings 'apple' and 'banana'. Since the ASCII value of 'a' is 97 and 'b' is 98, the result is -1 because 97 is smaller than 98. If we compare only the first two characters, the result would be 0 because 'ap' and 'ba' have equal ASCII values.

Use Cases of `strncmp()` for String Comparison

1. Using `strncmp()` to Compare Passwords

`strncmp()` can be used to safely compare plain-text passwords with hashed passwords. In this case, hashed versions of passwords are often stored on the server, and plain-text passwords are hashed during comparison for validation.


$password = 'password1';
$hashed_password = md5($password);
// Store the hashed password in the database
// ...

// Compare the entered password with the stored password
$string1 = $hashed_password;
$string2 = $db_password;
$result = strncmp($string1, $string2, strlen($string1));

if ($result == 0) {
    echo 'Password match';
} else {
    echo 'Password does not match';
}

In the above code, we compare the user's entered password with the hashed password retrieved from the database. If the result is 0, the passwords match, and the output will be 'Password match'. If not, the output will be 'Password does not match'.

2. Using `strncmp()` to Compare File Contents

Sometimes, we need to compare the contents of two files. `strncmp()` can be used for this task as well. The following code example demonstrates how to compare the content of two files:


$file1 = 'file1.txt';
$file2 = 'file2.txt';

$fp1 = fopen($file1, 'r');
$contents1 = fread($fp1, filesize($file1));
fclose($fp1);

$fp2 = fopen($file2, 'r');
$contents2 = fread($fp2, filesize($file2));
fclose($fp2);

$result = strncmp($contents1, $contents2, strlen($contents1));

if ($result == 0) {
    echo 'File contents are identical';
} else {
    echo 'File contents are not identical';
}

In this example, we compare the contents of two text files. If the contents are identical, the result is 0, otherwise, a non-zero result is returned.

3. Using `strncmp()` to Compare Arrays

Although `strncmp()` is primarily used for string comparison, we can convert arrays to strings and then compare them. This can be done by using `json_encode()` to convert an array to a JSON string, and then applying `strncmp()` to compare them.


$array1 = array('apple', 'banana');
$array2 = array('apple', 'banana');

$result = strncmp(json_encode($array1), json_encode($array2), strlen(json_encode($array1)));

if ($result == 0) {
    echo 'Arrays are identical';
} else {
    echo 'Arrays are not identical';
}

In this example, we convert two arrays to JSON strings and use `strncmp()` to compare them. If the array contents are identical, the result is 0.

Summary

The `strncmp()` function in PHP is a very useful tool that allows developers to compare strings, file contents, and even arrays. By using `strncmp()` effectively, we can simplify our code, increase efficiency, and ensure secure password comparison and content matching in a variety of use cases.