Skip to content

Instantly share code, notes, and snippets.

@Pephers
Last active September 26, 2023 17:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Pephers/7910835 to your computer and use it in GitHub Desktop.
Save Pephers/7910835 to your computer and use it in GitHub Desktop.
How to call a PHP function inside a Heredoc.
<?php
$cb = function ($fn) {
return $fn;
};
echo <<<HEREDOC
Hello, {$cb(ucfirst('world'))}
HEREDOC;
@counterpoint
Copy link

That is brilliant! I've used more cumbersome variations of that for a long time, but you have made it really neat.

@kodmanyagha
Copy link

kodmanyagha commented Mar 6, 2023

// ;)
$cb = fn($fn) => $fn;

echo <<<HEREDOC
Hello, {$cb(ucfirst('world'))}
HEREDOC;



// helper.js
function getCb() {
    return fn($fn) => $fn;
}

// Laravel - DataTablesExampleController.php
....
->addColumn('action', function ($data) {
    $call = getCb();

    $html = <<<HTML

<a href="panel/my-orders/$data->id#add_deposit_invoice" class="btn btn-primary btn-sm"><i class="fa-solid fa-receipt"></i>  {$call(__('app.datatables.add_deposit_invoice'))}</a>
<button class="btn btn-danger btn-sm"><i class="fa-solid fa-ban"></i>  {$call(__('app.datatables.cancel'))}</button>

HTML;

    return $html;
})
....

@lucionescu
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment