1

For example, I have a page called 'include.php' or 'include.html', which contains the following lines of code:

<h1>Hi {$yourname}, this is a header.</h1>

Now, I want to include this page using PHP on another page, let's say on 'index.php' using:

<?
 $yourname = 'Bir';
 include 'include.php';
 ?>

When I include it, the page shows:

<h1>Hi {$yourname}, this is a header.</h1>

The variable $yourname is not replaced by it's value "Bir".

How do I solve this problem?

user2936213
  • 1,021
  • 1
  • 8
  • 19
user3382146
  • 59
  • 1
  • 5

5 Answers5

4

Need to write in include.php as

<h1>Hi <?php echo $yourname ;?>, this is a header.</h1>
Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23
1

Another simple way..

test1.php

<?php
$var="<h1>Hi $yourname, this is a header.</h1>"; //Removed {}

test2.php

<?php
$yourname="Jacob";
include_once('test1.php');
echo $var; //"prints" <h1>Hi Jacob, this is a header.</h1>
Mario Segura
  • 325
  • 1
  • 8
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

Check out http://us3.php.net/str_replace, and replace wherever you see {$yourname} with what ever the value really is.

Mario Segura
  • 325
  • 1
  • 8
1

if you want to avoid <?php ?> then you have to use code like this

 <?php echo "<h1>Hi ".$yourname." this is a header.</h1>";
Prafulla
  • 600
  • 7
  • 18
0

use this

<h1>Hi <?= $yourname ?>, this is a header.</h1>
Firman Hidayat
  • 415
  • 4
  • 13
  • 2
    I don't see any reason this should be downvoted. +1 from me because downvoter didn't write any comment. – Mike Mar 05 '14 at 06:24
  • @Mike, That's true. Maybe the downvoter hates short tags in PHP. – Shankar Narayana Damodaran Mar 05 '14 at 06:33
  • @ShankarDamodaran well they *are* discouraged even in the docs. Personally I don't use them or even recommend them, but meh. They are still part of the PHP code and until they are deprecated or removed, I don't see it as *that* big of a deal. But again, I don't agree with downvoting without leaving a comment. – Mike Mar 05 '14 at 07:41