Unless it is an object, count casts its argument to an array, and

  count((array) "example")
= count(array("example"))
= 1
Answer from phihag on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.substr-count.php
PHP: substr_count - Manual
If the offset is negative, counting starts from the end of the string. ... 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. This function returns an int. ... <?php $text = 'This is a test'; echo strlen($text), PHP_EOL; // 14 echo substr_count($text, 'is'), PHP_EOL; // 2 // the string is reduced to 's is a test', so it prints 1 echo substr_count($text, 'is', 3), PHP_EOL; // the text is reduced to 's i', so it prints 0 echo substr_count($text, 'is', 3, 3), PHP_EOL; // prints only 1, because it doesn't count overlapped substrings $text2 = 'gcdgcdgcd'; echo substr_count($text2, 'gcdgcd'), PHP_EOL; // throws an exception because 5+10 > 14 echo substr_count($text, 'is', 5, 10), PHP_EOL; ?>
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_string_substr_count.asp
PHP substr_count() Function
Note: This function does not count overlapped substrings (see example 2). Note: This function generates a warning if the start parameter plus the length parameter is greater than the string length (see example 3). ... <?php $str = "This is nice"; echo strlen($str)."<br>"; // Using strlen() to return the string length echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is nice" echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is nice" echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i" ?> Try it Yourself ยป
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_string_count_chars.asp
PHP count_chars() Function
affected_rows autocommit change_user character_set_name close commit connect connect_errno connect_error data_seek debug dump_debug_info errno error error_list fetch_all fetch_array fetch_assoc fetch_field fetch_field_direct fetch_fields fetch_lengths fetch_object fetch_row field_count field_seek get_charset get_client_info get_client_stats get_client_version get_connection_stats get_host_info get_proto_info get_server_info get_server_version info init insert_id kill more_results multi_query next_result options ping poll prepare query real_connect real_escape_string real_query reap_async_query refresh rollback select_db set_charset set_local_infile_handler sqlstate ssl_set stat stmt_init thread_id thread_safe use_result warning_count PHP Network
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-substr_count-function
PHP substr_count() Function - GeeksforGeeks
June 22, 2023 - Below programs illustrate the substr_count() function: Program 1: When both optional parameters are not passed. ... <?php // PHP program to demonstrate the substr_count() function $str = "geeks for geeks"; echo substr_count($str, "geeks"); // ...
Find elsewhere
๐ŸŒ
BCCNsoft
doc.bccnsoft.com โ€บ docs โ€บ php-docs-7-en โ€บ function.substr-count.html
Count the number of substring occurrences
substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive. ... This function doesn't count overlapped substrings. See the example below! ... 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. This function returns an integer. ... <?php $text = 'This is a test'; echo strlen($text); // 14 echo substr_count($text, 'is'); // 2 // the string is reduced to 's is a test', so it prints 1 echo substr_count($text, '
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ phps-substr-count-function-a-complete-guide
PHP's `substr_count()` Function: A Complete Guide
When null, searches to the end of the string. Negative values subtract from the total searchable length. Return Value: Returns an integer representing the count of non-overlapping occurrences of $needle in $haystack. The simplest use case involves counting all occurrences of a substring: <?php $text = "To be or not to be, that is the question."; $count = substr_count($text, "be"); echo "Found {$count} occurrences of 'be'"; // Output: Found 2 occurrences of 'be' ?>
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-count-the-number-of-words-in-a-string-in-php
Count the number of words in a string in PHP - GeeksforGeeks
December 5, 2025 - The str_word_count() method counts the number of words in a string. Fastest and simplest built-in method specifically made for counting words. Reliable for basic ASCII text, requiring almost no preprocessing.
๐ŸŒ
3D Bay
clouddevs.com โ€บ home โ€บ php guides โ€บ phpโ€™s str_word_count() function: counting words in strings
PHP's str_word_count() Function: Counting Words in Strings
December 13, 2023 - Learn how to count words in strings with PHP's str_word_count() function. Explore its usage, options, and practical examples in this comprehensive guide.
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ php-for-loop-exercise-7.php
PHP for loops Exercise: Count a specified characters in a string - w3resource
March 31, 2026 - The loop condition is based on the length of the text string (strlen($text)). Within the loop, it uses "substr()" function to extract each character from the text string (substr($text, $x, 1)) and compares it with the search character ($search_char).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-program-to-count-the-occurrence-of-each-characters
PHP Program to Count the Occurrence of Each Characters - GeeksforGeeks
July 23, 2025 - This article explores various approaches to achieve this task using PHP, These are: ... This approach involves splitting the input string into an array of individual characters using str_split() function.