The difference between PHP intval () and (int) conversions ____php

Source: Internet
Author: User
Tags null null

1, Intval & (int) can not be converted to Object.

2, conversion efficiency (int) > Intval () > sprintf (Intval is the built-in method of PHP, relatively low efficiency).

3, the other exactly the same.

4, input 0123, the 0x123 (int) function will also be in the octal and 16 binary conversion.


Test code:

$n = "19.99";
$example = Array (',
    ', 19.99, ' 19.99 ', ', ' abc123 ', ' 123abc ', 0x20, ' 0x20 ', Array (' 19.99 '), ' +41 ', ' -41 ',
    ' 10 000000000000000000000000000000 ', (float) 1999, NULL, $n *100, Strval ($n *100), ' j18ugj9hu0gj5hg ', 0123

);

foreach ($example as $key => $value) {
    echo ' Transform value is: '. GetType ($value). '  . Json_encode ($value). "<br>";
    echo "Intval is:", Intval ($value). " <br> ";
    echo "(int) is:", (int) ($value). " <br> ";
    echo "--------------------------------". "<br>";
}

Run Result:

The Transform Value Is:integer 19
Intval is:19
(int) is:19
--------------------------------
The Transform Value is:string "19"
Intval is:19
(int) is:19
--------------------------------
The Transform Value is:double 19.99
Intval is:19
(int) is:19
--------------------------------
The Transform Value is:string "19.99"
Intval is:19
(int) is:19
--------------------------------
The Transform Value is:string ""
Intval is:0
(int) is:0
--------------------------------
The Transform Value is:string "abc123"
Intval is:0
(int) is:0
--------------------------------
The Transform Value is:string "123ABC"
Intval is:123
(int) is:123
--------------------------------
The Transform Value Is:integer 32
Intval is:32
(int) is:32
--------------------------------
The Transform Value is:string "0x20"
Intval is:0
(int) is:0
--------------------------------
The Transform Value is:array ["19.99"]
Intval is:1
(int) is:1
--------------------------------
The Transform Value is:string "+41"
Intval is:41
(int) is:41
--------------------------------
The Transform Value is:string "-41"
Intval is:-41
(int) is:-41
--------------------------------
The Transform Value is:string "10000000000000000000000000000000"
Intval is:2147483647
(int) is:2147483647
--------------------------------
The Transform Value is:double 1999
Intval is:1999
(int) is:1999
--------------------------------
The Transform Value is:null NULL
Intval is:0
(int) is:0
--------------------------------
The Transform Value is:double 1999
Intval is:1998
(int) is:1998
--------------------------------
The Transform Value is:string "1999"
Intval is:1999
(int) is:1999
--------------------------------
The Transform Value is:string "J18UGJ9HU0GJ5HG"
Intval is:0
(int) is:0
--------------------------------
The Transform Value Is:integer 83
Intval is:83
(int) is:83
--------------------------------











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.