【发布时间】:2015-10-01 06:46:48
【问题描述】:
我使用的一些函数在回显时返回一个字符串,类似这样。
echo my_function();
这会在屏幕上打印“hello”。
print_r( my_function() );
这会在屏幕上打印一个数组或一个对象。两种情况下的功能都是一样的,即使是可能的参数。
这是怎么做到的?
可能的功能
function my_function() {
// If this function is used by echo return the string
// If this function is used by print_r return an array
$string = 'Hello';
$array = array('some', 'data');
// return $string or $array depending on
}
现实生活中的例子
这个函数在 echo 上返回一个图像,在 print_r 上返回一个对象:http://getkirby.com/docs/cheatsheet/helpers/thumb
【问题讨论】:
-
发布你的函数里面有什么
-
@Uchiha 我在我的问题中添加了一个函数。
-
但是你在这里返回的东西
-
我认为最简单的方法是在函数中添加一个参数,指定其调用方式
标签: php arrays string object echo