length

Return the length of a string, array or object

Definition

number length(string|array|object $subject)

Returns the length of the given argument using the following types rules:

  • string: returns the number of code points in the string
  • array: returns the number of elements in the array
  • object: returns the number of key-value pairs in the object

Parameters

string | array | object $subject

Data to calculate the length of.

Returns

number

Length of input.

Examples

{
  a: length('abc'),
  b: length(`[]`),
  c: length(['a', 'b', 'c']),
  d: length(`{}`),
  e: length({name: 'Mt. Everest', height: '8849m'}),
  f: length(not_there),      // Error
  g: length(`123`),          // Error
  h: length(`true`),         // Error
  i: length(`null`)          // Error
}
{
  "a": 3,
  "b": 0,
  "c": 3,
  "d": 0,
  "e": 2
}