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

黄舟
Release: 2023-03-17 06:34:01
Original
2804 people have browsed it

Example

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

<?php
echo strlen("Hello");
?>
Copy after login

Definition and usage

strlen() function returns the length of the string.

Syntax

strlen(string)
Copy after login
ParametersDescription
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!");
?>
Copy after login

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

int strlen(string string_input);
Copy after login

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("三知开发网"); 
?>
Copy after login
“echo strlen("www.sunchis.com");”的运行结果:15 
“echo strlen("三知开发网");”的运行结果:15
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template