contains

Check if a string or array contains a value

Definition

boolean contains(array|string $source, any $value)

For array $source, returns whether the array contains an element equal to $value.

For string $source, returns whether $value is a substring of $source.

Parameters

array | string $source

An array or a string to be searched.

any $value

Value to look for in $source.

Returns

boolean

true if $source contains $value, otherwise false.

Examples

{
  a: contains('ace0', 'a'),
  b: contains('ace0', 'b'),
  c: contains('ace0', 'ce0'),
  d: contains('ace0', `0`),
  e: contains('ace0', ''),
  f: contains(['a', 'c', `true`, `null`], `true`),
  g: contains(['a', 'c', `true`, `null`], `null`),
  h: contains(['a', 'c', `true`, `null`], `[]`),
  i: contains(['a', 'c', `true`, `null`], `{}`)
}
{
  "a": true,
  "b": false,
  "c": true,
  "d": true,
  "e": true,
  "f": true,
  "g": true,
  "h": false,
  "i": false
}