Home>
<? php

    function getCircleArea ($radius) {
      echo $radius * $radius * 3;
    }

    getCircleArea (5);
    // result is 75
  ?>

vs

<? php

    function getCircleArea ($radius) {
      return $radius * $radius * 3;
    }

    $circleArea = getCircleArea (5);
    echo $circleArea;

  ?>
What is the benefit of

?

I think the code above is cleaner with fewer lines.

I'm sorry for the low-level questions of programming beginners, but thanks for your answers

  • Answer # 1

      

    I think the code above is cleaner with fewer lines.

    In the above function, the value isechoso it can't bereusedin other code. It ’s overwhelmingly useful to set the value).

    If the program is larger than a certain level, the output to the browser will be done in a limited place, so the "function to doechodirectly" will not have a turn.

  • Answer # 2

    One of the benefits of using

    return is that you don't have to worry about what happens in the function when you read the code.

    For example, if you write the following code (same as the first code written in the question):

    function getCircleArea ($radius) {
      echo $radius * $radius * 3;
    }
    getCircleArea (5);

    When someone else (or you half a year later) reads this code, he sees only thegetCircleArea (5);line first. And I am surprised to run. "Hey, it was output without permission".

    What if this is written (almost the same as the second code in the question)?

    function getCircleArea ($radius) {
      return $radius * $radius * 3;
    }
    echo getCircleArea (5);

    There is no difference in the execution result, but it is easier to predict the execution result just by looking at theecho getCircleArea (5);line.


    Another advantage of using

    return is that the function can be reused for another purpose.

    If you want to find the volume of a cylinder, you can usegetCircleArea ()return as shown in the code below.

    function getCircleArea ($radius) {
      return $radius * $radius * 3;
    }
    function getCylinderVolume ($circle_area, $height) {
      return $circle_area * $height
    }
    $circle_area = getCircleArea (5);
    echo $getCylinderVolume ($circle_area, 10);
    // I'm sorry if there is a bug because I'm not running

  • Answer # 3

    User-defined functions are easier to use for a single purpose "Get" a "CircleArea" like "getCircleArea"
    It's a function, so I think it's not the purpose of echoing.

  • Answer # 4

    If you echo within a function, it ends on the spot.
    By returning, it becomes useful when you want to use the processing result in another processing.

    In the example presented, it is too extreme to say, "This is good". It depends on your requirements.
    But what about the versatility?

    The function "just echo and finish" is not very practical. If you pick up an Exception and end with a system error, you can use echo or just die ().

    The main purpose of functionalization is "role sharing".
    In the example code, it is named "getCircleArea".
    It is designed to perform certain calculations with the passed arguments.
    So if you do echo, don't you think it's not the expected behavior from the name "get"?

    In other words, "depending on requirements" means "how much role is given to the function".
    It is a function name, argument, and internal processing.
    Whether or not there is a return cannot be determined simply because the code is shortened.

  • Answer # 5

    Echo and return are completely different.

    The contents of the function are completely different.


    1. A function that simply outputs the calculation result

    function getCircleArea ($radius) {
        echo $radius * $radius * 3;
    }

    2. A function that gets the calculated result and returns it as a return value

    function getCircleArea ($radius) {
        return $radius * $radius * 3;
    }

    1 does not work on reusing calculation results, and 2 can expand the range of things that can be done using calculation results.