Content · Programming · PHP

PHP strcmp() Function: Usage and Examples

Compares two strings lexicographically, with case sensitivity.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP strcmp() compares two strings lexicographically, with case sensitivity. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is strcmp() in PHP?


Compares two strings lexicographically, with case sensitivity.

Its call structure is:

strcmp($string1, $string2)

The arguments identify the values or resources passed to strcmp(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.

Example 1: Basic Usage


This example introduces strcmp with a direct call and a small, readable input.

<?php

var_dump(strcmp("same", "same") === 0);

Output:

bool(true)

Compares two strings lexicographically, with case sensitivity. Compare against zero, not a particular positive or negative number.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how strcmp behaves.

<?php

var_dump(strcmp("Alpha", "alpha") !== 0);

Output:

bool(true)

The changed input illustrates why output may differ with the chosen values or environment. Compare against zero, not a particular positive or negative number.

Practical Example: Using the Feature


The following example incorporates strcmp into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$expectedCode = "A12"; $suppliedCode = "A12"; if (strcmp($expectedCode, $suppliedCode) === 0) { echo "Exact match"; }

Output:

Exact match

In a real application, check each operation result, validate values before use and clean up acquired resources. Compare against zero, not a particular positive or negative number.

Important Behavior and Edge Cases


  • Purpose: Compares two strings lexicographically, with case sensitivity.
  • Key limitation: Compare against zero, not a particular positive or negative number.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
strcmp()Compares two strings lexicographically, with case sensitivity.
strcasecmp()Ignores ASCII case when comparing two strings.

Common Beginner Mistakes


MistakeCorrect Approach
Compare against zero, not a particular positive or negative number.The correct approach depends on this behavior: Compare against zero, not a particular positive or negative number.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Compares two strings lexicographically, with case sensitivity.
  • Compare against zero, not a particular positive or negative number.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

strcmp is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

Share this resourceSend this page to someone who may find it useful.