Note: the below is expected to work for PHP Version 7.x but does not work on PHP Version 5.6 (For 5.6, first assign the anonymous function to a variable)
/*
* Strings before & after
*/
$table_heading_text = "HEADING";
$table_bottom_text = "BOTTOM";
/*
* Use the function this way
*/
echo $table_heading_text . (function (array $array_of_strings)
{
$out = '';
foreach ($array_of_strings as $this_particular_string)
{
$out .= $this_particular_string;
}
return $out;
})(array(
"hi",
"mom"
)) . $table_bottom_text;
In short ...
- function must return some value that can be converted to text
- function definition must be enclosed in parenthesis
( ... )
- Don't forget to have calling arguments after the function definition
Examples:
echo "BEFORE" . (function ($x){return $x;})(" - MIDDLE - ") . "AFTER";
echo "BEFORE" . (function (){return " - MIDDLE - ";})() . "AFTER";
Also, using implode() may be better for this particular task.