lastKey
Note
This is a point-in-time snapshot of the API documentation from January 2026. Going forward, we will not be maintaining a public copy of these references, and recommend users to refer to the built-in signature helpers available in the Hack LSP instead for complete and up-to-date information.
Returns the last key in the current Vector
public function lastKey(): ?int;
Returns
Examples
This example shows how lastKey() can be used even when a Vector may be empty:
function echoLastKey(Vector<string> $v): void {
$last_key = $v->lastKey();
if ($last_key !== null) {
echo 'Last key: '.$last_key."\n";
} else {
echo 'No last key (Vector is empty)'."\n";
}
}
<<__EntryPoint>>
function basic_usage_main(): void {
// Will print "Last key: 3"
echoLastKey(Vector {'red', 'green', 'blue', 'yellow'});
// Will print "No last key (Vector is empty)"
echoLastKey(Vector {});
}