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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Creating Custom category.php With Pagination
  • 2.  Replace Space with Dash in php String
  • 3.  How to Create an Empty Array in php
  • 4.  How to Get Current System IP Address in php
  • 5.  php getimagesize – Get Height, Width and Image File Type
  • 6.  php floor – Round Down and Find the Floor of a Number in php
  • 7.  In PHP isset Method is Used For Checking if Variable is Defined
  • 8.  Reversing an Array in php with array_reverse() Function
  • 9.  Truncate Strings in php Using substr()
  • 10.  How to Reverse Array in php Without a Function or Other Variables

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