R() and e() bug

Morning @texnixe
Before I open a bug on github can you double check and make sure this is really a bug?

I’m using the r() and e() helpers and I’m experiencing a weird behavior.
If I do this

e( isset($var) , $var);

I get an Undefined variable: var even though isset does indeed return false in this case. Same thing happen with r()

Can you double check and let me know if this is a bug or if I’m doing something wrong?
And, if it’s a bug, should I open an issue in the toolkit repo or in the kirby core repo?

Thanks

No, this is not a bug. r() and e() are functions to which you are passing arguments. And in the example above, the second argument ($var) is not defined and therefore, you get the error. That is, the expression is not evaluated within the parentheses, but only in the return statement.

In this case, the alternative to a standard if statement is using the ternary operator:

<?php echo isset($var)? $var: null; ?>

e() will only work if you pass a known variable or a string:

<?php e(isset($var), 'blue', 'black'); ?>
<?php
$var = 'red';
e($var == 'blue', $var, 'black'); 
?>

Got it. Make sense now that I think about it.
I’ll stick with the classic ?: then.

Thanks

If you are on PHP 7 also check out: https://wiki.php.net/rfc/isset_ternary

3 Likes

Nice, didn’t know about this one.
Very useful, thanks