函数回声与返回 [英] Function Echo vs Return

查看:157
本文介绍了函数回声与返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一些代码给我适合。

调用函数 d($ z);





<$>

$ z本身是代码中其他地方声明的变量。 p $ p> 函数d($ x1){
$ distance = $ x1;
返回$ distance;

$ / code>

这样做:

 函数d($ x1){
$ distance = $ x1;
echo $ distance;
}

任何想法为何?

注意:我不想让这个值回显。我希望能够稍后使用该值作为变量,并且不一定会打印到屏幕上。 (现在,上面是一个简化版本,很多已被删除,我真正想做的事情,但即使上述不会正确返回给我)



虽然这不是我想要做的,但因为我想使用$ distance的结果作为内部变量(而不是回显它),所以如果我做了<?= $ distance?>< code>它不适用于返回。

解决方案

有两种可能的(可能更多)问题:


  1. 您没有将正确的值传递给函数

  2. 你没有将返回值赋给变量(例如 $ value = d(some value here);


    编辑:不要忘记echo $ value(存储返回)

    >

    Okay, so I have some code that is giving me fits.

    To call the function d($z);

    $z itself is a declared variable elsewhere in the code.

    This doesn't work:

    function d($x1){
    $distance = $x1;
    return $distance;
    }
    

    This works:

    function d($x1){
    $distance = $x1;
    echo $distance;
    }
    

    Any ideas why?

    Note: I don't want the value echo'd. I want to be able to use the value later on, as a variable, and not necessarily printed to the screen. (Now, the above is a simplified version, a lot has been removed, of what I really want to do, but even the above doesn't return properly for me)

    Although it's not what I want to do, since I want to use the result of $distance as an internal variable (not echo it), if I do <?=$distance?> it doesn't work with the return.

    解决方案

    There are 2 possible (probably more) problems:

    1. You are not passing a correct value into the function

      or

    2. You are not assigning the return to a variable (ex. $value = d("some value here");)

    Edit: Don't forget to echo $value (stored return)

    这篇关于函数回声与返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆