Possible to treat html that is directly outputted as a php string
I would simply like to know if something similar to this is possible in php somehow:
<?php
$myhtmlstring = "
?>
<table>
<tr>
<td>test</td>
</tr>
</tabl开发者_JAVA技巧e>
<?php
";
?>
The reason for this is I would like to be able to write the html in this nice looking format but have php trim the white space after the fact.
You can use heredoc.
You can use the alternative heredoc syntax:
$myhtmlstring = <<<EOT
<table>...</table>
EOT;
Or you can use output buffering:
<?php
ob_start();
?>
<table>...</table>
<?php
$myhtmlstring = ob_get_clean();
?>
Yes
<?php
$myhtmlstring = '
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
';
// Do what you want with the HTML in a PHP variable
// Echo the HTML from the PHP variable to make the webpage
echo $myhtmlstring;
?>
I usually use the buffer functions, like so:
<?php
$whatever = "Hey man";
// This starts the buffer, so output will no longer be written.
ob_start();
?>
<html>
<head>
<title><?php echo $whatever ?></title>
</head>
<body>
<h1><?php echo $whatever ?></h1>
<p>I like this in part because you can use variables.</p>
</body>
</html>
<?php
// Here's the magic part!
$myhtmlstring = ob_get_clean();
?>
For more information about the buffer functions, look up ob_start()
on
php.net.
do you mean so?
<?php
$string = '<table border="1">
<tr>
<td> test </td>
</tr>
</table>';
echo $string;
?>
精彩评论