Home  >  Article  >  Backend Development  >  PHP function strlen() that returns the length of a string

PHP function strlen() that returns the length of a string

黄舟
黄舟Original
2017-11-04 11:02:222813browse

Example

The function returns the length of the string "Hello":

<?php
echo strlen("Hello");
?>

Definition and usage

strlen() function returns the length of the string.

Syntax

strlen(string)
Parameters Description
string Required. Specifies the string to check.

Technical details

Return value: If successful, return the length of the string, Returns 0 if the string is empty.
PHP Version: 4+
Change Log: Before PHP 5.3.0 , this function treats the array as a string Array, thus returning a string of length 5 and generating an E_NOTICE level error.

More examples

Example 1

Return the length of the string "Hello World":

<?php
echo strlen("Hello world!");
?>

In In PHP, the function strlen() returns the length of a string. The function prototype is as follows:

int strlen(string string_input);

The parameter string_input is the string to be processed.

strlen() function returns the length of bytes occupied by the string. An English letter, number, and various symbols all occupy one byte, and their lengths are all 1. A noon character occupies two bytes, so the length of a noon character is 2. For example

<?php 
echo strlen("www.sunchis.com"); 
echo strlen("三知开发网"); 
?>
“echo strlen("www.sunchis.com");”的运行结果:15 
“echo strlen("三知开发网");”的运行结果:15

There is a question here, doesn’t a Chinese character occupy 2 bytes? "Sanzhi Development Network" clearly has five Chinese characters, so how could the result be 15?

The reason is here: when calculating strlen(), a UTF-8 Chinese character will be treated as having a length of 3.

The above is the detailed content of PHP function strlen() that returns the length of a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn