We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Shorthand ternary operators

I want to use shorthand ternary operators in volt, is it possible?

In PHP 5.x:

$var = $x ?: $y;

In PHP 7.x:

$var = $x ?? $y;

My usage:

$var = request.getQuery('ids') ? request.getQuery('ids') : [];

Expected but gettting error

$var = request.getQuery('ids') ?: [];


47.7k

This is volt syntax:

{% set var = request.getQuery('ids') ?: [] %}


58.1k

Syntax error, unexpected token COLON



47.7k
Accepted
answer
edited Sep '16

How about:

{% set var = request.getQuery('ids')|default([]) %}

https://docs.phalcon.io/uk/latest/reference/volt.html#filters

As far as I can see at time of writing the volt compiler does not process elvis operator ?: or null coalescing operator ?? .



58.1k
edited Sep '16

That my answer! Great solution, thanks @baychae

I hadnt use before default filter, i like it ;)

How about:

{% set var = request.getQuery('ids')|default([]) %}

https://docs.phalcon.io/uk/latest/reference/volt.html#filters

As far as I can see at time of writing the volt compiler does not process elvis operator ?: or null coalescing operator ?? .