Get latest news and Web Designing tutoria

Friday 29 May 2015

PHP Booleans

A Boolean value is one that is in either of two states. They are known as True or False values, in programming. True is usually given a value of 1, and False is given a value of zero. You set them up just like other variables:
$true_value = 1;
$false_value = 0;

You can replace the 1 and 0 with the words "true" and "false" (without the quotes). But a note of caution, if you do. Try this script out, and see what happens:
You can replace the 1 and 0 with the words "true" and "false" (without the quotes). But a note of caution, if you do. Try this script out, and see what happens:
<?php
$true_value = true;
$false_value = false;
print ("true_value = " . $true_value);
print (" false_value = " . $false_value);
?>
What you should find is that the true_value will print "1", but the false_value won't print anything! Now replace true with 1 and false with 0, in the script above, and see what prints out.
Boolean values are very common in programming, and you often see this type of coding:
$true_value = true;
if ($true_value) {
print("that's true");
}
This is a shorthand way of saying "if $true_value holds a Boolean value of 1 then the statement is true". This is the same as:
if ($true_value == 1) {
print("that's true");
}
The NOT operand is also used a lot with this kind of if statement:
$true_value = true;
if (!$true_value) {
print("that's true");
}
else {
print("that's not true");
}
You'll probably meet Boolean values a lot, during your programming life. It's worth getting the hang of them!

0 comments:

Post a Comment