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

Yield ou return

<?php

function foo() {
    yield 1;
    yield 2;
    yield 3;
    
    return ['a', 'b', 'c'];
}

function foo2() {
    return ['a', 'b', 'c'];
}

function goo($x, $y, $z) {
    print "$x $y $z\n";
}

goo(...foo());
goo(...foo2());

Il est possible d’étaler un tableau comme arguments dynamiques : cela s’applique aux tableaux retournés, après un appel de fonction.

L’appel de fonction peut en réalité être de deux types : une fonction ou un générateur.

Le premier type est un générateur : les valeurs produites par yield sont alors utilisées comme arguments et le tableau retourné est ignoré.

Dans le second cas, le tableau est retourné et utilisé.

See Also

PHP Features

Last updated: 11 October 2026