Home >Backend Development >PHP Tutorial >PHP performance optimization: isset() is faster than strlen()

PHP performance optimization: isset() is faster than strlen()

小云云
小云云Original
2017-11-25 09:02:022337browse

Many programmers like to use strlen() mb_strlen() functions when judging the length of strings. Although they have always been professional in testing string lengths, they don’t know that strlen() mb_strlen() functions are not the most effective. Good. In fact, when PHP determines the length of a string, using isset() is faster than strlen() and has higher execution efficiency. To determine the length of a string in PHP, using isset() is faster than strlen() and has higher execution efficiency.

So why is isset() faster than strlen()?

strlen() function executes quite quickly because it does not do any calculations and only returns the known string length stored in the zval structure (C's built-in data structure used to store PHP variables). However, since strlen() is a function, it will be somewhat slow, because the function call will go through many steps, such as lowercase letters and hash search, and will be executed together with the called function. Therefore, in some cases, reasonable use of isset() can speed up your program. Because isset() is a language construct, its execution does not require function lookup and letter lowercase, etc.

Specific examples of determining the length of a string through isset() and strlen() are as follows:

 $str='http://www.phpernote.com/php-template/436.html';    
 if(strlen($str)<5){echo "未满5";}    
if(!isset($str{5})){echo "未满5";}

Let’s analyze the two functions strlen() and isset() in detail.

PHP strlen() function

Definition and usage

strlen() function returns the length of the string.

Syntax: strlen(string)

Parameters: string
Description: Required. Specifies the string to check.

strlen() function instance

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

The result will be output:

12

PHP isset() function

isset function is detected Whether the variable is set.

Syntax: bool isset (mixed var [, mixed var [, ...]] )

Return value:

If the variable does not exist, return FALSE
If the variable exists and its value is NULL, it will also return FALSE
If the variable exists and its value is not NULL, it will return TRUE
When checking multiple variables at the same time, TRUE will be returned only when each single item meets the previous requirement. Otherwise the result is FALSE
If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("") is not equivalent to PHP's NULL constant.

Warning: isset() can only be used with variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.

The above is the reason why isset() is faster than strlen(). You can practice it.

Related recommendations:

Detailed explanation of how to use isset() and unset() functions in php

php returns the length of the string Function strlen()

PHP gets the string length function strlen() function

The above is the detailed content of PHP performance optimization: isset() is faster than strlen(). 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