MIKESTOWE.COM

you are here

Weekly PHP Test

Assuming $cache->load($key) contains a cache of “yes” and $mediaCacheEnabled = true, what will the var_dump of $data return in this case?

PHP
1
2
3
4
5
if (!$data = $cache->load($key) || !$mediaCacheEnabled) {
$data = 'no';
}
var_dump($data);


..

..

..

..

..

..

..

..

..

..

ANSWER:

(bool) true

True is returned as $data = $cache->load($key) || !$mediaCacheEnabled is interpreted as a single statement. To prevent this from happening separate the $data condition from the $mediaCacheEnabled condition using parenthesis as follows:

PHP
1
if ((!$data = $cache->load($key)) || !$mediaCacheEnabled) {

Share this Page:
Facebook Twitter Linkedin Reddit Tumblr Email

Leave a Reply

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