Strpos logic errors that are common in php: php Tutorial

Source: Internet
Author: User
Strpos logic errors that are common in php
  • Let's take a look at the usage of strpos:


  • (PHP 4, PHP 5, PHP 7)

    Strpos: Locate the first occurrence of a string

    Mixed strpos (string $ haystack, mixed $ needle [, int $ offset = 0])

    Returns the position of the needle that exists at the start of the haystack string (independent of offset ). Note that the string position starts from 0, not from 1.

    If no needle is foundFALSE. (Note the returned values in this case)


    2. instance description (error code that causes the problem)

     

    It is easy for beginners to judge the true value of if. because the function does not find the corresponding character, the returned value is false, therefore, this kind of judgment may occur to deal with the problems found and not found. The result of this example shows that the character 'T' actually exists and is also found, and the position value of the character is returned, that is, 0, because 0 is a false value, the program executes the code in else, causing inconsistency between the program running result and the expected result, resulting in a so-called logical error.


    3. correct code (solution)

     

    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.