<? 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
-
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 the
getCircleArea (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 the
echo getCircleArea (5);
line.
Another advantage of usingreturn is that the function can be reused for another purpose.
If you want to find the volume of a cylinder, you can use
getCircleArea ()
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.
Related articles
- php - i want to get data and return a string in laravel model
- php - i want to upload an image using xfree
- php - wordpress: tab switching using custom field items
- php - about the version when developing using the framework
- php - about passing value to form using tag
- about php functions
- php - payment using square i want to register a memo or product name in payment
- how to write phpdoc @return in laravel
- i want to update the db using variables in pdo in php, but it doesn't work
- how to send an email using yahoo mail in php
- php - i want to call my name using my membership information, but
- php - i want to save a file on another server using the move_uploaded_file function
- php - i want to fix the display being enlarged when using a smartphone
- php - [wp] category display using get_the_terms
- php - i want to run a file on another server using curl
- php - make a table using an array of numbers only
- php - function execution and return value after routing without framework
- about php functions
- i don't know how to remove some lines using php: fopen ()
- i want to send a request to an app with a login screen using phpstorm's http client
In the above function, the value is
echo
so 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 do
echo
directly" will not have a turn.