I am looking for a reason why in blade files (Laravel) code with mixed @php()
and @php ... @endphp
fails, and works fine with consistent usage of those codes across the file. I’ve just learned that the hard way but can’t find the reason.
Fails:
<code>@php($foo = 'foo')
<h1>{{ $foo }}</h1>
@php
$bar = $foo . 'bar';
@endphp
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
</code>
<code>@php($foo = 'foo')
<h1>{{ $foo }}</h1>
@php
$bar = $foo . 'bar';
@endphp
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
</code>
@php($foo = 'foo')
<h1>{{ $foo }}</h1>
@php
$bar = $foo . 'bar';
@endphp
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
Works fine:
<code>@php($foo = 'foo')
<h1>{{ $foo }}</h1>
@php($bar = $foo . 'bar');
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
</code>
<code>@php($foo = 'foo')
<h1>{{ $foo }}</h1>
@php($bar = $foo . 'bar');
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
</code>
@php($foo = 'foo')
<h1>{{ $foo }}</h1>
@php($bar = $foo . 'bar');
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
Also works fine:
<code>@php
$foo = 'foo';
@endphp
<h1>{{ $foo }}</h1>
@php
$bar = $foo . 'bar';
@endphp
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
</code>
<code>@php
$foo = 'foo';
@endphp
<h1>{{ $foo }}</h1>
@php
$bar = $foo . 'bar';
@endphp
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
</code>
@php
$foo = 'foo';
@endphp
<h1>{{ $foo }}</h1>
@php
$bar = $foo . 'bar';
@endphp
@for($i = 1; $i<10; $i++)
<h2><b>{{ $i }}<b/>{{ $bar }}</h2>
@endfor
An error