Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 47 / 68

Functions That Return Multiple Values (Arrays and list())

In this lesson you will learn to return several values from one function using an array and list() syntax. This is for anyone searching for "PHP return multiple values".

A PHP function returns an array, and receiving it as [$a, $b] = someFunction(...) makes it read as though several values came back at once. Picture handing over one box containing two cards, a name and a score.

The sample code has minMax return the array [min($nums), max($nums)], and the caller destructures it with [$min, $max] = minMax(...). That short square-bracket form is shorthand for the list() construct.

A common stumbling block for beginners is the order of the receiving variables. Returning an array means the caller must list its variables in the same order; get it wrong and the wrong values land in them. Returning an associative array and receiving by name is an alternative.

Designing how the return value is received, not just what it is, is what makes a function pleasant to use. This list-syntax pattern is a common and readable choice in PHP function design.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)