All rules

Sheath rule

blade-valid-php-syntax

Raw PHP tags and @php blocks must compile into valid PHP.
Package
Core
Category
Blade
Default severity
error by default
Auto-fix
Manual fix

#Why

Blade copies the contents of @php blocks and raw PHP tags into the compiled view as written. Invalid PHP there is not caught until the view is rendered, and the parse error names the compiled file under storage/ rather than the template that contains it.

#Examples

#Bad

@php $value = ; @endphp
<?php
echo ;

#Good

@php
$value = make_value();
@endphp
@php if ($visible): @endphp
<p>Visible</p>
@php endif; @endphp

#Notes

  • The rule validates all raw PHP regions together, so PHP control structures may span multiple PHP regions.
  • Inline @php(...) arguments must also contain valid PHP. See blade-valid-directive-arguments.
  • The rule is enabled as an error in the recommended preset.