Returns the last key in the current Vector


public function lastKey(): ?int;

Return Values

  • ?int - The last key (an integer) in the current Vector, or null if the Vector is empty.


This example shows how lastKey() can be used even when a Vector may be empty:


namespace Hack\UserDocumentation\API\Examples\Vector\LastKey;

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";

// Will print "Last key: 3"
echoLastKey(Vector {'red', 'green', 'blue', 'yellow'});

// Will print "No last key (Vector is empty)"
echoLastKey(Vector {});
Last key: 3
No last key (Vector is empty)