Reputation: 2302
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
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
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
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
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
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
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
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
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