PHP : Display a message using a function without using echo
Here开发者_如何学C's a simple php function.
function hello($name) {
$message = 'Hello '.$name.' How are you feeling today?';
return $message;
}
And as you know, when I execute this function it returns a message.
<?php
echo hello(Stackoverflow);
?>
Output :
Hello Stackoverflow How are you feeling today?
Is there any way to display this message without using the echo (at least not here)
like <?php hello(Stackoverflow); ?>
and it should return the message.
If that's the case, your function should have echo
.
function hello($name) {
$message = 'Hello '.$name.' How are you feeling today?';
echo $message;
}
There are many ways to display something. printf
is very useful in this case.
<?php
function hello($name){
printf('Hello %s How are you feeling today?', $name);
}
?>
If short tags is on <?= hello('Stackoverflow') ?>
should work. But it's not recomended to use it.
function hello($name) {
$message = 'Hello '.$name.' How are you feeling today?';
echo $message;
}
You can always print it to the PHP error log: http://php.net/manual/en/function.error-log.php
<?= hello('String') ?>
精彩评论