How to Find String Length in PHP With Out Using Strlen

As a programmer, you may encounter situations where you need to find the length of a string, but you can’t use the built-in strlen function.

This may be due to some restrictions or limitations set by the project or programming challenge.

In such cases, you can use alternative methods to find the length of a string in PHP.

In this tutorial, we’ll discuss a few methods to find the length of a string without using strlen.


Using For Loop

One of the simplest ways to find the length of a string is by using a for loop.

You can create a loop that runs until the end of the string, incrementing the count for each iteration.

Here’s an example of how you can use a for loop to find the length of a string in PHP.

$string = "Hello, World!";
$length = 0;
for ($i = 0; $string[$i] != ''; $i++) {
    $length++;
}
echo $length;

In this example, we create a variable called $length and set it to zero.

We then create a for loop that starts at 0 and continues until the end of the string.

For each iteration of the loop, we increment the value of $length by 1.

Finally, we echo the value of $length to display the length of the string.

Using While Loop

Another alternative to using strlen is to use a while loop.

The concept is similar to the for loop, but instead of using a for loop, we’ll use a while loop.

Here’s an example of how you can use a while loop to find the length of a string in PHP.

$string = "Hello, World!";
$length = 0;
$i = 0;
while ($string[$i] != '') {
    $length++;
    $i++;
}
echo $length;

In this example, we create a variable called $length and set it to zero.

We then create a while loop that continues until the end of the string.

For each iteration of the loop, we increment the value of $length by 1 and also increment the value of $i by 1.

Finally, we echo the value of $length to display the length of the string.

Using Recursion

A more complex but efficient way to find the length of a string in PHP is by using recursion.

Recursion is a programming concept where a function calls itself repeatedly until a certain condition is met.

Here’s an example of how you can use recursion to find the length of a string in PHP.

function findLength($string, $i = 0) {
    if ($string[$i] == '') {
        return $i;
    }
    return findLength($string, $i + 1);
}

$string = "Hello, World!";
$length = findLength($string);
echo $length;

In this example, we create a function called findLength that takes two arguments: $string and $i.

The function checks if the character at the index $i of the string is equal to an empty string.

If it is, the function returns the value of $i. If not, the function calls itself with the updated value of $i + 1.

Finally, we call the function and store the result in a variable $length, which will be the length of the string.

We then echo the value of $length to display the length of the string.


Conclusion

In this tutorial, we discussed three methods to find the length of a string in PHP without using the built-in strlen function.

Whether you choose to use a for loop, a while loop, or recursion, each method provides an alternative solution to finding the length of a string.

The choice of method will depend on the specific requirements of your project or challenge.

Regardless of the method you choose, the most important thing is to have a solid understanding of how to find the length of a string in PHP.