strontkever
strontkever

Reputation: 29

Using php variables in included html

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?

Upvotes: 2

Views: 131

Answers (3)

ICoffeeConsumer
ICoffeeConsumer

Reputation: 912

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

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

Upvotes: 0

Teena Thomas
Teena Thomas

Reputation: 5239

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>"; 
   ?>

Upvotes: 0

Samuel Cook
Samuel Cook

Reputation: 16828

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>

Upvotes: 1

Related Questions