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:


$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!


=== and !==

In recent editions of PHP, two new operators have been introduced: the triple
equals sign ( = = =) and an exclamation, double equals ( != =). These are used
to test if one value has the same as another AND are of the same type. An example
would be:

$number = 3;
$text = ‘three’;

if ($number === $text) {


else {

print(“Not the same”);


So this asks, “Do the variables match exactly?” Since one is text
and the other is a number, the answer is “no”, or false. We won’t
be using these operators much, if at all!


Kaynak : ‘sitesinden alıntı

Yorum Yap