Php performance optimization: using isset () to determine the string length is faster than strlen. How can I determine the length of a string in php? Many people think of strlen () mb_strlen () functions first. However, in terms of program performance, how can these two functions determine the length of a string in php? Many people think of strlen () mb_strlen () functions first. However, in terms of program performance, these two functions are not optimal in determining the length of a string, although they are specialized functions for detecting the length of a string.
In my practice, php judges the length of a string, and isset () is faster than strlen (), and the execution efficiency is higher.
Why is isset () faster than strlen?
The strlen () function is executed quite quickly because it does not perform any calculations and only returns the known string lengths stored in the zval structure (C's built-in data structure, used to store PHP variables. However, because strlen () is a function, it is more or less slow, because function calling goes through many steps, such as lowercase letters and hash searches, and will be executed along with the called function. Therefore, in some cases, the rational use of isset () can accelerate your program. Because isset () is a language structure, it does not require function searching or lowercase letters for execution.
The following is an example of how to judge the string length through isset () and strlen:
$ Str = 'http: // www.phpernote.com/php-template/436.html'{if (strlen ($ str) <5) {echo "below 5";} if (! Isset ($ str {5}) {echo "below 5 ";}
Next we will analyze the strlen () and isset () functions in detail.
PHP strlen () function
Definition and usage
The strlen () function returns the length of the string.
Syntax: strlen (string)
Parameter: string
Description: required. Specifies the string to be checked.
Strlen () function instance
The result will be output:
12
PHP isset () function
The isset function checks whether variables are set.
Syntax: bool isset (mixed var [, mixed var [,...])
Return value:
If the variable does not exist, FALSE is returned.
If the variable exists and its value is NULL, FALSE is returned.
If the variable exists and the value is not NULL, true is returned.
When multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement; otherwise, the result is FALSE.
If unset () is used to release a variable, it will no longer be isset (). If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("") is not equivalent to the NULL constant of PHP.
Warning: isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function.
Articles you may be interested in
- Php string replacement function str_replace is faster than preg_replace
- Php's most precise string length truncation function
- Some powerful string processing functions forgotten by php
- Php obtains the character length of the utf8 string
- Summary of String Functions in PHP
- Php determines whether the string is full of English, pure Chinese, and a combination of Chinese and English
- PHP string escape functions (addslashes, stripslashes)
- Php converts multiple consecutive spaces in a string into one.
Why? Many people think of strlen () mb_strlen () functions first. However, in terms of program performance, these two functions are determining the string length...