Calculate the length of a string with PHP

Calculating the length of a string is a useful feature of any language, in fact
it’s one of the few pieces of functionality that has a consistently simple
syntax across modern languages. You can calculate the length of a
string as part of some server side user input validation or do so to determine
if you should truncate a string and add ellipses to it. To calculate the length
of a string all you need to do is:

$string = 'This is my string, it's kinda longish but not really.'
$length = strlen($string

Not much to it! In a future post I’ll discuss truncating a string with ellipses
🙂

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.