Calculate the length of a string with PHP

Josh Sherman
1 min read
Software Development 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 :)

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

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

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles