How to correctly implement PHP string conversion to numeric requirements _php Tutorial

Source: Internet
Author: User
When a string is evaluated as a number, the type and value of the result are determined according to the following rules.

If any one of the characters is included in ".", "E" or "E", the string is evaluated as a float. Otherwise, it is treated as an integer.

The value is determined by the first part of the string. Fei Fei, ASP Technology Park If the string starts with a valid numeric data, the number is used as its value, otherwise its value is 0 (0). Valid numeric data starts with an optional sign, followed by one or more digits (optionally including a decimal fraction) followed by an optional exponent. Fei Fei asp! Technology Park Index is an "E" or "E" followed by one or more numbers.

 
 
  1. < ? PHP
  2. $ Foo = 1 + "10.5";
    $foo is float (11.5)
  3. $ Foo = 1 + " -1.3e3";
    $foo is float (-1299)
  4. $ Foo = 1 + "bob-1.3e3";
    $foo is integer (1)
  5. $ Foo = 1 + "BOB3";
    $foo is integer (1)
  6. $ Foo = 1 + "Ten Small Pigs";
    $foo is integer (11)
  7. $ Foo = 4 + "10.2 Little piggies";
    $foo is float (14.2)
  8. $ Foo = "10.0 Pigs" + 1;
    $foo is float (11)
  9. $ Foo = "10.0 Pigs" + 1.0;
    $foo is float (11)
  10. ?>

If you want to test any of the PHP strings in this section into numeric examples, you can copy and paste the examples and add the following line to see what happens.

 
  
  
  1. < ? PHP
  2. echo "$ Foo = = $foo; Type is ".
    GetType ($foo). "<br/>n";
  3. ?>

Do not expect to be able to encode a character when converting it to an integer (which may also be done in C). Use the Ord () and Chr () functions if you want to convert PHP strings to numeric values between character encodings and characters.


http://www.bkjia.com/PHPjc/446222.html www.bkjia.com true http://www.bkjia.com/PHPjc/446222.html techarticle When a string is evaluated as a number, the type and value of the result are determined according to the following rules. If you include., E, or e any one of the characters, the string is treated as a float ...

  • 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.