Echo html code in php

Hello guys,staright to my problem. i have a html code as below:



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="graph/enhance.js" type="text/javascript"></script>
<script src="graph/excanvas.js" type="text/javascript"></script>
<script src="graph/jquery.min.js" type="text/javascript"></script>
<script src="graph/visualize.jQuery.js" type="text/javascript"></script>
<link href="graph/basic.css" rel="stylesheet" type="text/css" />
<link href="graph/visualize.css" rel="stylesheet" type="text/css" />
<link href="graph/visualize-light.css" rel="stylesheet" type="text/css" />
<style type="text/css">


.dwpeAd {
	color: #333;
	background-color: #F4F3Ea;
	position:fixed;
	right: 20px;
	top: 20px;
	padding: 5px;
}

.visualize {
	margin: 20px 0 0 30px;
}
		

</style>
<script type="text/xml">

<oa:widgets>
  <oa:widget wid="2281525" binding="#jQueryVisualizeChart" />
</oa:widgets>

</script>
</head>

<body>
<script type="text/javascript">

   	$(function(){
		 $('table').visualize({type: 'line', height: '300px', width: '420px', appendTitle : true, lineWeight : 8, colors : ['#b3c3f4','#39007a','#a4deab','#49a7c0','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']}).appendTo('#jQueryVisualizeChart').trigger('visualizeRefresh');

		});
	 	
</script>
<div id="jQueryVisualizeChart"></div>
<br />
<table>
  <caption>
    Student Information
  </caption>
  <thead>
    <tr>
      <td></td>
      <th scope="col">Attd</th>
      <th scope="col">Quiz1</th>
      <th scope="col">Quiz2</th>
      <th scope="col">Quiz3</th>
      <th scope="col">Quiz4</th>
      <th scope="col">Test1</th>
      <th scope="col">Test2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Mary</th>
      <td>190</td>
      <td>160</td>
      <td>40</td>
      <td>120</td>
      <td>30</td>
      <td>70</td>
      <td>70</td>
    </tr>

  </tbody>
</table>

</body>
</html>


i tried to echo it like this but it’s not working:



<?php
echo '
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="graph/enhance.js" type="text/javascript"></script>
<script src="graph/excanvas.js" type="text/javascript"></script>
<script src="graph/jquery.min.js" type="text/javascript"></script>
<script src="graph/visualize.jQuery.js" type="text/javascript"></script>
<link href="graph/basic.css" rel="stylesheet" type="text/css" />
<link href="graph/visualize.css" rel="stylesheet" type="text/css" />
<link href="graph/visualize-light.css" rel="stylesheet" type="text/css" />
<style type="text/css">


.dwpeAd {
	color: #333;
	background-color: #F4F3Ea;
	position:fixed;
	right: 20px;
	top: 20px;
	padding: 5px;
}

.visualize {
	margin: 20px 0 0 30px;
}
		

</style>
<script type="text/xml">

<oa:widgets>
  <oa:widget wid="2281525" binding="#jQueryVisualizeChart" />
</oa:widgets>

</script>
</head>

<body>
<script type="text/javascript">

   	$(function(){
		 $("table").visualize({type: "line", height: "300px", width: "420px", appendTitle : true, lineWeight : 8, colors : ["#b3c3f4","#39007a","#a4deab","#49a7c0","#8d10ee","#5a3b16","#26a4ed","#f45a90","#e9e744"]}).appendTo("#jQueryVisualizeChart").trigger("visualizeRefresh");

		});
	 	
</script>
<div id="jQueryVisualizeChart"></div>
<br />
<table>
  <caption>
    Student Information
  </caption>
  <thead>
    <tr>
      <td></td>
      <th scope="col">Attd</th>
      <th scope="col">Quiz1</th>
      <th scope="col">Quiz2</th>
      <th scope="col">Quiz3</th>
      <th scope="col">Quiz4</th>
      <th scope="col">Test1</th>
      <th scope="col">Test2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Mary</th>
      <td>190</td>
      <td>160</td>
      <td>40</td>
      <td>120</td>
      <td>30</td>
      <td>70</td>
      <td>70</td>
    </tr>

  </tbody>
</table>

</body>
</html>';

?>

any suggestion?

any suggestion?

Yup, don’t echo it :slight_smile:
You can also include arbitary HTML in a PHP file outside of the <?php ?> tags

for some purpose i need to echo it. any suggestion IF i want to echo it?

use HEREDOC or store it in an external file and read it from that file or include it in your script where needed.

$html <<< HTML
  // your HTML code here
HTML;
$html = file_get_contents('external.html');
include('external.html');

Hello cpradio, thanks for the suggestion, but i still want to echo it if possible instead of using include because i need to change the content of the table inside the html code from the database.

Then your best bet is either option 1 or 2, both give you a variable that you can later manipulate/echo

echo the html line by line. it will take time to code the whole html page, but at the long run it will make it easy for you to manipulate your variable.

On those occasions when I have really needed to echo HTML, I have found that the string will not work when it is encapsulated with single quotes, in which case, using double quotes for the string, I would either escape the HTML double quotes with slashes or else use single quotes. (Since I live in mortal terror of the mark-up gnomes, I take great pains to mollify the little devils with consistent use of double quotes in my HTML mark-up.) Taking the <head> section of your page as a for instance, I would do this:

$head = "<html xmlns=\\"http://www.w3.org/1999/xhtml\\">\
<head>\
<meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\" />\
<title>Untitled Document</title>\
<script src=\\"graph/enhance.js\\" type=\\"text/javascript\\"></script>\
<script src=\\"graph/excanvas.js\\" type=\\"text/javascript\\"></script>\
<script src=\\"graph/jquery.min.js\\" type=\\"text/javascript\\"></script>\
<script src=\\"graph/visualize.jQuery.js\\" type=\\"text/javascript\\"></script>\
<link href=\\"graph/basic.css\\" rel=\\"stylesheet\\" type=\\"text/css\\" />\
<link href=\\"graph/visualize.css\\" rel=\\"stylesheet\\" type=\\"text/css\\" />\
<link href=\\"graph/visualize-light.css\\" rel=\\"stylesheet\\" type=\\"text/css\\" />\
\
<style type=\\"text/css\\">\
.dwpeAd {color: #333; background-color: #F4F3Ea; position:fixed; right: 20px; top: 20px; padding: 5px;}\
.visualize {margin: 20px 0 0 30px;}\
</style>\
\
<script type=\\"text/xml\\">\
\
<oa:widgets>\
\	<oa:widget wid=\\"2281525\\" binding=\\"#jQueryVisualizeChart\\" />\
</oa:widgets>\
\
</script>\
</head>";

Then I would call that string in the template like this before proceeding to mark up the <body> section in the conventional manner:

<?php echo $head; ?>

Then I would ask myself why am I putting the templating engine to such trouble when, as others have suggested, HTML mark-up should be kept separate from PHP code where possible.