lundi 18 mars 2019

PHP empty() returns true for a set variable, except over CLI

In a PHP 7.3 Laravel project, empty() is returning an incorrect result when the code runs in a browser, but the correct result when run over command line (within a Tinker session).

Here is the problematic code:

echo $practiceArea->hero_video; // Added to confirm that the value is set. Result: "https://www.youtube.com/embed/0qisGSwZym4" is echoed
if(!empty($practiceArea->hero_video)) { 
   ... some HTML
}

The HTML does not fire because the condition always evaluates to false.

I've also tried:

if(empty($practiceArea->hero_video)) === false {

Yet if I try this same evaluation within a Tinker session, I get the opposite result:

>>> $pa = PracticeArea::find(11)
>>> ... an object is returned
>>> $pa->hero_video
=> "//www.youtube.com/embed/0qisGSwZym4"
>>> if(empty($pa->hero_video)) echo "Empty"; else echo "Not empty";
Not empty
>>> if(!empty($pa->hero_video)) echo "Not empty"; else echo "Empty";
Not empty



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire