Php 如何将XML作为简单字符串回显?

Php 如何将XML作为简单字符串回显?,php,echo,Php,Echo,我无法理解代码的行为: 输入: <?php function polldaddy_choices($choices) { foreach ($choices as $choice) { $answer = "<pd:answer> <pd:text>" . $choice . "</pd:text> </pd:answer>";

我无法理解代码的行为:

输入:

<?php
    function polldaddy_choices($choices) {
      foreach ($choices as $choice) {
        $answer = "<pd:answer>
                   <pd:text>" . $choice . "</pd:text>
                   </pd:answer>";
        echo $answer; 
     }
  }
  $total_choices = array('yes' , 'no' , 'do not know');
  $ans = polldaddy_choices($total_choices); 
  $xml = "world" . $ans . "hello" ;
  echo $xml;
?>

输出:

  <pd:answer>
      <pd:text></pd:text>
      </pd:answer><pd:answer>
      <pd:text></pd:text>
      </pd:answer><pd:answer>
      <pd:text></pd:text>
      </pd:answer>worldhello

世界你好
为什么字符串会出现在输出的末尾


这里是代码板上的链接:

您的函数没有重新调整任何内容。你在那个函数中直接回音

因此,首先调用
pollddaddy\u choices
,它响应html。然后,你回应:

$xml = "world" . "" . "hello" ;

您的函数没有重新调整任何内容。你在那个函数中直接回音

因此,首先调用
pollddaddy\u choices
,它响应html。然后,你回应:

$xml = "world" . "" . "hello" ;

因为您正在
echo
polldady\u选项
函数中输入输出。因此,以下是:

$ans=pollday\u选项($total\u选项)
实际上正在打印XML,并且:

$xml=“世界”$安。“你好”
将只打印worldhello,作为
$ans===null

我想你可能想做一些更像:

function polldaddy_choices($choices) {
    $answers = array();
    foreach ($choices as $choice) {
        $answer = "<pd:answer>
                   <pd:text>" . $choice . "</pd:text>
                   </pd:answer>";
        $answers[] = $answer;
    }

 return implode("\n", $answers);
}
函数选项($choices){
$answers=array();
foreach($choice作为$choice){
$answer=”
“$choice。”
";
$answers[]=$answer;
}
返回内爆(“\n”,$answers);
}

因为您正在
回送
您的
轮询选项
函数中的输出。因此,以下是:

$ans=pollday\u选项($total\u选项)
实际上正在打印XML,并且:

$xml=“世界”$安。“你好”
将只打印worldhello,作为
$ans===null

我想你可能想做一些更像:

function polldaddy_choices($choices) {
    $answers = array();
    foreach ($choices as $choice) {
        $answer = "<pd:answer>
                   <pd:text>" . $choice . "</pd:text>
                   </pd:answer>";
        $answers[] = $answer;
    }

 return implode("\n", $answers);
}
函数选项($choices){
$answers=array();
foreach($choice作为$choice){
$answer=”
“$choice。”
";
$answers[]=$answer;
}
返回内爆(“\n”,$answers);
}

您的函数直接响应xml代码。在下面的代码中,您将看到我创建了一个变量($answer=“”;),然后使用“.”将xml追加到变量的末尾。在函数的末尾,我返回$answer的值

然后调用函数($ans=polldaddy_choices($total_choices);)时,它会将函数的返回值放入$ans变量中

<?php
function polldaddy_choices($choices) {
  $answer = "";
  foreach ($choices as $choice) {
    $answer.= "<pd:answer>
               <pd:text>" . $choice . "</pd:text>
               </pd:answer>";
 }
 return $answer;
}
$total_choices = array('yes' , 'no' , 'do not know');
$ans = polldaddy_choices($total_choices); 
$xml = "world" . $ans . "hello" ;
echo $xml;
?>

您的函数直接响应xml代码。在下面的代码中,您将看到我创建了一个变量($answer=“”;),然后使用“.”将xml追加到变量的末尾。在函数的末尾,我返回$answer的值

然后调用函数($ans=polldaddy_choices($total_choices);)时,它会将函数的返回值放入$ans变量中

<?php
function polldaddy_choices($choices) {
  $answer = "";
  foreach ($choices as $choice) {
    $answer.= "<pd:answer>
               <pd:text>" . $choice . "</pd:text>
               </pd:answer>";
 }
 return $answer;
}
$total_choices = array('yes' , 'no' , 'do not know');
$ans = polldaddy_choices($total_choices); 
$xml = "world" . $ans . "hello" ;
echo $xml;
?>


是否有一行或语句您不明白?为什么字符串“hello”和“world”会出现在输出的末尾?我已经把它放在了开头和结尾。有一行或一句话你不明白吗?为什么字符串“hello”和“world”会出现在输出的末尾?我把它放在开头和结尾。严格地说,
“世界”。无效的“hello”
,但是当强制为字符串时,
NULL
将被视为
,因此这是一个细微的区别。严格来说,
“world”。无效的“hello”
,但是当强制为字符串时,
NULL
将被视为
,因此这是一个细微的区别。因此,当我尝试返回$answer而不是echo时,然后我得到的输出是:world不知道hello,但我需要重复与数组中字符串数量相同的结果,这就是为什么我尝试回显的原因,所以当我尝试返回$answer而不是echo时,然后我得到的输出是:world不知道hello,但我需要这个来重复数组中字符串的数量,这就是为什么我要尝试回显我需要确切的输出是world是否不知道hello,但上面代码没有返回任何内容,我使用echo而不是return的原因是return给出的otput为:world yes hello您是否尝试运行我在注释中放置的代码?确切的输出是:世界是不是不知道你好。为什么它会返回世界是的你好,因为return命令位于foreach循环内,因此在处理数组的第一个元素后退出函数。我需要确切的输出,如world yes no don know hello,但上面的代码没有返回任何内容,我之所以使用echo而不是return,是因为return的otput为:world yes hello您是否尝试运行我在注释中放置的代码?确切的输出是:世界是不是不知道你好。它返回world yes hello的原因是return命令位于foreach循环内,因此在处理数组的第一个元素后退出函数。