echo语句中的逗号表示什么? [英] what does comma in echo statement signify?

查看:103
本文介绍了echo语句中的逗号表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从递归函数中回显字符串:

echo< li>,$ node,recurse($ arr),< / li> ;;



echo< li> 。 $ node。 recurse($ arr)。 < / li>;

I am trying to echo a string from a Recursive function:
echo "<li>", $node, recurse($arr), "</li>";
and
echo "<li>" . $node . recurse($arr) . "</li>";

function writeList($tree)
{
    if($tree == null) return;
    echo "<ul>";
    foreach($tree as $node=>$children) {
        echo "<li>", $node, writeList($children) , "</li>";
    }
    echo "</ul>";
}

$ tree 是一种树状结构,可以在此问题中找到( form2)

$tree is a tree-like structure, as can be found in this question (form2)

而且,我可以注意到两者的输出是不同的。

有人可以告诉我使用<$ c $的区别吗c>,和,尤其是在此示例中?

And, I can notice that the output from the two is different.
Can someone tell me the difference in using , and . in general, and particularly, in this example?

编辑:如果我不想将字符串回显,而是希望将从此函数生成的字符串存储在变量中,该怎么办?我对第一个 echo 语句收到的输出特别感兴趣。

EDIT: what if rather than echoing the strings, I want to store the string generated from this function in a variable. I am, particularly, interested in the output received from the first echo statement.

编辑:
我正在喂这个数组:

I am feeding this array:

array
  3 => 
    array
      4 => 
        array
          7 => null
          8 => 
            array
              9 => null
      5 => null
  6 => null

我得到的输出是:

(来自第一个echo语句)

The outputs I am getting is:
(from first echo statement)

<ul><li>3<ul><li>4<ul><li>7</li><li>8<ul><li>9</li></ul></li></ul></li><li>5</li></ul></li><li>6</li></ul>

(来自第二个回显语句)

(from second echo statement)

<ul><ul><ul><li>7</li><ul><li>9</li></ul><li>8</li></ul><li>4</li><li>5</li></ul><li>3</li><li>6</li></ul>


推荐答案

编辑:确定,我知道了。罪魁祸首是您的 writeList()函数。该函数内部有一个辅助 echo

OK, I get it. The culprit is your writeList() function. There is a secondary echo inside that function.

执行此操作时:

echo "<li>", $node, writeList($arr), "</li>";

首先评估每个部分,然后打印出来。等效于:

Each part is evaluated first and then printed out. It is equivalent to:

echo "<li>";
echo $node;
echo writeList($arr);
echo "</li>";

但是当您这样做时:

echo "<li>" . $node . writeList($arr) . "</li>";

整个字符串是使用串联运算符构造的。首先,然后打印出来。这意味着在构造字符串期间,首先调用 writeList($ arr),然后在外部 echo 被调用。

The entire string is constructed using the concatenation operators . first, then printed out. This means writeList($arr) is called first during the construction of the string, then the outer echo is called.

为避免此问题,请勿在函数调用中回显任何内容。使用串联运算符构建字符串,然后返回它们,以便外部 echo 可以打印它们。

To avoid this problem, don't echo anything within your function calls. Build strings using the concatenation operator and then return them so that your outer echo can print them.


如果不是要回显字符串,我想将从此函数生成的字符串存储在变量中。我尤其对从第一个echo语句收到的输出感兴趣。

what if rather than echoing the strings, I want to store the string generated from this function in a variable. I am, particularly, interested in the output received from the first echo statement.

使用输出缓冲。

ob_start();
echo "<li>", $node, writeList($arr), "</li>";
$out = ob_get_clean();

但是对于该特定语句,为什么不直接连接呢?

But for that particular statement, why not just concatenate instead?

$out = "<li>" . $node . writeList($arr) . "</li>";

这篇关于echo语句中的逗号表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆