0
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是循环的并且包含许多行。如何确保 return 语句循环我的数据。我必须使用return.

4

4 回答 4

4

You used the result of the function "foo" incorrectly. You should do it like this..

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

EDIT: you also didn't pass any variable with the foo() - function and in your declaration in does requires a parameter $bar

于 2012-12-19T12:31:59.463 回答
2

Do you understand the concept of functions and scope of execution? After returning $print, the value of the variable will be assigned to $foo, $print wouldn't exist any more in th outer scope. You have to echo $foo.

于 2012-12-19T12:33:25.387 回答
0

I am not sure I understand you correctly but don't you want:

<?php

function show(){
    echo foo();
}

?>
于 2012-12-19T12:32:25.590 回答
0

如果您将 return 替换为 echo,则无需为此使用数组,因为您已附加了内容。

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

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();
于 2012-12-19T12:34:39.053 回答