Intval () and int ()

Source: Internet
Author: User

int intval (mixed $var [, int $base]) by using a specific binary conversion (default is decimal), the parameter base represents the binary, and only if Var is a string, base will make sense, which means to convert Var by base, return the variable The integer value of var.

Intval (), if the argument is a string, the integer value represented by the number string before the first character in the string that is not a number is returned. If the first string of strings is '-', the second begins. If the argument is a number of character points, the value after the rounding is returned. When Var is an array, VAR returns 0 if it is empty, and 1 is returned.

For example: Intval ("12AB3") returns 12

Intval ("ab123") returned is 0

Intval (" -1AB2") returned is-1

Intval (12.3223) =12

Intval ("100", 2) = 4 100 According to 2 is 4

Intval (Array ()) = 0

Intval (Array) = 1

=======================================

The effect of int () is the same as intval, but the efficiency seems not as high as intval

=======================================

If you need to determine whether a string is a pure number, you cannot use the Intval conversion, you must use Is_numeric ($var) and return True if Var is a pure number, otherwise false

Is_numeric ("12a") = False

Is_numeric ("123") = True

Is_numeric ("9.2") = True

=======================================

Regular expressions can also be used to determine the preg_match of a pure number(‘/^\d+$/i‘, $var)

Intval () and int ()

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.