substr_count

FunctionParams and return types changed in PHP 8.0

Count the number of substring occurrences.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

substr_count Function synopsis

substr_count(
    string $haystack,
    string $needle,
    int $offset = 0,
    ?int $length = null
  ): int

Parameters

$haystack

Typestring

The string to search in

$needle

Typestring

The substring to search for

$offset

OptionalTypeintDefault value0

The offset where to start counting. If the offset is negative, counting starts from the end of the string.

$length

OptionalType?intDefault valuenull

The maximum length after the specified offset to search for the substring. It outputs a warning if the offset plus the length is greater than the $haystack length. A negative length counts from the end of $haystack.

Return value

Typeint

This function returns an int.

Changes to the substr_count Function

PHP 8.0

  • Return type added: int
  • Parameter type added for parameter #1 ($haystack): string
  • Parameter type added for parameter #2 ($needle): string
  • Parameter type added for parameter #3 ($offset): int
  • Parameter default value added for position #3 ($offset): 0
  • Parameter type added for parameter #4 ($length): ?int
  • Parameter default value added for position #4 ($length): null
  substr_count(
-     $haystack,
+     string $haystack,
-     $needle,
+     string $needle,
-     $offset,
+     int $offset = 0,
-     $length
+     ?int $length = null
-   )
+   ): int

substr_count Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes