• Undefined variable when using inline Livewire component and PHP heredoc

    TIL: Undefined variable when using inline Livewire component and PHP heredoc'

    I just started playing around with Livewire V3 and ran into an issue when making an inline component.

    When using a PHP heredoc there are actually two different syntaxes that have pretty drastically different behavior.

    <?php
    
    // Will throw undefined variable $string error.
    $variable = <<<BLADE
    This is evaluated the same as a PHP quoted {{ $string }}
    BLADE;
    
    $correct = <<<'BLADE'
    This is returned entirely as a {{ $string }}
    BLADE;
    

    #Heredoc vs Newdoc

    The first example is a "heredoc" while the second example is called a "newdoc".

    For Livewire components, we want to use the newdoc syntax as this is what is eventually passed to the Blade compiler.

    The docs for Livewire do infact show using the newdoc syntax, but since I hadn't used it much before, I was staring at my editor for a few minutes before I figured out what was going on!

    profile image of Daron Spence

    Daron Spence

    Hello world.

    More posts from Daron Spence