Boolean Variables in PHP

As discussed in Variables in PHP, there is no need to specify the type of a variable in PHP. So, if you want to use the boolean variable in PHP, just specify the value

$booleanVariableName = value;

A boolean variable can take the following values true, false, TRUE, FALSE. And the name suggests their meanings, Let’s use them in some examples. Let’s start with the value TRUE.


$isWeekend = true;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = TRUE;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

The output is

Have a great Weekend!
Have a great Weekend!

Let’s now move on with FALSE

$isWeekend = false;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = FALSE;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

Let’s see the output.

Have a great Week!
Have a great Week!

We have used the available PHP boolean literals to signify the boolean values. Let’s explore more on the boolean literals by printing their values

echo true, "n";
echo false, "n";

echo "The value of true is ", true, "n";
echo "The value of false is ", false, "n";

The output will be a bit surprising

1

The value of true is 1
The value of false is 

As you see that corresponding to the boolean literal true, you see the value 1 and for false, no value is printed. This is because in PHP, a NULL, false, FALSE, an empty string all corresponds to the boolean false. So we see an empty string corresponding to false.

Let’s see what happens when we use NULL. As discussed, it corresponds to a false

$isWeekend = NULL;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

The output is

Have a great Week

Let’s now explore some conditional expressions, where we check simple equality conditions. From our earlier discussions, you can easily guess the output

echo 2 == 2, "n";
echo 2 != 2, "n";

The output is

1

The complete example

echo true, "n";
echo false, "n";

echo "The value of true is ", true, "n";
echo "The value of false is ", false, "n";

echo 2 == 2, "n";
echo 2 != 2, "n";

$isWeekend = true;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = TRUE;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = false;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = FALSE;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = "";
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = "false";
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

$isWeekend = NULL;
echo "Have a great ";
if ($isWeekend) {
    echo "Weekend!n";
}
else {
    echo "Week!n";
}

The output is

1

The value of true is 1
The value of false is 
1

Have a great Weekend!
Have a great Weekend!
Have a great Week!
Have a great Week!
Have a great Week!
Have a great Weekend!
Have a great Week!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s