Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML into PHP Variable (HTML outside PHP code)

I am new to php and wondering if I can have something like this:

<?php  ...  magicFunctionStart(); ?>  <html>    <head>...</head>    <body>...</body> </html>  <?php  $variable = magicFunctionEnd();  ... ?> 

What I have to use right now is

<?php  ...  $variable = "<html><head>...</head><body>...</body></html>" ?> 

Which is annoying and not readable.

like image 957
Maksim Vi. Avatar asked Oct 17 '09 06:10

Maksim Vi.


People also ask

How can we store HTML page in PHP variable?

You can store the html in the variable and use it whenever you'd like. You're not forced to echo it until you need it... In order to store the html into the internal buffer with ob_start(); i need to generate and echo it.

How can I call HTML ID in PHP?

PHP cannot "call an HTML control". If you want to dynamically modify things in the browser, you'll have to do it client-side using Javascript. It is client-side, so you have to use Javascript, not PHP.

Can I echo HTML in PHP?

Using echo or print: PHP echo or print can be used to display HTML markup, javascript, text or variables.

How can I use HTML code in PHP?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.


1 Answers

Have you tried "output buffering"?

<?php  ...  ob_start(); ?>  <html>    <head>...</head>    <body>...<?php echo $another_variable ?></body> </html>  <?php  $variable = ob_get_clean();  ... ?> 
like image 121
Wabbitseason Avatar answered Oct 03 '22 15:10

Wabbitseason