PHP:使用函数显示消息而不使用回显

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4232017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:21:06  来源:igfitidea点击:

PHP : Display a message using a function without using echo

phpfunctionecho

提问by Dhruv Kumar Jha

Here's a simple php function.

这是一个简单的php函数。

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.

喜欢<?php hello(Stackoverflow); ?>它应该返回消息。

回答by Ruel

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

如果是这种情况,您的函数应该具有echo.

function hello($name) {
  $message = 'Hello '.$name.' How are you feeling today?';
  echo $message;
}

回答by Lekensteyn

There are many ways to display something. printfis very useful in this case.

有很多方法可以展示一些东西。printf在这种情况下非常有用。

<?php
function hello($name){
   printf('Hello %s How are you feeling today?', $name);
}
?>

回答by Atul Kasliwal

function hello($name) {
  $message = 'Hello '.$name.' How are you feeling today?';
  echo $message;
}

回答by Napas

If short tags is on <?= hello('Stackoverflow') ?>should work. But it's not recomended to use it.

如果短标签打开<?= hello('Stackoverflow') ?>应该工作。但是不推荐使用。

回答by Ran Bar-Zik

You can always print it to the PHP error log: http://php.net/manual/en/function.error-log.php

您可以随时将其打印到 PHP 错误日志:http: //php.net/manual/en/function.error-log.php

回答by PrashantAdesara

<?= hello('String') ?>