Bird
0
0

Which of the following Blade snippets correctly uses raw PHP to set a variable $name to 'John'?

easy📝 Syntax Q3 of 15
Laravel - Views and Blade Templates
Which of the following Blade snippets correctly uses raw PHP to set a variable $name to 'John'?
A@php $name = 'John'; @endphp
B{{ $name = 'John' }}
C@php echo $name = 'John'; @endphp
D<?php $name = 'John'; ?>
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct raw PHP assignment in Blade

    Using @php ... @endphp to assign a variable is correct and clean.
  2. Step 2: Check other options

    {{ $name = 'John' }} tries to assign inside Blade echo which is invalid. @php echo $name = 'John'; @endphp echoes the assignment which is unnecessary. uses raw PHP tags which are discouraged in Blade.
  3. Final Answer:

    @php $name = 'John'; @endphp -> Option A
  4. Quick Check:

    Variable assignment in Blade raw PHP = @php ... @endphp [OK]
Quick Trick: Assign variables inside @php blocks, not inside {{ }} [OK]
Common Mistakes:
  • Trying to assign variables inside {{ }}
  • Echoing assignments unnecessarily
  • Using raw PHP tags instead of @php

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes