HTML into PHP Variable (HTML outside PHP code)
Asked Answered
D

7

29

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.

Defalcate answered 17/10, 2009 at 6:39 Comment(2)
What are you trying to accomplish?Vernation
I am trying to put html code into a php variableDefalcate
S
78

Have you tried "output buffering"?

<?php
 ...
 ob_start();
?>

<html>
   <head>...</head>
   <body>...<?php echo $another_variable ?></body>
</html>

<?php
 $variable = ob_get_clean();
 ...
?>
Sarson answered 17/10, 2009 at 6:50 Comment(2)
One more question =) Is it possible to put other php variable inside of it? <html> <head>...</head> <body>$content</body> </html>Defalcate
Would be nice to see support for this in templating languagues.Irreformable
C
15

I think you want heredoc syntax.

For example:

$var = <<<HTML
<html>
   <head>
random crap here
</html>
HTML;
Cascio answered 17/10, 2009 at 6:52 Comment(0)
J
4

I'm not really sure about what you are trying to accomplish, but I think something like the heredoc syntax might be useful for you:

<?
$variable = <<< MYSTRING

<html>
   <head>...</head>
   <body>...</body>
</html>

MYSTRING;

However if you are trying to make HTML templates I would highly recommend you to get a real templating engine, like Smarty, Dwoo or Savant.

Jorin answered 17/10, 2009 at 6:51 Comment(0)
I
1

Ok what you want to do is possible in a fashion.

You cannot simply assign a block of HTML to a php variable or do so with a function. However there is a number of ways to get the result you wish.

  1. Investigate the use of a templating engine (I suggest you do this as it is worth while anyway). I use smarty, but there are many others
  2. The second is to use an output buffer.

One of the problems you have is that any HTML you have in your page is immediately sent to the client which means it cant be used as a variable in php. However if you use the functions ob_start and ob_end_fush you can achive what you want.

eg

<?php 
  somesetupcode();
  ob_start();  ?>
<html>
<body>
html text
</body>
</html>
<?php
  //This will assign everything that has been output since call to ob_start to your    variable.
  $myHTML = ob_get_contents() ;
  ob_end_flush();

?>

Hope this helps you can read up on output buffers in php docs.

Inhale answered 17/10, 2009 at 6:58 Comment(0)
H
1

I always recommend to AVOID buffer functions (like ob_start ,or etc) whenever you have an alternative (because sometimes they might conflict with parts in same system).

I use:

function Show_My_Html()
{ ?> 
    <html>
      <head></head>
      <body>
         ...
      </body>
    </html>
    <?php 
}


...
//then you can output anywhere
Show_My_Html();
Honkytonk answered 13/6, 2015 at 17:18 Comment(0)
Z
-1
$html_content = '
    <p class="yourcssclass">Your HTML Code inside apostraphes</p>
';
echo $html_content;
Zymogen answered 23/10, 2018 at 12:5 Comment(0)
D
-10

Its REALLY CRAZY but be aware that if you do it :

<?php echo ""; ?>  

You will get it:

<html><head></head><body></body></html>  

Keep calm, its only php trying turn you crazy.

Dyanna answered 30/8, 2013 at 22:57 Comment(2)
It's never a correct answer. You probably viewed a dom source in a browser for an empty page and were amazed.Longsuffering
That's the browser generating elements which are defined in the HTML specification as having optional start and end tags. It has nothing to do with PHP.Highflown

© 2022 - 2024 — McMap. All rights reserved.