Iterator::key — Read the current element’s key
Read the current element’s key without advancing the iterator, and see how PHP assigns that key in a `foreach` loop.
CKTIS / PHP
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Read the current element’s key without advancing the iterator, and see how PHP assigns that key in a `foreach` loop.
`Iterator::next()` advances an iterator by one step without returning the element. When iterating manually, check `valid()` before reading the value with `current()`.
`foreach` calls `Iterator::rewind()` before its first `valid()` check. Whether that call positions the iterator at its start depends on the implementation.
`Iterator::valid()` tells you whether there is an element at the iterator’s current position. It does not test the element’s value or advance the iterator.
Putting `&` before `$list` lets a PHP function change the caller's variable. Write it in the parameter declaration, not next to the argument at the call site.
`&$projects` lets a function change the caller’s variable. Put `&` in the parameter declaration, not in the call.
A parameter declared as `&$response` lets a PHP function change the caller’s variable. Put `&` in the parameter declaration, not at the call site.
Putting `&` before `$session` lets the function modify the caller’s array directly. The parameter does not start a PHP session or save its data.
An ampersand before a parameter name lets the function change the caller’s variable. Write `&$st` in the declaration, but pass the variable normally: `markReady($status)`.
Adding `&` to the `$tasks` and `$used` parameters lets the function update both caller variables without returning them.
An ampersand before the `$users` parameter lets the function change the caller’s variable without returning the array. At the call site, pass the variable without `&`.
Assignment by reference gives two names to the same value. Writing through either name changes the shared value; `unset($alias)` removes only the alias binding.
Use `=&` to bind two names to the same variable contents. A write through either name is visible through the other for as long as both names remain bound.
Learn how the two ampersands create an alias for a variable returned by a function, and why this does not start a PHP session.
The ampersand before `$res` lets a function change the variable supplied by its caller instead of returning a replacement value.
In PHP 8.3, a parameter declared as `&$value` lets the function change the caller’s variable. With the default pass by value, assigning to the parameter does not change that variable.
Declare `array &$items` to require an array when the call begins and let the function modify the caller’s variable without returning a result.
`array &$product` requires an array at the call and lets the function change the caller’s variable without returning the array.
A function that returns by reference lets the caller alias a variable. Use `=&` to bind the alias; plain `=` assigns the value without creating that binding.
`SplFixedArray::count()` returns the current number of slots, including slots that contain `null`, rather than the number of non-null values.
Read a fixed-array slot with `offsetGet()` or `[]`, and distinguish a `null` value from an out-of-range index or an unsupported index type.
Write or replace a value in a fixed-size array. Compare the direct method call with bracket assignment and distinguish the two kinds of index errors in PHP 8.3.
Resize an existing `SplFixedArray`. Growing it adds slots initialized to `null`; shrinking it discards values from the end.
`toArray()` returns a native PHP array containing every slot, including those with a `null` value, without changing the original object.