Last active
November 6, 2024 19:26
-
-
Save Pephers/7910835 to your computer and use it in GitHub Desktop.
How to call a PHP function inside a Heredoc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cb = function ($fn) { | |
return $fn; | |
}; | |
echo <<<HEREDOC | |
Hello, {$cb(ucfirst('world'))} | |
HEREDOC; |
// ;)
$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;
})
....
Also, there are a lot of options:
https://stackoverflow.com/questions/104516/calling-php-functions-within-heredoc-strings/36202673#36202673
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is brilliant! I've used more cumbersome variations of that for a long time, but you have made it really neat.