...

/

Referential Transparency

Referential Transparency

Learn about referentially transparent functions with different useful examples.

Overview

A referentially transparent function is one in which the function call is interchangeable with the output of the function. This interchangeability shouldn’t affect a program’s side effects or side causes. Like immutability, referential transparency is associable with pure functions. In fact, the latter is a metric for measuring the former.

Press + to interact
<?php
$insertUnderscores = fn (string $text): string => (
implode('_', explode(' ', $text))
);
var_dump($insertUnderscores('hello world') === 'hello_world'); // evaluates to true

The lambda function above replaces spaces in a text with underscores. This function’s output, with a "hello world" input, is compared with the underscore-separated string "hello_world". The ...

Access this course and 1400+ top-rated courses and projects.