0

I currently have a php which echo my html template.

However in that HTML template there is another echo which calls from another php script.

Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.

HTML TEMPLATE

<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>

CONTACT TEMPLATE

<php? $name = "hello world"; $email = "[email protected]"; ?>
7
  • 1
    Is this typo error <php? Commented Jul 4, 2017 at 6:49
  • PHP isn't executed recursively. <?php ... ?> code inside strings is not executed. Commented Jul 4, 2017 at 6:50
  • @Nawin Maybe it's part of the template system? Commented Jul 4, 2017 at 6:50
  • Thank you for you respond. Is there a way that I can make the string executed within then? Commented Jul 4, 2017 at 6:51
  • 1
    Switch to a proper template system like Twig(twig.sensiolabs.org), it's easy to do and more secure than a role your own. Commented Jul 4, 2017 at 6:53

4 Answers 4

2

I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.

Also, I must add that you are declaring php incorrectly.

This is preferred

<?php

not

<php?

So make sure for your contact template you use the correct tag.

Also to include a file you have to call it/require it.

Back to the original question - Here is your method

<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>

Here is the correct method

<?php 
 require('contact.php');
 $html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>'; 
 echo $html; 
?>

First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.

Hope this helps you on your way.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay let me phrase it another way. I have a contact.php which echo $name and $email. I have another template.php that echo $html $name and $email is within the $html
Have you tried updating your code to what I provided?
1

Try to use include. The include statement includes and evaluates the specified file, in this case - your template.

Comments

0

Just Concatenation

<? 
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>

2 Comments

Hi. It doesn't work. Do you do an include in my template php for the php then run this concat?
Do you use correct brackets <? ... ?> instead <php? ?> ?
0

Change the tags from <php? ?> to <?php ?> in your script

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.