• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php strlen() – Get the Length of String Variable in php

php strlen() – Get the Length of String Variable in php

March 29, 2022 Leave a Comment

In php, the strlen() function allows us to get number of characters and length of a string variable.

$variable = "I'm a variable";

echo strlen($variable);

//Output:
14

When working with string variables in our php programs, it is useful to be able to easily extract information about the values of the strings. In php, we can get the length of a string with the php strlen() function.

strlen() returns the length, or the number of characters, of a string.

To use strlen(), pass a string variable and strlen() will return the string length.

Below is a simple example in php of how to use strlen() to get the length of a string variable.

$variable = "I'm a variable";

echo strlen($variable);

//Output:
14

Examples of Using the php strlen() Function

strlen() is a very useful function and there are a number of examples of how we can use strlen().

One example is if you want to loop over the characters in a string. You can use range() and loop from 0 to the length of the string.

$variable = "this is a variable";

foreach(range(0,strlen($variable)) as $i) {
   // do stuff here
}

Another example of using the php strlen() function is if you want to get a substring of the last handful of characters.

Below is an example in php of how to get a substring of the last three characters using substr() and strlen().

$variable = "this is a variable";

$last_three = substr($variable, strlen($variable) - 3);

echo $last_three;

//Output:
ble

Hopefully this article has been useful for you to learn how to use the php strlen() function to find the length of string variables in your php programs.

Other Articles You'll Also Like:

  • 1.  Remove HTML Tags from String in php
  • 2.  php json_encode() Function – Get JSON Representation of Variables in php
  • 3.  Get Array Length with php count() Function
  • 4.  php Rename File – Renaming Files and Directories Easily
  • 5.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 6.  Get First Element of Array in php
  • 7.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 8.  php acos – Find Arccosine and Inverse Cosine of Number
  • 9.  PHP Variables Are Preceded by $
  • 10.  php Move File from One Directory to Another Folder

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy