dragon87836215 2012-07-24 19:46
浏览 6
已采纳

变量/函数和PHP逻辑

This may sound like a dumb question, but does assigning a variable to a function mean it only calls the function ONE time, and can be reused unlimited times without making an additional call to the function or does it still call the function multiple times?

is this:

$variable = function_name();
echo $variable;
echo $variable;

the same as this:

echo function_name();
echo function_name();

I understand both situations are ultimately displaying the same bit of information, but I'm curious if that bit of data is fetched one time and reused many, or if it's fetched upon each echo.

So does assigning a variable to a function mean function_name(); does the work only ONE time and the variable just recycles the data for later usage or does function_name(); actually do the work TWO times?

Is it better practice to assign variables or just directly call the function each time it needs to be used?

  • 写回答

4条回答 默认 最新

  • dongpo5264 2012-07-24 19:52
    关注

    a function should always return some values so for example, if you have a function like this.

    function foo() {
        return 'hello world';
    }
    

    and if you call it and assign it to a variable then

    /*
    * in the below example the function will be called once 
    * and the function's return value will be assigned to $a variable
    * now $a will contain value 'hello world';
    */
    $a = foo();
    

    if you try printing the variable $a it will simply print the value it contains and not call the function once again, since $a now contains 'hello world';

    by echoing $a variable multiple times.

    echo $a;
    echo $a;
    echo $a;
    

    will simply print the value hello world three times and not call the function three times.

    but when you call the function multiple times for example.

    $a = foo();
    $a = foo();
    

    now coming to your question.

    this will result in the function being called multiple times. and the value being overwritten from the previous value returned by the function call. So does assigning a variable to a function mean function_name(); does the work only ONE time and the variable just recycles the data for later usage or does function_name(); actually do the work TWO times?

    i hope i have explained this in full details.

    Is it better practice to assign variables or just directly call the function each time it needs to be used?
    

    well it depends on the context, and the function you want to use, the bottom line is, always try returning some values from the function, for example.

    a) if the function is meant for fetching some values and formatting it, then should return it as array, string or whatever data type.

    b) if the function is meant to do some execution and not any fetching, then you can return boolean values like return true|false indicating wether the operation was successful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题