HH\Vector::lastValue

Description

public function lastValue(): ?Tv

Returns the last value in the current Vector.

Return Values

  • ?Tv - The last value in the current Vector, or null if the current Vector is empty.

Examples

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

<?hh

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

function echoLastValue(Vector<string> $v): void {
  $last_value = $v->lastValue();
  if ($last_value !== null) {
    echo 'Last value: '.$last_value."\n";
  } else {
    echo 'No last value (Vector is empty)'."\n";
  }
}

// Will print "Last value: yellow"
echoLastValue(Vector {'red', 'green', 'blue', 'yellow'});

// Will print "No last value (Vector is empty)"
echoLastValue(Vector {});
Output
Last value: yellow
No last value (Vector is empty)