Several common questions about PHP

Source: Internet
Author: User
PHP several FAQs

One, PHP floating point number of a common problem

First, let's look at an example:

 
  

??? Why is the output 57? Is this a bug in PHP? A lot of people often ask such questions, in order to understand this problem, it is necessary to know the expression of floating point number.

For example, a floating-point number with a 64-bit length (double): a 1-bit sign bit (E), a 11 exponent (Q), and a 52-bit mantissa (M) are represented (altogether 64 bits).

The ① sign bit: The highest bit represents the positive or negative of the data, 0 represents a positive number, and 1 indicates a negative number.

???? ② Digit: Indicates that the data is based on a base of 2 emerges, and the exponent is represented by an offset code;

???? ③ Mantissa: A valid number that represents the fractional data.

The key point here is that decimals are represented in binary, about how decimals are represented in binary notation, as follows:

? ? ? 1. Let's start by explaining how the integer part is represented in binary notation, such as:


? is the integer divided by 2 to see its remainder until it is 0 position. The integral part calculation is very simple, so see how the decimal part calculates

? ? ? 2, the fractional part uses the binary expression computation method as follows:

??? for example: 5.6. Integer Part 5 binary is 101

???? 0.6, there is a formula: * * If <1 is 0, cardinality = cardinality, >1 is 1, cardinality = radix-1.

??? Then: 0.6*2=1.2>0? So that's 1? Cardinality =1.2-1=0.2

?????? 0.2*2=0.4<1? So that's 0? base =0.4

?????? 0.4*2=0.8<1? So it's 0? cardinality =0.8

?????? 0.8*2=1.6>1? So, 1? base is 1.6-1=0.6

? ? ? ? ? ?... ...

? ? ? ? ?? So 5.6 can be expressed as: 101.1001 ...

? ? ? 3, 0.58 for the binary representation, the value is infinitely long as follows:

The binary representation of 0.58 is basically (52 bits) is: The binary representation of 00101000111101011100001010001111010111000010100011110.57 is basically (52 bits) is: 0010001111010111000010100011110101110000101000111101

As for the 0.58 * 100 of the specific floating-point multiplication, we do not consider so thin, with vague intention to see: 0.58*100=57.999999999. Then intval, Nature is 57.

The key point of this problem is that " you seem to have a poor decimal number, which is infinite in the binary representation of the computer."

Ii. Merging of PHP arrays

? ? When it comes to array merging, we think of the Array_merge function or the array_merge_recursive function, in fact, there is a way, in this way we usually think very little, that is, "+" operator. Let's look at their usage separately:

? ? 1. Array_merge when the array is marked as an integer type:

?? We can see the subscript drop of the original array and then re-generate a new merged array in the order of the original array.

? ? 2. Array_merge when the array is labeled as a String type:

? ? We can see that the subscript of the array is preserved, but if there is a consistent subscript in the original array, the value of the subsequent array overrides the value of the preceding array.

? ? 3. Array_merge_recursive when the array is marked as an integer type:

? ? Found the same effect as array_merge.

?? 4, Array_merge_recursive when the subscript of an array is a string type:

? ? We find the subscript reserved, and if the original array has a consistent subscript, there is no case of numerical coverage. Instead, an array is generated to hold these values.

? 5. When the "+" operator is used:

??? We can tell whether the subscript type is a number or a string, and if there is a consistent subscript in the original array, then the data behind the array is not stored. In fact, the function of "+" is to complement the functions of the previous array of the subscript is not yet appear in the data from the back of the array to fill the surface.

  • Related Article

    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.