title: Error in loop with DOM PHP
body: <p>I'm having two errors, can't get this to work...</p>
  
  <p>With the code like this:</p>
  
  <pre><code>&lt;html&gt;
      &lt;body&gt;
          &lt;?php
          /*
           Fontes:
           - http://blog.clares.com.br/gerando-xml-com-php/
           - https://forum.imasters.com.br/topic/482006-resolvido%C2%A0gerar-nomes-aleat%C3%B3rios/
           */
          header('Content-type: text/xml; charset=utf-8');
  
          if(empty($_POST["Pergunta"])) {
              $pergunta = "";
              echo "dar um alert e voltar";
          } else {
              $pergunta = $_POST["Pergunta"];
          }
          if(empty($_POST["Resposta"])) {
              $resposta = "";
              echo "dar um alert e voltar";
          } else {
              $resposta = $_POST["Resposta"];
              $teste = explode("\n",$resposta);
              echo '&lt;pre&gt;' . print_r($teste) . '&lt;/pre&gt;';
          }
          /* CHECKBOX
           if(isset($_POST['MostrarAIML'])){
           //echo "checkbox marcado! &lt;br/&gt;";
           //echo "valor: " . $_POST['MostrarAIML'];
           $MostrarAIML = true;
           }else{
           //echo "checkbox nÃ£o marcado! &lt;br/&gt;";
           $MostrarAIML = false;
           }
  
           if(isset($_POST['SalvarAIML'])){
           //echo "checkbox marcado! &lt;br/&gt;";
           //echo "valor: " . $_POST['SalvarAIML'];
           $SalvarAIML = true;
           }else{
           //echo "checkbox nÃ£o marcado! &lt;br/&gt;";
           $SalvarAIML = false;
           } */
          if(empty($_POST["forma"])) {
              $escolhe = "MostrarAIML";
          } else if($_POST["forma"]=='MostrarAIML'){
              $escolhe = "MostrarAIML";
          } else if($_POST["forma"]=='SalvarAIML'){
              $escolhe = "SalvarAIML";
          }
  
          function nome_aleatorio(){
              $tamanho = mt_rand(5,9);
              $all_str = "abcdefghijlkmnopqrstuvxyzwABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
              $nome = "";
              for ($i = 0;$i &lt;= $tamanho;$i++){
                  $nome .= $all_str[mt_rand(0,61)];
              }
              return $nome;
          }
  
  
          # versao do encoding xml
          $dom = new DOMDocument("1.0", "UTF-8");
  
          # retirar os espacos em branco
          $dom-&gt;preserveWhiteSpace = false;
  
          # gerar o codigo
          $dom-&gt;formatOutput = true;
  
          # criando o nÃ³ principal (root)
          $root = $dom-&gt;createElement("aiml");
  
          $comentario = $dom-&gt;createComment("Copyright Â©2017 - FAST.aiml
           URL: http://avatar.cinted.ufrgs.br/alunos/fabricio/fastaiml/
           Powered by Fabricio Herpich &lt; fabricio_herpich at hotmail.com &gt;
           Project coordinator: Liane Margarida Rockenbach Tarouco &lt; liane at penta.ufrgs.br &gt;");
  
          # nÃ³ filho (category)
          $category = $dom-&gt;createElement("category");
          # nÃ³ filho (pattern) / #setanto a pergunta
          $pattern = $dom-&gt;createElement("pattern", $pergunta);
          # nÃ³ filho (template) / #setanto a resposta
          $template = array();
  
          for($i = 0; $i &lt; sizeof($teste); $i++){
              $template[$i] = $dom-&gt;createElement("template", $teste[$i]);
          }
  
          # adiciona os nÃ³s em aiml
          $category-&gt;appendChild($comentario);
          $category-&gt;appendChild($pattern);
  
          for($i = 0; $i &lt; sizeof($template); $i++){
              $category-&gt;appendChild($template[$i]);
          }
  
          # adiciona o nÃ³ contato em (root) agenda
          $root-&gt;appendChild($category);
          $dom-&gt;appendChild($root);
  
  
  
          # busco um nome aleatÃ³rio
          $nome_arquivo = nome_aleatorio();
  
          $dom-&gt;save("backup__/" . $nome_arquivo . ".xml");
  
  
  
  
          if($escolhe == "MostrarAIML"){
              # imprime o xml na tela
              print $dom-&gt;saveXML();
  
          }
  
          if($escolhe == "SalvarAIML"){
              # imprime o xml na tela
              print $dom-&gt;saveXML();
              # salva o arquivo AIML.xml
              header("Content-Disposition: attachment; filename=fastaiml_" . $nome_arquivo .  ".xml");
          }
  
          ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I'm getting this error:</p>
  
  <blockquote>
    <p>This page contains the following errors:
    error on line 12 at column 18: XML declaration allowed only at the start of the document
    Below is a rendering of the page up to the first error.**</p>
  </blockquote>
  
  <p>I have other php file that works perfectly, where the header() function is after the $nome_arquivo = nome_aleatorio();
  But, In this case I don't have the for loops on the code, 'cause I only have one  for the file...and in this case I have to put multiple  inside the  tag...</p>
  
  <p>If I put the header() like the other file, I get:</p>
  
  <blockquote>
    <p>This page contains the following errors:
    error on line 7 at column 18: XML declaration allowed only at the start of the document
    Below is a rendering of the page up to the first error.**</p>
  </blockquote>
  
  <p>how to work around this?</p>