[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> compat.php (summary)

WordPress implementation for PHP functions either missing from older PHP versions or not included by default. This file is loaded extremely early and the functions can be relied upon by drop-ins. Ergo, please ensure you do not rely on external functions when writing code for this file. Only use functions built into PHP or are defined in this file and have adequate testing and error suppression to ensure the file will run correctly and not break websites.

File Size: 551 lines (16 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 1 file
 wp-includes/sodium_compat/autoload.php

Defines 21 functions

  _()
  _wp_can_use_pcre_u()
  _is_utf8_charset()
  mb_substr()
  _mb_substr()
  mb_strlen()
  _mb_strlen()
  utf8_encode()
  utf8_encode()
  utf8_decode()
  utf8_decode()
  array_is_list()
  str_contains()
  str_starts_with()
  str_ends_with()
  array_find()
  array_find_key()
  array_any()
  array_all()
  array_first()
  array_last()

Functions
Functions that are not part of a class:

_( $message )   X-Ref
Compat function to mimic _(), an alias of gettext().

param: string $message The message being translated.
return: string

_wp_can_use_pcre_u( $set = null )   X-Ref
Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.

param: bool $set Deprecated. This argument is no longer used for testing purposes.

_is_utf8_charset( $charset_slug )   X-Ref
No description

mb_substr( $string, $start, $length = null, $encoding = null )   X-Ref
Compat function to mimic mb_substr().

param: string      $string   The string to extract the substring from.
param: int         $start    Position to being extraction from in `$string`.
param: int|null    $length   Optional. Maximum number of characters to extract from `$string`.
param: string|null $encoding Optional. Character encoding to use. Default null.
return: string Extracted substring.

_mb_substr( $str, $start, $length = null, $encoding = null )   X-Ref
Internal compat function to mimic mb_substr().

Only supports UTF-8 and non-shifting single-byte encodings. For all other encodings
expect the substrings to be misaligned. When the given encoding (or the `blog_charset`
if none is provided) isn’t UTF-8 then the function returns the output of {@see \substr()}.

param: string      $str      The string to extract the substring from.
param: int         $start    Character offset at which to start the substring extraction.
param: int|null    $length   Optional. Maximum number of characters to extract from `$str`.
param: string|null $encoding Optional. Character encoding to use. Default null.
return: string Extracted substring.

mb_strlen( $string, $encoding = null )   X-Ref
Compat function to mimic mb_strlen().

param: string      $string   The string to retrieve the character length from.
param: string|null $encoding Optional. Character encoding to use. Default null.
return: int String length of `$string`.

_mb_strlen( $str, $encoding = null )   X-Ref
Internal compat function to mimic mb_strlen().

Only supports UTF-8 and non-shifting single-byte encodings. For all other
encodings expect the counts to be wrong. When the given encoding (or the
`blog_charset` if none is provided) isn’t UTF-8 then the function returns
the byte-count of the provided string.

param: string      $str      The string to retrieve the character length from.
param: string|null $encoding Optional. Count characters according to this encoding.
return: int Count of code points if UTF-8, byte length otherwise.

utf8_encode( $iso_8859_1_text )   X-Ref
Converts a string from ISO-8859-1 to UTF-8.

param: string $iso_8859_1_text Text treated as ISO-8859-1 (latin1) bytes.
return: string Text converted into a UTF-8.

utf8_encode( $iso_8859_1_text )   X-Ref


utf8_decode( $utf8_text )   X-Ref
Converts a string from UTF-8 to ISO-8859-1.

param: string $utf8_text Text treated as UTF-8.
return: string Text converted into ISO-8859-1.

utf8_decode( $utf8_text )   X-Ref


array_is_list( $arr )   X-Ref
Polyfill for `array_is_list()` function added in PHP 8.1.

Determines if the given array is a list.

An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.

param: array<mixed> $arr The array being evaluated.
return: bool True if array is a list, false otherwise.

str_contains( $haystack, $needle )   X-Ref
Polyfill for `str_contains()` function added in PHP 8.0.

Performs a case-sensitive check indicating if needle is
contained in haystack.

param: string $haystack The string to search in.
param: string $needle   The substring to search for in the `$haystack`.
return: bool True if `$needle` is in `$haystack`, otherwise false.

str_starts_with( $haystack, $needle )   X-Ref
Polyfill for `str_starts_with()` function added in PHP 8.0.

Performs a case-sensitive check indicating if
the haystack begins with needle.

param: string $haystack The string to search in.
param: string $needle   The substring to search for in the `$haystack`.
return: bool True if `$haystack` starts with `$needle`, otherwise false.

str_ends_with( $haystack, $needle )   X-Ref
Polyfill for `str_ends_with()` function added in PHP 8.0.

Performs a case-sensitive check indicating if
the haystack ends with needle.

param: string $haystack The string to search in.
param: string $needle   The substring to search for in the `$haystack`.
return: bool True if `$haystack` ends with `$needle`, otherwise false.

array_find( array $array, callable $callback )   X-Ref
Polyfill for `array_find()` function added in PHP 8.4.

Searches an array for the first element that passes a given callback.

param: array    $array    The array to search.
param: callable $callback The callback to run for each element.
return: mixed|null The first element in the array that passes the `$callback`, otherwise null.

array_find_key( array $array, callable $callback )   X-Ref
Polyfill for `array_find_key()` function added in PHP 8.4.

Searches an array for the first key that passes a given callback.

param: array    $array    The array to search.
param: callable $callback The callback to run for each element.
return: int|string|null The first key in the array that passes the `$callback`, otherwise null.

array_any( array $array, callable $callback )   X-Ref
Polyfill for `array_any()` function added in PHP 8.4.

Checks if any element of an array passes a given callback.

param: array    $array    The array to check.
param: callable $callback The callback to run for each element.
return: bool True if any element in the array passes the `$callback`, otherwise false.

array_all( array $array, callable $callback )   X-Ref
Polyfill for `array_all()` function added in PHP 8.4.

Checks if all elements of an array pass a given callback.

param: array    $array    The array to check.
param: callable $callback The callback to run for each element.
return: bool True if all elements in the array pass the `$callback`, otherwise false.

array_first( array $array )   X-Ref
Polyfill for `array_first()` function added in PHP 8.5.

Returns the first element of an array.

param: array $array The array to get the first element from.
return: mixed|null The first element of the array, or null if the array is empty.

array_last( array $array )   X-Ref
Polyfill for `array_last()` function added in PHP 8.5.

Returns the last element of an array.

param: array $array The array to get the last element from.
return: mixed|null The last element of the array, or null if the array is empty.



Generated : Sat May 9 08:20:02 2026 Cross-referenced by PHPXref