Home >Backend Development >PHP Problem >Tips on using strlen() function in php
String is a frequently used data type in php. We often need to determine the length of the string. In this case, we need to use the strlen()
function. This article will take you with us. Take a look at this function. First, let’s take a look at the syntax of the strlen()
function:
strlen ( string $string )
$string: The string whose length needs to be calculated.
Return value: If successful, the length of the string $string is returned; if string is empty, 0 is returned.
Usage examples:
1.$str is an English string
<?php $str = 'fdgdfgsdfg'; echo strlen($str); echo "<br>"; $str = 'dfgdf ab cd '; echo strlen($str); echo "<br>"; ?>
输出: 10 12
2.$str is a Chinese string
We can find that it is very convenient for English, but it needs to be converted for Chinese characters, so we can use mb_strlen()
<?php $str = "你好"; echo strlen($str); echo mb_strlen($str,'UTF-8'); // 2 ?>
输出:6 2
Recommended: 《2021 PHP interview questions summary (collection)》《php video Tutorial》
The above is the detailed content of Tips on using strlen() function in php. For more information, please follow other related articles on the PHP Chinese website!