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);


?>

My question relates to my previous question (http://stackoverflow.com/questions/13917256/php-why-cant-eom-contain-php-functions)

Given that $print is looped and contains many rows. How can I ensure the return statement loops my data. I must use return.

TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

4 Answers4

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

ennovativemedia
  • 361
  • 1
  • 7
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.

erenon
  • 18,838
  • 2
  • 61
  • 93
0

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

<?php

function show(){
    echo foo();
}

?>
webnoob
  • 15,747
  • 13
  • 83
  • 165
0

If you replace return with echo, there is no need to use array for that, as you have appended the content.

If you echo the content in the function itself, then there is no need to get the return value in another variable.

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();
poke
  • 369,085
  • 72
  • 557
  • 602
Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32