PHP provides built-in functions to measure string length and count array elements. The most commonly used are strlen() and count().
strlen() – Returns the number of characters in a string.count() – Returns the number of elements in an array.strlen(string) → integer lengthcount(array) → integer total elements
// Calculate length of a string
<?php
$string = "Hello World!";
$length = strlen($string);
echo $length;
?>
The string Hello World! contains 12 characters including the space and exclamation mark.
// Count elements in an array
<?php
$array = array(1, 2, 3, 4);
echo count($array);
?>
The array contains 4 elements, so count() returns 4.
Type text below to see character count (JavaScript simulation of strlen()):
Characters: 0
strlen() before trimming or formatting strings.count() inside loops for dynamic array handling.mb_strlen().strlen() vs mb_strlen().