Following on along a similar line to How can I echo HTML in PHP? by Jarvis.
The reason I am re-raising this is to focus on the case where PHP is to be output by a function - some of the answers originally given did not seem applicable for this case.
I am re-writing my fairly largish DB driven site. It seems a good time to re-evaluate choices I made in 2005 regarding the output of HTML from a PHP routine.
Currently I have followed the route of...
function write_my_name( $name )
{
echo "<h1>Hello your name is $name</h1>";
echo "<h2>Alternatively we could code it like this</h2>" . $name;
}
It tends to get a bit messy escaping quotes but I am used to it and in many ways it has served me just fine.
Now I am playing around with design templates a lot more with the consequence that I just want to cut and paste a block of HTML from a web based design tool and slap it into the middle of my PHP code - I want to just change a few hard wired bits of text with PHP generated values and then run the code and see what it looks like. This gives me the opportunity to rapidly evaluate different layouts with the very minimal adjustment of going around replacing hard-html-coded strings with PHP generated values
<?PHP echo $somevar; ?>
This is working fine with straight forward scripts I simply end up with
<?PHP LOTS OF CODE ?>
<H3><?PHP echo $titleforh3tag ?> </H3>
<!-- HTML CUT AND PASTED HERE TRYING OUT DIFFERENT LAYOUTS-->
<?PHP LOTS OF CODE ?>
Speculatively I tried this approach in a routine rather than a straight PHP script although I suspected it would not work correctly
<?PHP function fred( $arg1 )
LOTS OF PHP CODE
?>
<H3><?PHP echo $titleforh3tag ?> </H3>
<!-- HTML CUT AND PASED HERE TRYING OUT DIFFERENT LAYOUTS-->
<PHP?
return;
?>
Note that function fred is split by PHP open/close tags and sandwiches raw html with PHP generated strings in places.
The error I receive is
Parse error: syntax error, unexpected $end in example.php on line XXX
My PHP open/close tags are balanced so I am assuming that the error is confirming my suspicion that this technique is compatible with straight forward PHP scripts but not code within a function.
Any views on best method to easily cut paste HTML into the middle of a PHP function avoiding writing lots of echo statements, at least until I have settled on which of the many choices I am trying out.
I am using "Ultimate Blocks" as my layout tool, it sits on top of bootstrap - I can try out as many ideas as I want, copy the code with a few clicks and dump it into the middle of my PHP scripts, a little wiring and within minutes I am looking at the live result. This is why I would rather avoid having to echo each line of HTML.