PHP7 DATA TYPES INTEGERS FLOATS STRINGS BOOLEAN LESSON 10

Booleans in PHP7

A Boolean value will return TRUE or FALSE. If an integer is zero 0 it is considered FALSE in PHP7. If a string has nothing in it then it is considered FALSE in PHP7. Negative numbers are considered TRUE to Boolean. Here are some examples.

Integer Boolean False

<?php $var=0; var_dump((bool) $var); ?>

Result would be: bool(false).

Integer Boolean True

Notice we are using a negative 2 “-2”. Even though it is negative number it is still TRUE to Boolean.

Only cool people share!

<?php $var=-2; var_dump((bool) $var); ?>

Result would be: bool(true)

String Boolean False

<?php $var=""; var_dump((bool) $var); ?>

Result would be: bool(false).

How We Would Use Booleans

We use booleans in PHP to see if something is true or false. So if we need to see if a variable has a value or not we can use the var_dump((bool) $var); to see if there is anything in it or not.


booleans-in-php7

Here are the complete files for this lesson. 

 

PHP7 DATA TYPES INTEGERS FLOATS STRINGS BOOLEAN LESSON 10 was last modified: October 29th, 2021 by Maximus Mccullough
PHP7 DATA TYPES HOW THEY ARE USED

Pages:Previous 1 2 3 4

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.