What Do You Mean Truthy?
We looked at boolean values in the previous article. Booleans represent true or false. PHP Truthy Booleans, which are values that are not true or false, but when evaluated inside an expression, they can act like they are.
The best examples are the integers 0 and 1. If we passed 1 to the if statement, PHP would interpret the number 1 as truthy and would execute the statement inside the body. If you pass 0 to the if statement, PHP would interpret 0 as falsy, so it would not execute the statement inside the if statement body.
<?php if ( 1 ) { echo "This is a truthy value"; }if ( 0 ) { echo "This is a falsy value"; } ?>
As a matter of fact, we can add any integer value, positive or negative, and PHP will interpret them as truthy. It only interprets 0 as falsy.
<?php
if ( -5 ) {
echo "This statement will execute";
}
?>
There are other values that are considered truthy and falsy. The empty string “” is considered falsy.
<?php $camera = "";if ( $my_camera ) { echo "This statement won't execute"; } ?>
A non-empty string is actually considered truthy. If you have any string with a value inside it, like “something random,” that statement will be evaluated to true.
<?php $camera = "EOS R";if ( $camera ) { // This statement executes echo "You have a nice camera"; } ?>
Even if we passed the string “false” to the if statement, it’s still considered truthy.
<?php $some_string = "false";if ( $some_string ) { echo "Yup, this will execute"; } ?>
I know that we haven’t covered arrays just yet, but an array is just a storage container that can store multiple values, such as multiple strings.
<?php $cars = []; // empty arrayif ( $cars ) { echo "Empty arrays are considered falsy"; } ?>
Empty arrays are considered falsy in nature, so the statement inside the body will not be executed. A nonempty array is considered truthy, so the statement inside the body will be executed.
<?php $cars = ['MKIV Supra', 'R34 GTR', 'WRX STi'];if ( $cars ) { echo "What a car collection!"; } ?>
Developers will sometimes prefer truthy expressions like the one above to test and see if there’s anything inside the array. If there is, execute some code; I prefer to explicitly check. PHP has a count() function that counts the number of elements inside an array. We can then use the comparison operator and explicitly check to see if there are one or more elements inside the array. If there are, then execute the code. If not, skip over it.
<?php $cars = ['3000GT', 'S13 240sx', '240z'];if ( count($cars) > 0 ) { echo "Another impressive collection."; } ?>
PHP Tutorial Series
Continue your learning with these articles
TRUE OR FALSE?
The boolean value is a scalar data type that represents the values true and false. That’s really it. End of article. That would be a short article so lets take a look at a few examples before we abruptly end it.
What Do You Mean Truthy?
PHP – P5: Truthy Bools
Booleans represent true or false. PHP has truthy booleans, which are values that are not true or false, but when evaluated inside an expression, they can act like they are.
You Know, Integers. Like Math.
The integer data type represents whole numbers, either positive or negative or zero, not fractions.