Php performance optimization: Using isset () to determine the string length is faster than strlen ()

Source: Internet
Author: User

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, 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

<?phpecho strlen("Hello world!");?>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.