0

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.

Community
  • 1
  • 1
Jonnie
  • 1
  • 1
  • 2
    "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." — Your assumption is wrong and the code you provided doesn't demonstrate the problem (it has *other* problems, like you mixing up the position of `php` and `?`). – Quentin Oct 26 '16 at 10:08
  • i tried that its working. only one mistake is `` replace with `` – Karthi Oct 26 '16 at 10:13
  • `html` is perfectly well supported syntax. You merely have a syntax error somewhere in your code. – deceze Oct 26 '16 at 10:15
  • Thanks Quentin I just corrected those errors which were not in the code just the demonstration pseudo code. Your answer suggests no difference in the way functions are parsed? Content inside a file but outside of PHP tags gets output "as is" to the webpage when the file is included but similarly scoped content in a function gets output "as is" when the function is actually called? – Jonnie Oct 26 '16 at 10:20
  • Thanks LifeTimeProgrammer - yes sorry the mistakes were only in the pseudo code, what I have on the screen is a bit more involved. Thanks for the early responses - I better just go back and check. I am departing from very well worn tracks here. – Jonnie Oct 26 '16 at 10:23
  • Thanks for the help from everyone - perhaps if this is a naive question that will only take people's time someone might want to close or remove it. – Jonnie Oct 26 '16 at 10:23

0 Answers0