Return values versus side effects

Author Topic: Return values versus side effects  (Read 423 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 139
  • Karma: 0
    • No Addiction
Return values versus side effects
« 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.


Related Topics

  Subject / Started by Replies Last post
13 Replies
Last post 08-14-2013, 00:10:59
by Johnny007
1 Replies
Last post 12-03-2011, 06:00:24
by C.Rebecca
3 Replies
Last post 11-20-2015, 04:40:31
by lucygomez
8 Replies
Last post 09-09-2016, 05:47:11
by sachin_pal
1 Replies
Last post 05-16-2016, 02:06:04
by TomClarke