0
0
Laravelframework~10 mins

Raw PHP in Blade (@php) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Raw PHP in Blade (@php)
Start Blade Template
Encounter @php block
Execute PHP code inside block
Output or store results
Continue rendering Blade template
End Template Rendering
Blade template engine reads the @php block, runs the PHP code inside, then continues rendering the rest of the template.
Execution Sample
Laravel
@php
  $greeting = 'Hello, world!';
@endphp
<p>{{ $greeting }}</p>
This code runs raw PHP inside Blade to set a variable, then outputs it in HTML.
Execution Table
StepActionPHP Code ExecutedVariable StateOutput Produced
1Enter @php blockNo PHP code executed yet$greeting = undefinedNo output
2Execute PHP code$greeting = 'Hello, world!';$greeting = 'Hello, world!'No output
3Exit @php blockEnd of PHP block$greeting = 'Hello, world!'No output
4Render <p> tag with Blade echoEcho $greeting$greeting = 'Hello, world!'<p>Hello, world!</p>
5Finish templateNo PHP$greeting = 'Hello, world!'Full HTML output
💡 Template rendering completes after processing @php block and outputting variable.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$greetingundefinedundefined'Hello, world!''Hello, world!''Hello, world!'
Key Moments - 2 Insights
Why doesn't the @php block directly output anything?
Because @php runs raw PHP code without echoing; output happens only when Blade echoes variables like {{ $greeting }} as shown in step 4.
Can variables set inside @php be used later in the template?
Yes, variables set inside @php persist and can be used anywhere later in the Blade template, as seen with $greeting in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $greeting after step 2?
Aundefined
B'Hello, world!'
Cnull
DEmpty string
💡 Hint
Check the 'Variable State' column at step 2 in the execution table.
At which step does the template produce visible HTML output?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Output Produced' column to find when HTML appears.
If you remove the @endphp tag, what happens to the execution flow?
APHP code runs normally
BVariables are still set but output is empty
CBlade throws a syntax error and stops rendering
DTemplate skips the PHP code
💡 Hint
Blade requires @endphp to close the PHP block; missing it breaks the template parsing.
Concept Snapshot
@php ... @endphp lets you run raw PHP inside Blade templates.
Variables set inside @php persist for later use.
No output happens inside @php unless you echo explicitly.
Use {{ }} to output variables safely.
Always close @php blocks with @endphp.
Full Transcript
In Laravel Blade templates, the @php directive allows you to write raw PHP code inside your template. When the Blade engine encounters @php, it executes the PHP code inside the block without producing output unless you explicitly echo something. Variables defined inside this block remain available for the rest of the template. After the @php block ends with @endphp, Blade continues rendering the template, and you can output variables using the {{ }} syntax. This flow ensures you can mix PHP logic and HTML cleanly. Remember to always close your @php blocks to avoid syntax errors.