29

I want to include html code inside php variables. I remember there was a way to define html code inside a php variable, something similar to this:

<?php $my_var = { ?>
<div>SOME HTML</div>
<?php } ?>

I found it on PHP.net but I can't find it now. There was something else instead of the "{" but I don't remember exactly. I am looking to directly write the html code like above, so NOT like this: $my_var = '<div>SOME HTML</div>'; Any ideas?

8 Answers 8

113

Try this:

<?php ob_start(); ?>
<div>HTML goes here...</div>
<div>More HTML...</div>
<?php $my_var = ob_get_clean(); ?>

This way you will retain syntax highlighting for HTML.

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

2 Comments

This method seemed to work the best if you have additional php code inside the html.
Some references for or elaboration on the functions here would be useful.
57
<?php
$my_var = <<<EOD
<div>SOME HTML</div>
EOD;
?>

It's called heredoc syntax.

6 Comments

I was hoping that Dreamweaver will help with formatting the html syntax properly but I see it doesn't recognize it as a html :| I cannot close that php tag before html and open it again after the html, can I ?
No, unfortunately, you can't :-/ You can use ob_start() and ob_get_clean() to begin saving the output buffer, and then fetching the buffer into a variable. That's considered an (ugly) hack, though.
It is not considered ugly hack. Use that if it eases your work and makes you feel better. Nobody will notice that tiny speed difference.
Rok, I'm not talking about performance. I'm talking about software architecture. Using the output buffer to fetch data into variables is not a clean and maintainable approach.
ob_start() seems to be a very extreme solution in any case, and I can imagine all kind of bugs with page loads, scripts, etc.
|
11

Save your HTML in a separate file: example.html. Then read in the contents of the file into a variable

$my_var = file_get_contents('example.html');

1 Comment

Yes, this method would also work for the initial purpose I had.
5

If you are going to be echoing the content then another way this can be done is by using functions,

this also has the added benefit of programs using syntax highlighting.

function htmlContent(){
?>
    <h1>Html Content</h1>
<?php
}


htmlContent();
?>

1 Comment

IMO the most elegant approach, allowing to pass parameters and include php inside the element like this <?php MyElement("some text"); ?> , thanks for this tip =)
3

If you don't care about HTML syntax highlighting you could just define a normal variable. You don't have to escape HTML either because PHP lets you use single quotes to define a variable, meaning you can use double quotes within your HTML code.

$html = '
 <p>
  <b><i>Some html within a php variable</i><b>
 </p>
 <img src="path/to/some/boat.png" alt="This is an image of a boat">
' ;

Works perfectly for me, but it's a matter of preference and implementation I guess.

Comments

1

To define HTML code inside a PHP variable:

1) Use a function (Ref: @RedSparr0w)

<?php
function htmlContent(){
?>

    <h1>Html Content</h1>

<?php
}
?>

2. Store inside a variable

$var = htmlContent();

1 Comment

No idea why this is upvoted, that's not how you store an echoing into your variable. $var = htmlContent(); will just echo the HTML and assign NULL to $var
-1

To add to the few answers that mention using a function in the var. may I suggest an anonymous function. I'm not a php wizard so please correct me if I'm wrong, but I would imagine it working something like the following:

<?php
$my_var = function(){
   ?>
   <div>SOME HTML</div>
   <?php
};

// Display content
$my_var();

To extend this even further, if other php content is needed in the html it could be passed in a few different ways.

$method1 = "Title"; // Will be the same for every instance 
$method2 = "default content"; // Can change for each instance
$my_var = function($unique=$method2/*set default*/) use ($method1){
   ?>
   <h1><?php echo $prop1; ?></h1>
   <div><?php echo $unique; ?></div>
   <?php
};

// Display content

$my_var();
//returns

Title

default content
$my_var("Some unique content");
//returns

Title

Some unique content

Comments

-1

How can I use this array, in the tr, td section of html and assign this html content to a php variable $data.

$student = [
'saurabh' => 26,
'John' => 20,
'Ross' => 30 
];

<!DOCTYPE html>
<html>
<head>
<style>
table{
  border: 1px solid black;
}
.lft {
    padding: 0px 80px 10px 5px;
}
td {
    border-bottom: 2px solid black;
}
</style>
</head>
<body>
<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Saurabh</td>
    <td>26</td>
  </tr>
</table>
</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.