PHP Data Type examples

Source: Internet
Author: User
Tags array definition type null
PHP supports 8 types of raw data. Four scalar types: Boolean (Boolean), Integer (integer), float (floating-point, also called Double), String (string), two composite types: Array (array), object, Finally, there are two special types: resource (Resource), NULL (no type).

Note: If you want to see the value and type of an expression, use the Var_dump () function. Use the GetType () function if you just want a readable type of expression for debugging purposes. To view a type, do not use GetType () and use the Is_type function.

PHP is a weak language, according to the program run environment automatic conversion, when using the = = number, if the comparison of a number and string or comparison of strings involved in digital content, the string will be converted to numeric values and comparisons are performed numerically. This rule also applies to switch statements. (absolute comparison please use = = =)

first operand type /p>

type of second operand

type conversion

int

floating-point

int converted to float

int

floating-point

string

string converted to floating-point

A summary is floating point > Integer > String > Boolean type

  1. Boolean type

    When converted to Boolean, the following values are considered false:

    All other values are considered TRUE (including any resources).

      • The Boolean value false itself

      • Integer value 0 (0)

      • Floating-point value 0.0 (0)

      • An empty string, and the string "0"

      • An array that does not include any elements

      • Objects that do not include any member variables (PHP 4.0 only applies)

      • Special type NULL (including variables that have not been assigned)

      • SimpleXML object generated from an empty tag

  2. Integer type

      • Integer overflow: If a given number exceeds the range of integers, it will be interpreted as float. Similarly, float is returned if the result of the operation is outside the integer range.

      • There are no divisible operators in PHP (this differs from Java). 1/2 produces a float0.5. The value can be cast to integer by discarding the fractional part, or better rounded with the round () function.

        <?phpvar_dump (25/7);         Float (3.5714285714286) var_dump ((int) (25/7)); Int (3) var_dump (round (25/7));  Float (4)?>
      • When you convert from a floating-point number to an integer, the rounding is rounded down.

      • Warning

        Never cast an unknown fraction to an integer, which can sometimes lead to unpredictable results.

        <?phpecho (int) ((0.1+0.7) * 10); Show 7!? >
  3. Float type

  4. <?php$a = 0.1; $b = 0.9; $c = 1;var_dump (($a + $b) = = $c);//truevar_dump (($c-$b) = = $a);//falseprintf ("%.20f", $a + $b); 1.00000000000000000000printf ("%.20f", $c-$b); 0.09999999999999997780?>

    This problem occurs because the floating-point calculation involves precision, which can result in loss of precision when the floating-point number is converted into binary.

  5. So never believe that the float is accurate to the last one, and never compare two floating-point numbers for equality.

  6. If you do need a higher precision, you should use any mathematical function of precision.

  7. The high-precision operation is as follows: Bcadd adds two high-precision numbers Bccomp compare two high-precision numbers, and returns -1,0,1BCP divides two high-precision numbers bcmod to find high-precision digital remainder Bcmul multiplies two high-precision numbers by Bcpow Seeking high precision digital exponentiation bcpowmod to find a high-precision digital exponentiation Bcscale Configure the default number of decimal places, equivalent to the "scale=" in the Linux BC BCSQRT the high-precision number of square root bcsub subtract two high-precision numbers
  8. As the warning message says, it is problematic to compare two floating-point numbers for reasons of internal expression. There are, however, circuitous ways to compare floating-point values.

    To test whether a floating-point number is equal, use a minimum error value that is only a little larger than the value. This value is also known as the Machine Minimum (epsilon) or the smallest element to take an integer, which is the smallest differential value that can be accepted in the calculation.

    and are equal within five digits of the decimal point.

    <?php$a = 1.23456789; $b = 1.23456780; $epsilon = 0.00001;if (ABS ($a-$b) < $epsilon) {    echo "true";}? >
  9. String type

  10. If the string is enclosed in double quotation marks ("), PHP will parse some special characters: \ n, \ \, \$

  11. The most important feature of a string defined in double quotation marks is that the variable is parsed.

  12. String with '. ' Stitching.

  13. Array

  14. An array of type PHP is actually an ordered map, which is a type that associates values to the keys.
    You can use the array () language structure to create a new array. It accepts any number of key (key) = = VALUES (value) pairs separated by commas.

  15. array (key = value, ...) Key (Key) is an integer literal or string//value (value) can be any type of value in addition key will have the following cast: <?php $arr = Array (5 =& Gt        1, 2);    $arr [] = 56; This is the same as $arr [13] = 56; At the the script $arr ["x"] = 42; This adds a new element to the array with key "X" Unset ($arr [5]);    This removes the element fromthe array unset ($arr); This deletes the whole array 
      • arrays and objects cannot be used as key names. Insisting on doing so will result in a warning: illegal offset type.

  16. The foreach control structure is used exclusively for arrays. It provides a simple way to iterate through an array.

  17. The assignment of arrays (array) always involves a copy of the value. Use the reference operator to copy an array by reference.

  18. <?php        $arr 1 = Array (2, 3);        $arr 2 = $arr 1;        $arr 2[] = 4; $arr 2 is changed,//$arr 1 is still array (2, 3)               $arr 3 = & $arr 1;        $arr 3[] = 4; Now $arr 1 and $arr 3 is the same    ?>
  19. Null
    A special NULL value indicates that a variable has no value. The only possible value for a null type is NULL.
    A variable is considered NULL in the following cases:
    1. Assigned value is NULL. 2. Has not been assigned a value. 3. Be unset ().
    Convert to null: using (unset) $var converting a variable to NULL will not delete the variable or unset its value. Only NULL values are returned.

Related recommendations:

Conversion of PHP Data type conversions

Objects that parse the PHP data type (object)

Type of string for PHP data type

PHP Data type of Boolean variable detailed

PHP Data type

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.