PHP: Booleans


Before I usually used other method to check if the array has value or non empty array (true or false)

like:

if (is_array($arr) && count($arr) > 0) {
     //true
}

To shorten this if you need need to check if array has value then you can just simply to like this:

if ($arr) {
     //true
}

non empty array always return true else false.

but be careful take note, in PHP

0  = false

-1 = negative values are always true

see below guide lines

var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)

Published by

Emisyl

Is a web developer and a Php enthusiasts base in the Philippines

Other sites
https://planetcodes.blogspot.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Emisyl