Weird issue with e()

Hello,
I’ve got this weird issue with the e() function. I’m checking that the article has a cover before displaying it.

The code below returns an error when the cover is empty:

<?php e($article->cover()->isNotEmpty(), '<span><img src="' . $article->image($article->cover())->url() . '"/></span>' , '');?>

While this one doesn’t.

<?php e($article->cover()->isNotEmpty(), '<span><img src="' . thumb($article->file($article->cover()), array("width" => 1000))->url() . '"/></span>'  , '' );?>

I’ve spent a lot of time trying to debug what was going on here… The only difference really is using thumb in the bottom one. It seems $article is throwing off the ($condition, $a, $b) although there is no misplaced comma. Any thoughts?

Edit: the error is that I’m calling url() on an undefined object. Obviously it’s running the $a part of the code when it should be running $b…

Whatever the source of this error, when using html tags, I’d always prefer a proper if statement … otherwise it get’s messy.

Or at the very least a proper ternary operator
condition ? true-case : false-case

Thanks for your feedback both. I’ll take that into account :wink:

The issue is that e() is just an ordinary function, not a PHP built-in one. So the arguments you pass to it are evaluated before the function is called (while if only evaluates them if the condition is true). That’s why you should always use something like this for such “is not empty” checks:

<?php if($article->cover()->isNotEmpty()): ?><span><img src="<?= $article->image($article->cover())->url() ?>"/></span><?php endif ?>
2 Likes

Thanks, I will not make the same mistake again.