PHP more efficient example of character length judgments

Source: Internet
Author: User
Tags learn php vars

The function to test the length of the string in PHP is strlen, but today I find it more efficient to use isset, saying no, you don't believe me. Let me give you two examples to see.

Learn PHP, get a string in PHP has a strlen function, here and you share a in some specific circumstances to judge the length of the string is more efficient way.

Take a look at the following two code execution efficiency, you understand the implementation of code to understand the idea, the code is as follows:

  1. <?php
  2. //Use strlen mode
  3. $arr = ' 123456 ';
  4. $sTime = microtime (1);
  5. if (strlen ($arr) > 6) {
  6. //echo 1;
  7. }
  8. echo microtime (1)- $sTime;
  9. //output: 0.00035595893859863
  10. /*-------------------------------------*/ 
  11. //Use the Isset ($arr {}) mode
  12. $arr = ' 123456 ';
  13. $sTime = microtime (1);
  14. //Open source software: vcphp.com
  15. if (!isset ($arr {6})) {
  16. //echo "1RN";
  17. }
  18. echo microtime (1)- $sTime;
  19. Output: 0.00019097328186035
  20. ?>

Enterprise Template site Sharing

PHP more efficient example of character length judgments

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.