PHP configuration problem - <? instead of <?php

If that's the wrong way to do it would this be the right way to do it?

PHP:
<?
$bookname = 'Gone with the wind';
echo '<?xml version="1.0"?>
<Book>
<bookname>',$bookname,'</bookname>
</Book>';
?>

I'm only asking as I want to improve my coding practices. :)

If it we're me I would get the info from a db or flat file, whatever and loop an array echoing the opening tag, value, then closing tag.

I would always use the longer tag <?php as it won't mess with it
PHP:
<?xml version="1.0"?>
<book>

<?php


#Db stuff goes here
$bookname = array("Book 1","Book2","Book3");

foreach ($bookname as $name)
{
  
    echo "<bookname>$name</bookname>\n";

}

?>
</book>

To be honest I haven't used xml for ages, I believe there is also a xml class which you can implement too.
 
Last edited:
I guess the proper way would be to use an XML generating class and feed it results from a database query.

If you don't want to do that then maybe use a template for the main structure and then templates for any repeating blocks. Put tags in the template(s) where you want data to go and dynamically swap out the tags for real data as you loop. Save the whole thing into a variable as you go along and print/echo at the end.
 
Back
Top Bottom