I know this is a pretty old issue, but this piece of code worked for me.

$s = 'string';
$i=0;
while ($s[$i] != '') {
  $i++;
}
print $i;
Answer from Vaibhav Jain on Stack Overflow
🌐
PHP
php.net › manual › en › function.strlen.php
PHP: strlen - Manual
PHP's strlen function behaves differently than the C strlen function in terms of its handling of null bytes ('\0'). In PHP, a null byte in a string does NOT count as the end of the string, and any null bytes are included in the length of the string.
🌐
W3Schools
w3schools.com › php › func_string_strlen.asp
PHP strlen() Function
zip_close() zip_entry_close() ... zip_entry_open() zip_entry_read() zip_open() zip_read() PHP Timezones ... The strlen() function returns the length of a string....
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-find-string-length-in-php.php
How to Find String Length in PHP
The strlen() function return the length of the string on success, and 0 if the string is empty. Let's take a look at the following example to understand how it actually works: ... <?php $str1 = 'Hello world!'; echo strlen($str1); // Outputs: ...
🌐
Udemy
blog.udemy.com › home › it & development › web development › the php strlen function: getting the php string length
The PHP STRLEN Function: Getting the PHP String Length - Udemy Blog
April 14, 2026 - So, the PHP string “Hello, World!” is an array of 13 characters, starting with the number 0. The built-in function PHP STRLEN returns the number of characters in the string. ... So, you use STRLEN any time you need the length of a string.
Find elsewhere
🌐
W3Schools
w3schools.com › php › php_string.asp
PHP Strings
In PHP, strings are surrounded by either double quotes, or single quotes.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-strlen-function
PHP strlen() Function - GeeksforGeeks
July 11, 2025 - The strlen() is a built-in function in PHP which returns the length of a given string.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-get-string-length-in-php
How to get String Length in PHP ? - GeeksforGeeks
July 23, 2025 - ... <?php // PHP program to count all // characters in a string $str = "GeeksforGeekslover"; // Using strlen() function to // get the length of string $len = strlen($str); // Printing the result echo $len; ?>
Top answer
1 of 6
110

http://php.net/manual/en/language.types.string.php says:

Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)

In PHP 5.x, strings were limited to 231-1 bytes, because internal code recorded the length in a signed 32-bit integer.


You can slurp in the contents of an entire file, for instance using file_get_contents()

However, a PHP script has a limit on the total memory it can allocate for all variables in a given script execution, so this effectively places a limit on the length of a single string variable too.

This limit is the memory_limit directive in the php.ini configuration file. The memory limit defaults to 128MB in PHP 5.2, and 8MB in earlier releases.

If you don't specify a memory limit in your php.ini file, it uses the default, which is compiled into the PHP binary. In theory you can modify the source and rebuild PHP to change this default value.

If you specify -1 as the memory limit in your php.ini file, it stop checking and permits your script to use as much memory as the operating system will allocate. This is still a practical limit, but depends on system resources and architecture.


Re comment from @c2:

Here's a test:

<?php

// limit memory usage to 1MB 
ini_set('memory_limit', 1024*1024);

// initially, PHP seems to allocate 768KB for basic operation
printf("memory: %d\n",  memory_get_usage(true));

$str = str_repeat('a',  255*1024);
echo "Allocated string of 255KB\n";

// now we have allocated all of the 1MB of memory allowed
printf("memory: %d\n",  memory_get_usage(true));

// going over the limit causes a fatal error, so no output follows
$str = str_repeat('a',  256*1024);
echo "Allocated string of 256KB\n";
printf("memory: %d\n",  memory_get_usage(true));
2 of 6
17

String can be as large as 2GB.
Source

🌐
Functions-Online
functions-online.com › strlen.html
test strlen online - PHP string functions - functions-online
The function strlen() returns the length of the given $string. ... @mjb4: the charset is a general problem. PHP uses ISO, this site UTF-8.
🌐
Learnsic
learnsic.com › blog › the-php-strlen-function-getting-the-php-string-length
The PHP STRLEN Function: Getting the PHP String Length
This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy · Got it
🌐
Reintech
reintech.io › blog › phps-strlen-function-practical-guide-measuring-string-length
PHP's `strlen()` Function: A Practical Guide for Measuring String Length
For standard ASCII strings, strlen() provides accurate character counts since each character occupies exactly one byte: $username = "admin_user"; $length = strlen($username); echo $length; // Output: 10 // Validating input length if (strlen($password) < 8) { throw new Exception("Password must be at least 8 characters"); } // Checking empty strings $input = trim($_POST['email']); if (strlen($input) === 0) { $errors[] = "Email address is required"; }
🌐
Kubernetes
kubernetes.io › docs › concepts › configuration › configmap
ConfigMaps | Kubernetes
November 21, 2025 - Unlike most Kubernetes objects that have a spec, a ConfigMap has data and binaryData fields. These fields accept key-value pairs as their values. Both the data field and the binaryData are optional. The data field is designed to contain UTF-8 strings while the binaryData field is designed to contain binary data as base64-encoded strings.
🌐
Google
developers.google.com › recaptcha › recaptcha v2
reCAPTCHA v2 | Google for Developers
// The id of the reCAPTCHA widget is assigned to 'widgetId1'. widgetId1 = grecaptcha.render('example1', { 'sitekey' : 'your_site_key', 'theme' : 'light' }); widgetId2 = grecaptcha.render(document.getElementById('example2'), { 'sitekey' : 'your_site_key' }); grecaptcha.render('example3', { 'sitekey' : 'your_site_key', 'callback' : verifyCallback, 'theme' : 'dark' }); }; </script> </head> <body> <!-- The g-recaptcha-response string displays in an alert message upon submit.
🌐
Nextcloud
nextcloud.com › home › changelog
Nextcloud changelog
April 12, 2022 - Fix: Remove deprecated RFC7231 constant to avoid warnings on PHP 8.5 (server#58201) Fix: obey x-nc-scheduling flag on delete (server#58203) Remove external shares from share list (server#58204) Fix: show configuration options for external storage backends (server#58205) Fix(snowflake): cast lastId to string (server#58206) Feat: improve VerifyMountPointEvent event (server#58207) Chore: update `@nextcloud/files` to v4.0.0-rc.3 (server#58208) Fix(preview): Fix scanning preview (server#58209) Fix(preview): Handle unique constraints (server#58216) Fix(user_status): use getFirstDay() from @nextcloud
🌐
Altorouter
altorouter.com › home › php string mastery: unleash power with length manipulation
String Length PHP: A Comprehensive Guide
January 22, 2024 - Mastering string length functions is crucial whether you’re validating user input, truncating text, or performing complex string operations. PHP’s simplest method of determining how long a string is is strlen(). This function returns the number of characters in a given string.
🌐
PHP
durak.org › sean › pubs › software › php-7.0.0 › function.strlen.html
Get string length - PHP 7.0.0 Documentation
(PHP 4, PHP 5) strlen — Get string length · int strlen ( string $string ) Returns the length of the given string. string · The string being measured for length. The length of the string on success, and 0 if the string is empty. Example #1 A strlen() example ·