PHP函数命令仅限于一次使用吗?

时间:2012-12-19 12:29:46

标签: php arrays function loops reset

function getHeader($id){

    // VARS
    $print="";

    // SQL TO GET THE ORDER
    $mySQL=mysql_query("
    SELECT * FROM `table`
    ");

    // LOOP THE IDS 
    while($r=mysql_fetch_array($mySQL)){

    $print .=  '<p>'.$r["id"].'</p>';

    }
    return $print;
}

// MAIL FUNCTION
function mailToSend($Id){

        $getHeader = getHeader($Id);

$html = <<<EOM
        <!DOCTYPE html>
        <html lang="en">
        <head>
        </head>
        <body>
        $getHeader;
        </body>
        </html>
EOM;

}

mailToSend(46088);


?>

我的问题与我之前提出的问题有关(http://stackoverflow.com/questions/13917256/php-why-cant-eom-contain-php-functions)

鉴于$print是循环的并且包含许多行。如何确保ret​​urn语句循环我的数据。我必须使用return

4 个答案:

答案 0 :(得分:4)

您错误地使用了“foo”函数的结果。 你应该这样做..

function show(){
    $foo =  foo();
    echo $foo;
}

编辑:您也没有使用foo() - 函数传递任何变量,并且在声明中需要参数$ bar

答案 1 :(得分:2)

您了解functionsscope of execution的概念吗? 返回$print后,变量的值将分配给$foo,$ print将不再存在于外部范围内。你必须echo $foo

答案 2 :(得分:0)

我不确定我是否理解你但你不想要:

<?php

function show(){
    echo foo();
}

?>

答案 3 :(得分:0)

如果用echo替换return,则不需要使用数组,因为你已经添加了内容。

如果您回显函数本身的内容,则无需在另一个变量中获取返回值。

function foo($bar){

    // VARS
    $foo="Chocolate";
    $print="";

    // SQL TO GET THE ORDER
    $mySQL=mysql_query("
    SELECT * FROM `table`
    ");

    // LOOP THE IDS 
    while($r=mysql_fetch_array($mySQL)){

        $print .=  '<p>'.$r["id"].'</p>';

    }
    echo $print;
}


// call the function
// you dont need to have function show
foo();