samedi 25 août 2018

PHP not evaluating expression from left to right?

I have the following expression:

if (!empty($forums) && count($forums) == 1)

Based on the order of evaluation, the first thing it should check is if $forums is not empty. If it is empty, then it should evaluate to false immediately when it hits the &&. At that point it should exit out of the if statement and not even attempt the count().

However, this error was produced:

ErrorException: [E_WARNING] count(): Parameter must be an array or an object that implements Countable

The stack trace shows that it attempted to do count(NULL). If $forums is evaluating to NULL; then why is the expression continuing instead of exiting out of the if statement?

Aucun commentaire:

Enregistrer un commentaire