Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Pure Functions

Pure Functions

In this lesson — part of Functional Foundations — you'll learn pure functions in PHP and why it matters in real work.

Why it matters

Functional style — pure functions and immutability — makes code easier to test and reason about.

Key ideas

  • Pure functions
  • Immutability
  • No shared mutable state
  • Predictable outputs

In practice

Here's how it looks in idiomatic PHP:

<?php
// pure: output depends only on inputs, no side effects
function square(int $x): int {
    return $x * $x;
}

echo square(5);   // always 25, mutates nothing

PHP note: A pure function in PHP touches no globals, performs no I/O, and returns the same result for the same arguments every time.

Try it yourself

Exercise: In PHP, rewrite a function that mutates a global so it's pure.

Recap

You now understand pure functions and can apply it in PHP. Mark this lesson complete and continue to the next one.