eHow launches Android app: Get the best of eHow on the go.

How To

How to Return the Length of a String in PHP

Member
By Mike
User-Submitted Article
(0 Ratings)

PHP, as with all other programming languages has a function where you can obtain the length of a string.

This comes in handy when you are validating data such as making sure a user has entered in some data into an input screen.

You can find the length of a string that is assigned a variable, or you can get the length of any string that you enter as is shown in this tutorial.

Here is how you obtain the length of a string with PHP's built in string length function.

Difficulty: Easy
Instructions
  1. Step 1

    To obtain the length of a string you use the strlen() function. It accepts one parameter and it must be of type string. The parameter is required so if you do not supply it you will receive an error.

  2. Step 2

    Lets say you have a variable called $mystring and you assign a string to that variable such as:
    $mystring="Hello World!"

    In the next step you will see how you insert the variable $mystring as the parameter required by the strlen() function.

  3. Step 3

    To obtain the length of the string assigned to our variable $mystring we use it in this manner:
    echo strlen($mystring);

    this will print out the length as a number on the string. In our example the number 12 would be displayed on the screen.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics