Seo Forum

Coding & Programming => Programming Forum => Topic started by: chinmay.sahoo on 12-15-2015, 04:18:20

Title: Return values versus side effects
Post by: chinmay.sahoo on 12-15-2015, 04:18:20
Every function call is a PHP expression, and (just as with other expressions) there are only two reasons why you might want to include one in your code: for the return value or for the side effects.

The return value of a function is the value of the function expression itself. You can do exactly the same things with this value as with the results of evaluating any other expression. For example, you can assign it to a variable, as in:

$my_pi = pi();

Or you can embed it in more complicated expressions, as in

$approx = sqrt($approx) * sqrt($approx)

Functions are also used for a wide variety of side effects, including writing to files, manipulating databases, and printing things to the browser window. It’s okay to make use of both return values and side effects at the same time—for example, it is very common to have a side-effecting function return a value that indicates whether or not the function succeeded. The result of a function may be of any type, and it is common to use the array type as a way for functions to return multiple values.