Tools, FAQ, Tutorials:
Comparing Two Strings with strcmp() in PHP
How To Compare Two Strings with strcmp()?
✍: FYIcenter.com
PHP supports 3 string comparison operators, <, ==, and >, that generates Boolean values. But if you want to get an integer result by comparing two strings, you can the strcmp() function, which compares two strings based on ASCII values of their characters. Here is a PHP script on how to use strcmp():
<?php $a = "PHP is a scripting language."; $b = "PHP is a general-purpose language."; print('strcmp($a, $b): '.strcmp($a, $b)."\n"); print('strcmp($b, $a): '.strcmp($b, $a)."\n"); print('strcmp($a, $a): '.strcmp($a, $a)."\n"); ?>
This script will print:
strcmp($a, $b): 1 strcmp($b, $a): -1 strcmp($a, $a): 0
As you can see, strcmp() returns 3 possible values:
2016-10-13, 385👍, 0💬
Popular Posts:
How To Run a PHP Script? A standard alone PHP script can be executed directly with the PHP Command L...
How To Run a PHP Script? A standard alone PHP script can be executed directly with the PHP Command L...
How To Control Table Widths? Usually, browsers will calculate the table width based on the content w...
Can You Add Values to an Array without Keys in PHP? Can You Add Values to an Array with a Key? The a...
How To Get the Total Number of Values in an Array in PHP? You can get the total number of values in ...