Detailed explanation of PHP strlen() function, PHP gets string length

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei CloudShare Expert, High-quality Creator in the Network Security Field
"Recommended Column": Friends who are interested in network security can pay attention to the column "Introduction to Mastery of Network Security"

strlen() can return the "length" of a "string " .

grammar

int strlen( $str )

parameter

  • $str : the string whose length needs to be calculated

return value

Returns the length "number of bytes" of the string , and returns 0 for an empty string, NULL, or false.

1. String

Passing "string" as a parameter will return the length of the string, which is also the most commonly used method.

Example:

var_dump(strlen('1234'));
var_dump(strlen('abcd'));

output:

int(4)
int(4)

Two, spaces

strlen() calculates the number of "bytes" rather than the number of "characters" . When a "space" is encountered , the length will also be +1.

Example:

# 一个空格
var_dump(strlen(' '));
# 两个空格
var_dump(strlen('  '));
# 字符中夹杂空格
var_dump(strlen('a b c '));

output:

int(1)
int(2)
int(6)

3. Integers and decimals

Although strlen() requires parameters of string type to be passed in, the length can also be calculated by passing in a "numeric type" . Integers and decimals are the same as strings, and the number of bytes will be calculated.

Example:

var_dump(strlen(123));
var_dump(strlen('123'));
var_dump(strlen(123.1));
var_dump(strlen('123.1'));

output:

int(3)
int(3)
int(5)
int(5)

4. Boolean type

The "Boolean type" is special, true returns 1, false returns 0.

Example:

var_dump(strlen(true));
var_dump(strlen(false));

output:

int(1)
int(0)

Five, translation characters

"Translation character" counts as one byte, length +1, but it needs to be wrapped in "double quotes" , and single quotes will be treated as a string.

Example:

var_dump(strlen("\n"));
var_dump(strlen("\n\r"));
var_dump(strlen('\n'));

output:

int(1)
int(2)
int(2)

6. Arithmetic operators

When the parameter passed in is "without quotation marks" and contains "arithmetic operators" , strlen() will perform the operation first, and then calculate the length of the operation result.

Example:

var_dump(strlen(1+1));
var_dump(strlen(5*5));
var_dump(strlen('1+1'));

output:

int(1)
int(2)
int(3)

Example: Reverse ~ will also be calculated first, and then calculate the length of the operation result

var_dump(strlen(~~10));
var_dump(strlen(~10));

output:

int(2)
int(3)

Seven, logical operators

When the parameter passed in is "without quotation marks" and contains "logical operators" , strlen() will perform the operation first, and then calculate the length of the operation result.

Since the results of logical operators are true or false , their return results are both 1 or 0.

Example:

var_dump(strlen(1&1));
var_dump(strlen(1|0));
var_dump(strlen(!1));

output:

int(1)
int(1)
int(0)

8. Special symbols

The "special symbol" of "English" is counted as one byte, and the length is +1; the "special symbol " of "Chinese" is counted as three bytes, and the length is +3.

Example:

var_dump(strlen(";"));
var_dump(strlen(";"));
var_dump(strlen(','));
var_dump(strlen(','));

output:

int(3)
int(1)
int(3)
int(1)

9. Chinese

The Chinese characters of "UTF8 encoding" occupy 3 bytes, and the length is +3; the Chinese characters of purple "GB2321 encoding" occupy 2 bytes, and the length is +2.

Example:

var_dump(strlen("中文"));
var_dump(strlen('中文'));

output:

int(6)
int(6)

10. Empty and NULL

When $str is "empty string" or NULL , return length 0.

Example:

var_dump(strlen(''));
var_dump(strlen(null));

output:

int(0)
int(0)

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131168453