PHP strcmp
Compare the entire text of two case-sensitive identifiers.
A simple example
strcmp
- Start with "a" and "a"
- Do this Compare the whole text
- Result Equal
<?php
$value = "a";
$other = "a";
echo ((strcmp($value, $other) === 0) ? "yes" : "no"), PHP_EOL;
yes
Syntax
strcmp($value, $other) === 0
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpVerify a locker code
A reader compares two identifiers that may look similar on their labels. Use the full values under the intended case rules so a difference in one letter or digit is not lost.
<?php
$value = "B-17";
$other = "B-17";
echo ((strcmp($value, $other) === 0) ? "yes" : "no"), PHP_EOL;
yes
Every character in the scan matches the assigned locker.
Distinguish two product variants
A shop sells two variants whose printed product codes differ by letter case. Compare them under the catalog's exact matching policy so the stockroom does not merge separate items.
<?php
$value = "sku-A";
$other = "sku-a";
echo ((strcmp($value, $other) === 0) ? "yes" : "no"), PHP_EOL;
no
The final character has different case.
Validate a locker ticket
The first reading comes from the ticket and the second from the lock. The reader compares both identifiers character for character. Do the two spellings match?
What to watch for
Unicode text with the same visual appearance may use different normalization forms.