PHP - 返回和回显值的函数

I would like to create a function that both returns and echoes a value. But not the two together. Or return or echo. And I am looking for a beautiful way to do it. Almost all functions / class methods in my project must have this option.

I can pass to a function a bool value that indicates whether to echo or to return a value but it seems not so beautiful to me.

How, you think, it is better to implement this? Maybe a built-in option in PHP?

Passing a boolean value is a good way to go, print_r does the same:

mixed print_r ( mixed $expression [, bool $return = false ] )

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.