Challenge - 5 Problems
Blade Control Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the rendered output of this Blade snippet?
Consider the following Blade template code:
What will be the output when this template is rendered?
@for($i = 1; $i <= 3; $i++)
@if($i % 2 == 0)
Even
@else
Odd
@endif
@endforWhat will be the output when this template is rendered?
Laravel
@for($i = 1; $i <= 3; $i++) @if($i % 2 == 0) Even @else Odd @endif @endfor
Attempts:
2 left
💡 Hint
Remember how the @for loop and @if condition work together to output text.
✗ Incorrect
The loop runs from 1 to 3. For each number, it checks if it's even or odd. 1 is odd, 2 is even, 3 is odd, so the output concatenates 'Odd', 'Even', 'Odd' without spaces.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in Blade @foreach usage?
Identify which Blade @foreach loop syntax will cause a syntax error when compiled.
Attempts:
2 left
💡 Hint
Check the correct syntax for the @foreach directive in Blade.
✗ Incorrect
The correct syntax is '@foreach($collection as $item)'. Using 'in' instead of 'as' causes a syntax error.
❓ state_output
advanced2:00remaining
What is the value of $count after this Blade loop?
Given the following Blade code snippet:
What is the final value of $count after rendering?
@php
$count = 0;
@endphp
@foreach(['a', 'b', 'c'] as $letter)
@php
if($letter === 'b') continue;
$count++;
@endphp
@endforeachWhat is the final value of $count after rendering?
Laravel
@php $count = 0; @endphp @foreach(['a', 'b', 'c'] as $letter) @php if($letter === 'b') continue; $count++; @endphp @endforeach
Attempts:
2 left
💡 Hint
Consider how the continue statement affects the loop iteration.
✗ Incorrect
The loop skips incrementing $count when $letter is 'b'. So increments happen for 'a' and 'c' only, totaling 2.
🔧 Debug
advanced2:00remaining
Why does this Blade @if condition not work as expected?
Examine this Blade snippet:
Why does it always show 'Value is not set' even if $value is assigned a non-null value later in the template?
@php $value = null; @endphp @if($value) Value is set @else Value is not set @endif
Why does it always show 'Value is not set' even if $value is assigned a non-null value later in the template?
Laravel
@php $value = null; @endphp @if($value) Value is set @else Value is not set @endif
Attempts:
2 left
💡 Hint
Think about when Blade compiles and evaluates variables in templates.
✗ Incorrect
Blade templates are compiled once. If $value is null at compile time or before rendering, the condition evaluates false. Later changes to $value in the template do not affect the already compiled condition.
🧠 Conceptual
expert2:00remaining
Which statement about Blade @foreach and $loop variable is true?
Select the correct statement about the $loop variable inside a Blade @foreach loop.
Attempts:
2 left
💡 Hint
Recall the properties of the $loop variable in Blade documentation.
✗ Incorrect
$loop->iteration counts from 1, $loop->remaining counts how many iterations remain after the current one. $loop->index counts from 0. $loop->last is true on last iteration. $loop->first is true on first iteration.