Bird
Raised Fist0
Laravelframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the @if directive do in a Laravel Blade template?
easy
A. It shows content only if a condition is true.
B. It repeats content for each item in a list.
C. It repeats content a fixed number of times.
D. It defines a variable inside the template.

Solution

  1. Step 1: Understand the purpose of @if

    The @if directive is used to check a condition and show content only when that condition is true.
  2. Step 2: Compare with other directives

    @foreach repeats content for each item, and @for repeats content a set number of times, so they do not match the description.
  3. Final Answer:

    It shows content only if a condition is true. -> Option A
  4. Quick Check:

    @if = conditional display [OK]
Hint: @if controls conditional display of content [OK]
Common Mistakes:
  • Confusing @if with @foreach or @for
  • Thinking @if repeats content
  • Assuming @if defines variables
2. Which of the following is the correct syntax to start a @foreach loop in Laravel Blade?
easy
A. @foreach $items as $item
B. @foreach $item in $items
C. @foreach($item in $items)
D. @foreach($items as $item)

Solution

  1. Step 1: Recall correct @foreach syntax

    The correct syntax uses parentheses and the keyword as: @foreach($items as $item).
  2. Step 2: Identify incorrect options

    Options A and D miss parentheses, and C uses incorrect syntax in instead of as.
  3. Final Answer:

    @foreach($items as $item) -> Option D
  4. Quick Check:

    @foreach syntax = parentheses + 'as' [OK]
Hint: Use parentheses and 'as' keyword in @foreach [OK]
Common Mistakes:
  • Omitting parentheses
  • Using 'in' instead of 'as'
  • Missing $ on variables
3. What will be the output of this Blade snippet?
@for($i = 1; $i <= 3; $i++)
  Number: {{ $i }}
@endfor
medium
A. No output because of syntax error
B. Number: 0 Number: 1 Number: 2
C. Number: 1 Number: 2 Number: 3
D. Number: 1 Number: 2 Number: 3 Number: 4

Solution

  1. Step 1: Analyze the for loop range

    The loop starts at 1 and runs while $i is less than or equal to 3, so it runs for 1, 2, and 3.
  2. Step 2: Determine output for each iteration

    Each iteration prints "Number: " followed by the current $i value, so output is "Number: 1 Number: 2 Number: 3".
  3. Final Answer:

    Number: 1 Number: 2 Number: 3 -> Option C
  4. Quick Check:

    Loop 1 to 3 prints numbers 1,2,3 [OK]
Hint: Count loop from start to end inclusive [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Including extra iteration beyond 3
  • Assuming syntax error
4. Identify the error in this Blade code:
@foreach($users as user)
  {{ $user->name }}
@endforeach
medium
A. Missing parentheses around $users
B. Missing $ before user in the loop declaration
C. Incorrect closing tag, should be @endfor
D. No error, code is correct

Solution

  1. Step 1: Check foreach syntax

    The variable in the loop must have a $ sign: @foreach($users as $user).
  2. Step 2: Verify other parts

    Parentheses are present, and closing tag is correctly @endforeach. So only missing $ is the error.
  3. Final Answer:

    Missing $ before user in the loop declaration -> Option B
  4. Quick Check:

    Variable names need $ in @foreach [OK]
Hint: Always use $ before variables in @foreach [OK]
Common Mistakes:
  • Omitting $ before loop variable
  • Using wrong closing directive
  • Missing parentheses
5. You want to display a list of products only if there are any products in the array. Which Blade code correctly does this?
hard
A. @if(count($products) > 0) @foreach($products as $product) {{ $product->name }} @endforeach @endif
B. @foreach($products as $product) @if($products) {{ $product->name }} @endif @endforeach
C. @for($i = 0; $i < count($products); $i++) @if($products[$i]) {{ $products[$i]->name }} @endif @endfor
D. @if($products) @foreach($products as $product) {{ $product->name }} @endforeach @endif

Solution

  1. Step 1: Check condition to display list

    @if(count($products) > 0) @foreach($products as $product) {{ $product->name }} @endforeach @endif uses @if(count($products) > 0) which correctly and explicitly checks if products exist before looping.
  2. Step 2: Analyze other options

    B places the check inside the loop, which is redundant since @foreach skips empty arrays. C uses manual @for with array indexing, which is error-prone (e.g., for associative arrays) and less idiomatic than @foreach. D uses @if($products), which works via implicit boolean conversion (empty arrays are falsy) but is less explicit and readable than count > 0.
  3. Final Answer:

    @if(count($products) > 0) followed by @foreach -> Option A
  4. Quick Check:

    Check count > 0 before @foreach [OK]
Hint: Use count() > 0 to check non-empty arrays before loops [OK]
Common Mistakes:
  • Using implicit @if($products) instead of explicit count > 0
  • Placing condition inside loop unnecessarily
  • Using @for with inner @if redundantly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard