-1

I have a php file with the following code:

echo($x);

Which simply prints

5.0 

How would I display this value in a html file?

timothyylim
  • 1,399
  • 2
  • 14
  • 32

1 Answers1

2

You can use any HTML manipulation APIs, like DOMDocument class. But, just to get you started, I assume you have a very simple HTML file:

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

And you want to put the value of the $x variable inside the <body> tag using a <div> tag; simply:

<?php
$dom = new DOMDocument;
$HTML_file = "paht/to/your/file.html";
$dom->loadHTMLFile($HTML_file);

// To get the <body> tag in the HTML file
$xpath = new DOMXPath($dom);    
$body = $xpath->query('//body')->item(0);

// Insert some tags inside the <body> tag
$x = 5;
$element = $dom->createElement('div', $x);
$body->appendChild($element);

// overwrites to the same file
$dom->saveHTMLFile($HTML_file);
?>
someOne
  • 1,975
  • 2
  • 14
  • 20
  • I am not sure if I am binding my html file properly as nothing shows up. Could you give me some tips on how to debug? Thanks so much in advance. – timothyylim Sep 24 '15 at 06:15
  • Also, should I change '//body' to something else? – timothyylim Sep 24 '15 at 06:16
  • @ttct Nothing is supposed to be showed, The script modifies the example HTML file; After the script execution, the sample file should be modified having a `
    ` inside the `` tag which has the content of the `$x` variable. Also, the `'//body'` is just an [XPath](http://www.w3schools.com/xsl/xpath_syntax.asp) expression to select the `` tag in the provided example; The code is just a simple example to get you started :)
    – someOne Sep 24 '15 at 06:33
  • Great, thanks a lot! – timothyylim Sep 24 '15 at 06:41
  • @ttct If it helps, please consider [accepting it as an answer](http://stackoverflow.com/help/someone-answers) :) – someOne Sep 24 '15 at 06:48
  • Yes, I did earlier but I don't think it was registered! Thanks again – timothyylim Sep 24 '15 at 16:17