/ / Possible de traiter du code HTML directement généré sous forme de chaîne php - php, html

Possible de traiter du code HTML directement généré sous forme de chaîne php - php, html

Je voudrais simplement savoir si quelque chose de similaire à ceci est possible en php:

<?php
$myhtmlstring = "
?>
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
";
?>

La raison en est que j'aimerais pouvoir écrire le code HTML dans ce format agréable, mais que php coupe l'espace blanc après coup.

Réponses:

3 pour la réponse № 1

Vous pouvez utiliser la syntaxe alternative heredoc:

$myhtmlstring = <<<EOT
<table>...</table>
EOT;

Ou vous pouvez utiliser mise en mémoire tampon de sortie:

<?php
ob_start();
?>

<table>...</table>

<?php
$myhtmlstring = ob_get_clean();
?>

4 pour la réponse № 2

Vous pouvez utiliser Heredoc.


3 pour la réponse № 3

Oui

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

?>

1 pour la réponse № 4

J'utilise habituellement les fonctions de tampon, comme ceci:

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

?>

Pour plus d'informations sur les fonctions de tampon, recherchez ob_start() sur php.net.


0 pour la réponse № 5

tu veux dire ça?

<?php
$string = "<table border="1">
<tr>
<td> test </td>
</tr>
</table>";
echo $string;
?>