Basic PHP Programming Question:
Download Job Interview Questions and Answers PDF
How To Compare Two Strings with Comparison Operators in PHP?
Answer:
PHP supports 3 string comparison operators, <, ==, and >, that generates Boolean values. Those operators use ASCII values of characters from both strings to determine the comparison results. Here is a PHP script on how to use comparison operators:
<?php
$a = "PHP is a scripting language.";
$b = "PHP is a general-purpose language.";
if ($a > $b) {
print('$a > $b is true.'." ");
} else {
print('$a > $b is false.'." ");
}
if ($a == $b) {
print('$a == $b is true.'." ");
} else {
print('$a == $b is false.'." ");
}
if ($a < $b) {
print('$a < $b is true.'." ");
} else {
print('$a < $b is false.'." ");
}
?>
This script will print:
$a > $b is true.
$a == $b is false.
$a < $b is false.
<?php
$a = "PHP is a scripting language.";
$b = "PHP is a general-purpose language.";
if ($a > $b) {
print('$a > $b is true.'." ");
} else {
print('$a > $b is false.'." ");
}
if ($a == $b) {
print('$a == $b is true.'." ");
} else {
print('$a == $b is false.'." ");
}
if ($a < $b) {
print('$a < $b is true.'." ");
} else {
print('$a < $b is false.'." ");
}
?>
This script will print:
$a > $b is true.
$a == $b is false.
$a < $b is false.
Download PHP Interview Questions And Answers
PDF
Previous Question | Next Question |
How To Concatenate Two Strings Together in PHP? | How To Convert Numbers to Strings in PHP? |