php8.5
Home/ Manual/ predefined / generator/ Generator::key

Generator::key

PHP function Edit on GitHub ✎

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

Get the yielded key

Description

Generator::key(): mixed

Gets the key of the yielded value.

Parameters Parameters

Return Values

Returns the yielded key.

Examples

Generator::key example

php
<?php

function Gen()
{
    yield 'key' => 'value';
}

$gen = Gen();

echo "{$gen->key()} => {$gen->current()}";

The above example will output:

output
key => value

Source: language/predefined/generator/key.xml · from the official PHP manual (php/doc-en)