Strings\startsWith()

predicate returns Closure returns bool pure
string → (string → bool)
At a glance — Bind a prefix; the returned Closure is a reusable "starts with this?" check. Wraps str_starts_with().

Returns a function which can be used to check if a string starts with a defined sub string. The created function can then reused over any string, or used as part of a Higher Order Function such as array_filter().

/**
  * @param string $find The value to look for.
  * @return Closure(string):bool
  */
 Strings\startsWith(string $find): Closure

Returned Closure

When Strings\startsWith() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param string $source
  * @return bool
  * @psalm-pure
  */ 
$function(string $source): bool

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create function to check if a string starts with 'foo'
$startsWithFoo = Strings\startsWith('foo');

// Called as a function.
echo $startsWithFoo('foo begins'); // true

// Used in a higher order function.
$array = array_filter(['foo begins', 'not foo'], $startsWithFoo);
print_r($array); // ['foo begins']

Curried

This can be called inline using currying.

Strings\startsWith('foo')('foo begins'); // true
Strings\startsWith('foo')('not foo'); // false

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$array = array_filter(['foo begins', 'not foo'], Strings\startsWith('foo'));

// ['foo begins']

Details

Strings Functions

Releated String predicate Functions