-3

我不明白在函数中使用函数时排序是如何工作的。

function funcChild(){
    echo "Sparta";
}

function funcMain($panic){
    echo "This is $panic";
}



funcMain(funcChild());

它输出:

SpartaThis is 

我只想让它输出“这是斯巴达”。

4

5 回答 5

1

Then you need to return the value from the function so it can be passed to the next function, not echo it right there on the spot.

于 2013-04-02T12:17:00.660 回答
1

echo and return are not equivalent. When you echo something, it gets immediately printed, rather than stored anywhere. When your code executes, funcChild() executes first because funcMain() is trying to use it as a parameter. funcChild() echos "Sparta" to the screen, and then returns nothing for funcMain() to use. That means funcMain() runs with no input.

Change echo "Sparta" to return "Sparta"

function funcChild(){
    return "Sparta";
}

function funcMain($panic){
    echo "This is $panic";
}

funcMain(funcChild());
于 2013-04-02T12:17:28.210 回答
0
function funcChild(){
    return "Sparta";
}

function funcMain($panic){
    echo "This is $panic";
}



funcMain(funcChild());
于 2013-04-02T12:17:48.040 回答
0

这就像数学,由内而外开始,你必须生成一个返回而不是直接打印

function funcChild(){
    return "Sparta"; // return ... not direct print!
}

function funcMain($panic){
    echo "This is $panic";
}



funcMain(funcChild());
于 2013-04-02T12:18:54.563 回答
0

为什么不将您的代码更改为:

function funcMain($panic) {
  echo "This is $panic";
}

完成了吗?我看不出您有任何理由想要或需要做您正在尝试的事情。但是,您获得结果的原因是对 funcChild 的调用是作为 funcMain 的参数发生的。因此,funcChild 先执行,然后 funcMain 执行。

如果您绝对必须做您正在尝试的事情,那么 funcChild(funcMain()) 应该是如何做到的。

于 2013-04-02T12:22:09.470 回答