Using the PHP Function Strlen to Get the Length of a String
The php function strlen is a handy tool when you need to get the length of a string. It works with any string and returns the number of characters in it. The string could consist of letters, numbers, special characters and arithmetic values.
The strlen() function has one mandatory parameter, the string for which you want to know the length. The function is very easy to use and does not return any error if the string does not exist or is empty.
You must be aware that the strlen() function operates on bytes, not characters, so it may not give you exactly the same result as when using it with single-byte character encodings (like ASCII). For example, in multibyte character encodings, such as UTF-8, each character is represented by multiple bytes. For this reason, a multibyte string with emojis or accented letters might have a larger length than you expect.
When you want to validate form fields, the string length of a value is often a crucial factor to consider. For example, if you have a $META_DESCRIPTION variable that has to be less than or equal to 160 characters, the string length function will help you determine if this is the case.
In the following script, you can see how to use the php function strlen to count the length of a string and the sum of two other strings. Both values are then printed later. The script also showcases that the strlen() function accurately identifies an empty string as having a length of 0, demonstrating how versatile it is when it comes to evaluating numeric sequences.