younes0
younes0

Reputation: 2302

How do I "echo" a function results into another function?

I have this function :

function MakeInput($id, $class) {
    $value = substr($id, 2);
    global $$value;
    echo '<input type="text" id="'.$id.'" name="'.$id.'" class="'.$class.'" value="'.htmlentities($$value,ENT_COMPAT,'UTF-8').'" />';
}

Which do not return but echoes the results.

I'd like to use it as it is into another function:

function ItkMakeMo($NomDebVar) {
$Output = '
    <tr>    
        <td>Test</td>
    </tr>
    <tr>
        <td>'.MakeInput($NomDebVar.'moshj','IntInput').'</td>
    </tr>
';
echo $Output;

}

So this solution doesn't work, PHP sends the MakeInput output before the "echo $Output"

Upvotes: 0

Views: 7105

Answers (8)

Rob
Rob

Reputation: 1846

In your particular case, I'd try to echo the output right away, without storing it in a variable first.

function ItkMakeMo($NomDebVar) {
echo '
    <tr>    
        <td>Test</td>
    </tr>
    <tr>
        <td>';
MakeInput($NomDebVar.'moshj','IntInput').'</td>
echo '    </tr>
';
}

But as stated by others, it would definitely be best to change the function to return the value, rather than echoing it. Not just in this case, but as a general best practice. So only try this if you cannot change MakeInput yourself for some reason.

Upvotes: 1

Philip Ramirez
Philip Ramirez

Reputation: 2972

Since you said you would not like to modify the MakeInput function, use Output buffering.

function ItkMakeMo($NomDebVar) {
    ob_start();
    MakeInput($NomDebVar.'moshj','IntInput');
    $input = ob_get_clean();
    $Output = '
        <tr>    
            <td>Test</td>
        </tr>
        <tr>
            <td>'.$input.'</td>
        </tr>
    ';
    echo $Output;
}

If you don't need the $output variable but need MakeInput to echo, I'd follow what Rob suggested. With that said, follow what everyone else wrote because this is just an ugly way around poor programming.

Upvotes: 0

Aether
Aether

Reputation: 364

If for some reason you can't change MakeInput() or it would take more effort than it's worth to change it, then you can echo the output directly from ItkMakeMo():

function ItkMakeMo($NomDebVar) {
echo '
    <tr>    
        <td>Test</td>
    </tr>
    <tr>
        <td>';

MakeInput($NomDebVar.'moshj','IntInput');

echo '</td>
    </tr>
';
}

Then the MakeInput() output would be in the right place.

If possible, it's usually better not to directly output in functions and instead return the output as a string, then you can output it when you call the function.

Upvotes: 2

Stefan O.
Stefan O.

Reputation: 183

In the first function change echo with return:

function MakeInput($id, $class) {
    $value = substr($id, 2);
    global $$value;
    return '<input type="text" id="'.$id.'" name="'.$id.'" class="'.$class.'" value="'.htmlentities($$value,ENT_COMPAT,'UTF-8').'" />';
}

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382696

You should return the value:

function MakeInput($id, $class) {
    $value = substr($id, 2);
    global $$value;
    return '<input type="text" id="'.$id.'" name="'.$id.'" class="'.$class.'" value="'.htmlentities($$value,ENT_COMPAT,'UTF-8').'" />';
}

And later echo it.

Upvotes: 5

Grumpy
Grumpy

Reputation: 2243

function MakeInput($id, $class) {
    $value = substr($id, 2);
    global $$value;
    return '<input type="text" id="'.$id.'" name="'.$id.'" class="'.$class.'" value="'.htmlentities($$value,ENT_COMPAT,'UTF-8').'" />';
}

Upvotes: 0

Chinmayee G
Chinmayee G

Reputation: 8117

Try to return string in function MakeInput instead of echoing it,

function MakeInput($id, $class) {
    $value = substr($id, 2);
    global $$value;
    return '<input type="text" id="'.$id.'" name="'.$id.'" class="'.$class.'" value="'.htmlentities($$value,ENT_COMPAT,'UTF-8').'" />';
}

Upvotes: 1

My Other Me
My Other Me

Reputation: 5117

as opposed to using echo in your function, use return:

function MakeInput($id, $class) {
    $value = substr($id, 2);
    global $$value;
    return '<input type="text" id="'.$id.'" name="'.$id.'" class="'.$class.'" value="'.htmlentities($$value,ENT_COMPAT,'UTF-8').'" />';
}

Upvotes: 1

Related Questions