SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Comparisons (I)

PHP Example #18

Comparison operators

Comparison operators are a common piece in decision structures; they allow to define more complex conditions. There are several of these operators: equal to (==), not equal to (!=), greater than (>), lesser than (<), greater or equal than (>=) and lesser or equal than (<=).

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.

I have been at North America
I have been at South America
I have been at Europe
I have been at Africa
I have been at Asia
I have been at Oceania
I have been at Antarctica

<?php
$northamerica = $_POST['northamerica'];
$southamerica = $_POST['southamerica'];
$europe = $_POST['europe'];
$africa = $_POST['africa'];
$asia = $_POST['asia'];
$oceania = $_POST['oceania'];
$antarctica = $_POST['antarctica'];
$count = 0;
if ($northamerica != "1") {
print "You haven't ever been at North America.\n";
} else {
++$count;
}
if ($southamerica != "1") {
print "You haven't ever been at South America.\n";
} else {
++$count;
}
if ($europe != "1") {
print "You haven't ever been at Europe.\n";
} else {
++$count;
}
if ($africa != "1") {
print "You haven't ever been at Africa.\n";
} else {
++$count;
}
if ($asia != "1") {
print "You haven't ever been at Asia.\n";
} else {
++$count;
}
if ($oceania != "1") {
print "You haven't ever been at Oceania.\n";
} else {
++$count;
}
if ($antarctica != "1") {
print "You haven't ever been at Antarctica.\n";
} else {
++$count;
}
if ($count == 0) {
print "You are not from this planet. Have you ever been at Mars?\n";
} else {
print "You have visited $count continents.\n";
}
?>

PHP Example #19

Comparison operators

Comparison operators are a common piece in decision structures; they allow to define more complex conditions. There are several of these operators: equal to (==), not equal to (!=), greater than (>), lesser than (<), greater or equal than (>=) and lesser or equal than (<=).

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.

My age is:
<?php
$age = $_POST['age'];
if ($age >= 18) {
print "You are adult and thus old enough to watch a sex movie.\n";
} else {
print "You are too young to watch a sex movie.\n";
}
if ($age < 14) {
print "You are not even a teenager.\n";
}
if ($age >= 65) {
print "You are old enough for a discount.\n";
}
if ($age <= 40) {
print "You are still not old.\n";
}
if ($age > 80) {
print "You are very old fart.\n";
}
if ($age > 100) {
print "You are indeed a centenary old fart.\n";
}
?>

PHP Example #20

Comparing float numbers

Float numbers are stored in such a way that they could be not exactly equal to their assigned value. For such reason, it is not recommendable to use the operator == to compare them; instead a tolerance value should be used to compare them. The abs() function is useful for such operations.

<?php
$tolerance = 0.001;
$number1 = 50.0;
$number2 = 50.0002;
$number3 = 50.002;
if (abs($number1 - $number2) < $tolerance) {
print nl2br("Comparison between $number1 and $number2 with a tolerance of $tolerance. Precision is good enough.\n");
} else {
print nl2br("Comparison between $number1 and $number2 with a tolerance of $tolerance. Precision is not good enough.\n");
}
if (abs($number1 - $number3) < $tolerance) {
print nl2br("Comparison between $number1 and $number3 with a tolerance of $tolerance. Precision is good enough.\n");
} else {
print nl2br("Comparison between $number1 and $number3 with a tolerance of $tolerance. Precision is not good enough.\n");
}
?>
Comparison between 50 and 50.0002 with a tolerance of 0.001. Precision is good enough.
Comparison between 50 and 50.002 with a tolerance of 0.001. Precision is not good enough.