2

I'm looking for something much like the Using PHP variables inside HTML tags? question, but a little different.

In my case, I'd like to use code ore like this:

$somevar = 'a test';

include("file.html");

and file.html would contain

<b>hello, this is {$somevar}</b>

The problem is that it just prints hello, this is {$somevar}.

How can I make the HTML read the vars in the included file?

4
  • You need to echo it <b>hello, this is <?php echo $somevar; ?></b> Commented Nov 14, 2012 at 21:47
  • anything that's not within <?php ?> tag sets, or passed through eval() (bad idea, don't do it), is not run through the php parser. Commented Nov 14, 2012 at 21:47
  • 1
    @relentless You got it a bit backwards, see SamuelCook's answer Commented Nov 14, 2012 at 21:48
  • 1
    @Izkata - Yup, you're right. I wasn't paying enough attention. Edited. Commented Nov 14, 2012 at 21:52

3 Answers 3

1
echo "<b>hello, this is {$somevar}</b>";

or

<b>hello, this is <?=$somevar?></b>

or

<b>hello, this is <?php echo $somevar; ?></b>

or

<b>hello, this is <?php print $somevar; ?></b>
Sign up to request clarification or add additional context in comments.

7 Comments

im not sure. someone must not like it :/
someone must not like me very much.
Worth noting like @paquettg says, you have to change file.html to file.php
You don't need to change file extensions.
@nickb under default apache settings, you do
|
0

You need to include the variable defining program, in the other program wanting to access it. Example:

   Say test.html has $somevar.
   in file.html you do,

   <?php
   include('test.html');
    echo "<b>hello, this is $somevar</b>"; 
   ?>

Comments

0
<?php
include "stuff.php";
$somevar = "test";
?>

<html>
    <body><p><?php echo($somevar); ?></p></body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.