Reputation: 2301
Is it possible to declare a variable value like or something similar:
<?php
$var =
?>
<!-- block of html code that goes into $var -->
<?php
//do anything with $var that contains the html code of above
?>
Upvotes: 0
Views: 49
Reputation: 4137
No this is not possible.
You can declare a variable in a php
-block, then have that block terminate then some html
part and then another php
block and use it there.
But you cannot declare a variable like this, assigning the subsequent html contents to it.
But what you can do, as pointed out in Anders J's answer you can do the opposite, assign preceding contents to a variable. But note that there are some possible pitfalls.
Php is parsed by identifying the <?php
?>
block first, everything outside of that is ignored by the parser, so in order to do this you would better have that html content in a separate file and read it into your variable using something like file_get_contents
.
Upvotes: 0
Reputation: 161
<?php
ob_start();
?>
<!-- block of html code that goes into $var -->
<?php
$var = ob_get_contents();
while (@ob_end_flush());
Note this assumes you are not already using output buffering.
Upvotes: 3