How to find PHP String length programmatically?

Published March 11, 2021

Hi guys, in this post we are going to talk about php string length and how it useful in programming examples.

We all are know we can create a variable with string type and each string is a sequence of characters which will enclosed in between pair of double quotation or pair of single quotation is called string in PHP. So we need to find the how many characters contains in a string

PHP String with double quotation

$name="SriKrishna Devaraya";

String with single quotation

$name='SriKrishna Devaraya';

 

We have a string function to find string length in php by strlen() 

 

Syntax:
 

strlen(parameter);

 

This string function takes a string as parameter which we need to find the length of the string.

 

This function counts the number of characters that string contains and return this values as length of the string.

 

Example to Find String length in PHP

<?php

$str1 = 'PHP String';

echo strlen($str1); // Outputs: 10

echo "<br>";

$str2 = ' Length of the string by PHP string function! ';

echo strlen($str2); // Outputs: 46

?>

 

Output

10
46

 

 

PHP String length Examples

<?php

$str1 = 'abcd';

echo strlen($str1); // Outputs: 4

echo "<br>";

$str2 = ' ab cd ';

echo strlen($str2); // Outputs: 7

?>

 

 

<?php

$str1 = 'php';

echo strlen($str1); // Outputs: 3

echo "<br>";

$str2 = ' s p a c e';

echo strlen($str2); // Outputs: 10

?>

 

In this example we find the string length in php by string function strlen()

Like this we have other string functions in php to manipulate strings data, we will discuss all php string functions in coming posts

 

Read PHP string Parser | How to find string between string in PHP

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

707 Views