|
Free
PHP Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
|||||
PHP Tutorials |
||||||
|
Boolean Values in PHP
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; 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; print ("true_value = " . $true_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) { 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) { The NOT operand is also used a lot with this kind of if statement: $true_value = true; if (!$true_value) { You'll probably meet Boolean values a lot, during your programming life. It's worth getting the hang of them! |