Current Location: Home> Latest Articles> How to handle empty strings when using strcmp function

How to handle empty strings when using strcmp function

gitbox 2025-05-29

Before you go deeper, let’s take a look at the basic usage of strcmp .

 <?php
$result = strcmp("apple", "banana");
echo $result; // Output negative number,because "apple" Less than "banana"
?>

The meaning of the return value of strcmp is:

  • Less than 0: The first string is less than the second string

  • Equal to 0: Two strings are equal

  • Greater than 0: The first string is greater than the second string

The question arises: What happens when the string is empty?

If there is a string that is empty, for example:

 <?php
$str1 = "";
$str2 = "banana";

$result = strcmp($str1, $str2);
echo $result; // Output negative number
?>

The result is still negative. This shows that strcmp can correctly handle empty strings and will not report errors. But the question is - do you expect such a comparison?

When many programmers use strcmp , their actual intention is to determine whether the two "non-empty" strings are the same. If one of them is empty, it may mean that there is something wrong with the business logic. For example, if a user submits a user name empty, and the other is an existing user name in the database, do you need to verify whether the user name is empty first?

Correct way to deal with it: first determine whether the string is empty

 <?php
if ($str1 === "" || $str2 === "") {
    echo "At least one string is empty,Can&#39;t compare。";
} else {
    if (strcmp($str1, $str2) === 0) {
        echo "Two strings are the same";
    } else {
        echo "Two strings are different";
    }
}
?>

This way you can avoid logical errors. Even if strcmp itself can handle empty strings, it will be safer to first determine whether it is an empty string from a business perspective.

Another common misunderstanding: use == to compare directly

 <?php
if ($str1 == $str2) {
    echo "equal";
}
?>

Although this writing is simpler, it can lead to type conversion problems in some cases, such as:

 <?php
$str1 = 0;
$str2 = "a";

if ($str1 == $str2) {
    echo "equal"; // Output,because "a" Will be converted to 0
}
?>

Therefore, it is recommended to use strcmp or === instead of loose comparisons.

Example: User input verification scenario

 <?php
function isValidUsername($input) {
    $reserved = "admin";
    
    if (trim($input) === "") {
        return "Username cannot be empty";
    }

    if (strcmp($input, $reserved) === 0) {
        return "Username cannot be reserved";
    }

    return "Valid username";
}

echo isValidUsername("");        // Username cannot be empty
echo isValidUsername("admin");   // Username cannot be reserved
echo isValidUsername("guest");   // Valid username
?>

This example shows how to gracefully combine empty judgment with strcmp to make judgments.

summary

When comparing strings using strcmp , if there is an empty string involved, the function itself will not report an error, but this does not mean that your business logic is correct. Suggested practices:

  1. First determine whether it is an empty string , and then make a comparison;

  2. Use trim() to clear unnecessary blanks;

  3. Avoid the type conversion problems caused by using loose comparisons ( == );

  4. Is it clear the context of string comparisons, and is it dictionary order? Is it equal? Is it case sensitive?

Through these methods, your string comparison logic will be more rigorous and you can avoid many hidden bugs. If you encounter strcmp in scenarios such as processing user input, URL parameters, configuration values, etc., you can use these techniques to improve it to improve the code robustness.

Reference link