Note you can also use the '!' to convert a number to a
boolean, as if it was an explicit (bool) cast then NOT.
So you can do something like:
<?php
$t = !0; // This will === true;
$f = !1; // This will === false;
?>
And non-integers are casted as if to bool, then NOT.
Example:
<?php
$a = !array(); // This will === true;
$a = !array('a'); // This will === false;
$s = !""; // This will === true;
$s = !"hello"; // This will === false;
?>
To cast as if using a (bool) you can NOT the NOT with
"!!" (double '!'), then you are casting to the
correct (bool).
Example:
<?php
$a = !!array(); // This will === false; (as expected)
/*
This can be a substitute for count($array) > 0 or
!(empty($array)) to check to see if an array is empty or not
(you would use: !!$array).
*/
$status = (!!$array ? 'complete' : 'incomplete');
$s = !!"testing"; // This will === true; (as
expected)
/*
Note: normal casting rules apply so a !!"0" would
evaluate to an === false
*/
?>
----
Server IP: 64.71.164.2
Probable Submitter: 208.54.15.127
----
Manual Page -- http://www.php.net/manual/en/language.types.boolean.php
a>
Edit -- https://master
.php.net/note/edit/78099
Del: integrated -- h
ttps://master.php.net/note/delete/78099/integrated
Del: useless -- http
s://master.php.net/note/delete/78099/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78099/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78099/spam
Del: non-english --
https://master.php.net/note/delete/78099/non-english
Del: in docs -- http
s://master.php.net/note/delete/78099/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78099
Reject -- https://mast
er.php.net/note/reject/78099
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|