adrianTNT
adrianTNT

Reputation: 4082

Defining html code inside PHP variables

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?

Upvotes: 28

Views: 111533

Answers (8)

Saurabh
Saurabh

Reputation: 462

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>

Upvotes: -1

upsDuck
upsDuck

Reputation: 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

Upvotes: -1

S R Panda
S R Panda

Reputation: 19

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();

Upvotes: 1

RedSparr0w
RedSparr0w

Reputation: 468

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();
?>

Upvotes: 5

Mick Houtveen
Mick Houtveen

Reputation: 31

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.

Upvotes: 3

bart
bart

Reputation: 15288

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');

Upvotes: 11

Irfanullah Jan
Irfanullah Jan

Reputation: 3892

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.

Upvotes: 113

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91892

<?php
$my_var = <<<EOD
<div>SOME HTML</div>
EOD;
?>

It's called heredoc syntax.

Upvotes: 56

Related Questions