PHP Performance Optimizations: Isset () faster than strlen ()

Source: Internet
Author: User
Many programmers like to use strlen () Mb_strlen () These functions when judging the length of a string, although they have always been a professional test string length, but they do not know Strlen () Mb_strlen () These functions are not optimal. In fact, PHP determines the string length, using Isset () at a faster speed than strlen (), execution is more efficient. PHP determines the string length, uses isset () faster than strlen (), and performs more efficiently.

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

The strlen () function function executes quite quickly because it does not do any calculations and only returns the length of the known string stored in the Zval structure (the built-in data structure of C for storing PHP variables). However, because strlen () is a function, it is somewhat slower because function calls go through a number of steps, such as lowercase letters, hash lookups, which are executed with the called function. So in some cases, reasonable use of isset () can speed up your program. Because Isset () is a language structure, its execution does not require function lookups and lowercase letters.

Examples of string lengths are judged by Isset () and strlen () as follows:

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

Let's examine the two functions of strlen () and isset () 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 check.

Strlen () Function instance

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

The result will be output:

12

PHP isset () function

The Isset function is to detect whether a variable is set.

Syntax: bool Isset (mixed var [, mixed Var [, ...])

return value:

Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns TURE if the variable exists and the value is not NULL
Returns TRUE if each item meets the previous requirement when checking multiple variables at the same time, otherwise the result is FALSE
If a variable has been freed with unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, FALSE is returned. Also note that a null byte ("") is not equivalent to PHP's null constant.

Warning: isset () can only be used for variables, because passing any other parameter will result in parsing errors. To detect if a constant is set, use the defined () function.

The above is why Isset () speed is faster than strlen (), we can actually operate a bit.

Related Article

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.