0
0
Laravelframework~20 mins

Control structures (@if, @foreach, @for) in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blade Control Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Blade snippet?
Consider the following Blade template code:
@for($i = 1; $i <= 3; $i++)
  @if($i % 2 == 0)
    Even
  @else
    Odd
  @endif
@endfor

What 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
AOddEvenOdd
BOdd Odd Odd
CEvenEvenEven
DOddEven
Attempts:
2 left
💡 Hint
Remember how the @for loop and @if condition work together to output text.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in Blade @foreach usage?
Identify which Blade @foreach loop syntax will cause a syntax error when compiled.
A
@foreach($items as $item)
  {{ $item }}
@endforeach
B
@foreach($items in $item)
  {{ $item }}
@endforeach
C
@foreach($items as $item)
  {{ $loop-&gt;index }} - {{ $item }}
@endforeach
D
@foreach($items as $key =&gt; $item)
  {{ $key }}: {{ $item }}
@endforeach
Attempts:
2 left
💡 Hint
Check the correct syntax for the @foreach directive in Blade.
state_output
advanced
2:00remaining
What is the value of $count after this Blade loop?
Given the following Blade code snippet:
@php
  $count = 0;
@endphp
@foreach(['a', 'b', 'c'] as $letter)
  @php
    if($letter === 'b') continue;
    $count++;
  @endphp
@endforeach

What 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
A2
B0
C1
D3
Attempts:
2 left
💡 Hint
Consider how the continue statement affects the loop iteration.
🔧 Debug
advanced
2:00remaining
Why does this Blade @if condition not work as expected?
Examine this Blade snippet:
@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
ABecause the @else block always executes regardless of the @if condition.
BBecause Blade does not support null checks in @if statements.
CBecause $value is a reserved keyword in Blade and cannot be used in conditions.
DBecause Blade compiles the @if condition before $value is reassigned later, so it uses the initial null value.
Attempts:
2 left
💡 Hint
Think about when Blade compiles and evaluates variables in templates.
🧠 Conceptual
expert
2:00remaining
Which statement about Blade @foreach and $loop variable is true?
Select the correct statement about the $loop variable inside a Blade @foreach loop.
A$loop->index starts counting from 1 and $loop->last is true only on the last iteration.
B$loop->count gives the number of iterations left including the current one.
C$loop->iteration starts counting from 1 and $loop->remaining gives the number of iterations left after the current one.
D$loop->first is true only on the second iteration of the loop.
Attempts:
2 left
💡 Hint
Recall the properties of the $loop variable in Blade documentation.