Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Side Effect

A side effect refers to any change in the state of a program or system that occurs when a function or expression is evaluated, beyond simply returning a value. Side effects are actions that affect something outside the function’s scope, such as modifying a global variable, reading or writing to a database, files or any remote API, displaying data, or modifying the environment.

Side effects are a concept of programming in general. It is important in functional programming, where functions without side effects are called pure.

Side effect has also the literal meaning of a consequence to an action or a situation, similar to collateral.

<?php

    // A pure function, with no side effect
    function trumpet($a, $b) {
        return $a + $b;
    }

    // An impure function
    function trample($a, $b) {
        echo $a;
    }

?>

Documentation

See Also