<select id="year" name="year" class="form-control ">
{{ $last= date('Y')-120 }}
{{ $now = date('Y') }}
@for ($i ={{ $now }}; $i <= {{ $last }}; $i--)
<option value="{{ $i }}">{{ $i }}</option>
@endfor
</select>
我收到了错误消息 Parse error: syntax error, unexpected '<'
看起来变量无法读取。
如何在for循环中设置值?
最佳答案
基本上 {{ $last= date('Y')-120 }}
在此部分中,您将显示该值,但您需要分配该值。所以像这样分配:
<?php $last= date('Y')-120; ?>
同样的事情也适用于 for 循环。只需比较值。不要将它放在 Blade 语法中。
<select id="year" name="year" class="form-control ">
{{ $last= date('Y')-120 }}
{{ $now = date('Y') }}
@for ($i = $now; $i >= $last; $i--)
<option value="{{ $i }}">{{ $i }}</option>
@endfor
</select>
关于php - 如何在 laravel blade 的 for-loop 中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045911/