Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture PHP output into a variable?

Tags:

php

xml

I'm generating a ton of XML that is to be passed to an API as a post variable when a user click on a form button. I also want to be able to show the user the XML before hand.

The code is sorta like the following in structure:

<?php     $lots of = "php"; ?>  <xml>     <morexml>  <?php     while(){ ?>     <somegeneratedxml> <?php } ?>  <lastofthexml>  <?php ?>  <html>     <pre>       The XML for the user to preview     </pre>      <form>         <input id="xml" value="theXMLagain" />     </form> </html> 

My XML is being generated with a few while loops and stuff. It then needs to be shown in the two places (the preview and the form value).

My question is. How do I capture the generated XML in a variable or whatever so I only have to generate it once and then just print it out as apposed to generating it inside the preview and then again inside the form value?

like image 760
Binarytales Avatar asked Oct 05 '08 01:10

Binarytales


People also ask

How do I display the output of a PHP file?

With PHP, there are two basic ways to get output: echo and print . In this tutorial we use echo or print in almost every example. So, this chapter contains a little more info about those two output statements.

Where does PHP output go?

If it is turned on then stdout output will still go to the standard output of the console/browser but php://output will go to the buffer until the buffer reaches it's capacity or you manually flush the buffer.

Which function is used to output the result in PHP?

The basic functions for displaying output in PHP are as follows: Print() Function. Echo() Function. Printf() Function.


1 Answers

<?php ob_start(); ?> <xml/> <?php $xml = ob_get_clean(); ?> <input value="<?php echo $xml ?>" />͏͏͏͏͏͏ 
like image 159
moo Avatar answered Oct 25 '22 22:10

moo