Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : Display a message using a function without using echo

Tags:

function

php

echo

Here'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.

like image 987
Dhruv Kumar Jha Avatar asked Mar 03 '26 00:03

Dhruv Kumar Jha


2 Answers

If that's the case, your function should have echo.

function hello($name) {
  $message = 'Hello '.$name.' How are you feeling today?';
  echo $message;
}
like image 151
Ruel Avatar answered Mar 04 '26 13:03

Ruel


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);
}
?>
like image 44
Lekensteyn Avatar answered Mar 04 '26 12:03

Lekensteyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!