SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Comparisons (II)

PHP Example #21

Comparing strings

Comparing strings with the operators < and > will apply the criterion of alphabetical order, samely as in a dictionary. Write in the text field a word starting by A or one starting by Z.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Write a word:
<?php
$word = $_POST['letters'];
if ($word < 'b') {
print "Your word starts with the first letter of the alphabet.\n";
if ($word < 'amplitude') {
print "Your word goes before the word 'amplitude' in a dictionary.\n";
} else {
print "Your word goes after the word 'amplitude' in a dictionary.\n";
}
}
if ($word > 'y') {
print "Your word starts with the last letter of the alphabet.\n";
if ($word > 'zen') {
print "Your word goes after the word 'zen' in a dictionary.\n";
} else {
print "Your word goes before the word 'zen' in a dictionary.\n";
}
}
?>

PHP Example #22

Comparing strings

Comparison of strings can have unexpected results when they contain numbers. When the PHP interpreter sees strings like these, they are treated as numbers for comparison.

<?php
// These values are compared by dictionary order
if ("x54321" > "x5678") {
print 'The string "x54321" is greater than the string "x5676". ';
} else {
print 'The string "x54321" is not greater than the string "x5676". ';
}
// These values are compared by numeric order
if ("54321" > "5678") {
print 'The string "54321" is greater than the string "5676". ';
} else {
print 'The string "54321" is not greater than the string "5676". ';
}
// These values are compared by dictionary order
if ('6 pack' < '55 card stud') {
print 'The string "6 pack" is less than the string "55 card stud". ';
} else {
print 'The string "6 pack" is not less than the string "55 card stud". ';
}
// These values are compared by numeric order
if ('6 pack' < 55) {
print 'The string "6 pack" is less than the number 55. ';
} else {
print 'The string "6 pack" is not less than the number 55". ';
}
?>
The string "x54321" is not greater than the string "x5676". The string "54321" is greater than the string "5676". The string "6 pack" is not less than the string "55 card stud". The string "6 pack" is less than the number 55.

PHP Example #23

Comparing strings with strcmp()

The strcmp() function compares two strings following the criterion of dictionary order, returning a positive number if the first string is greater or a negative number if the opposite is true. If both strings are equal the function returns a 0.

<?php
$x = strcmp("x54321", "x5678");
if ($x > 0) {
print 'The string "x54321" is greater than the string "x5676". ';
} else {
print 'The string "x54321" is lesser than the string "x5676". ';
}
$x = strcmp("54321", "5678");
if ($x > 0) {
print 'The string "54321" is greater than the string "5676". ';
} else {
print 'The string "54321" is lesser than the string "5676". ';
}
$x = strcmp('6 pack', '55 card stud');
if ($x > 0) {
print 'The string "6 pack" is greater than the string "55 card stud". ';
} else {
print 'The string "6 pack" is not lesser than the string "55 card stud". ';
}
$x = strcmp('6 pack', 55);
if ($x > 0) {
print 'The string "6 pack" is greater than the number 55. ';
} else {
print 'The string "6 pack" is not lesser than the number 55". ';
}
?>
The string "x54321" is lesser than the string "x5676". The string "54321" is lesser than the string "5676". The string "6 pack" is greater than the string "55 card stud". The string "6 pack" is greater than the number 55.