开发者

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 va开发者_运维问答riable, 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?


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.


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

It's called heredoc syntax.


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


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.


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


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


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜