Code for conversion between php Data types

Source: Internet
Author: User
1. convert a PHP string to a numeric string in the original format of the numeric value. convert a string to a numeric. the converted numeric is a numeric string starting from the string.

1. PHP string conversion

A numeric value to a string is converted to a numeric value in the original format. a string to a numeric value is a numeric string starting from the string, A numeric string contains positive and negative values and numbers expressed in scientific notation.

  1. $ Number = intval ("5.6abc"); // 5
  2. $ Number = (float) "+ 5.6abc"; // 5.6
  3. $ Number = floatval ("-1.2e3f4g5"); //-1.2e3
  4. $ Result = "12.3xy45"-6; // 6.3
  5. $ Result = "xy1234"/5; // 0
  6. $ Result = "1.2.3.4" * 5; // 6
  7. $ Result = 1 + "-1. 3"; //-1299
  8. ?>

• Convert Boolean type to string, convert false to null string "", convert true to "1"

• Convert an Array to a string, which is "Array"

• Convert an Object to a string, which is "Object"

• Resource conversion string, which is "Resource id #1"

2. PHP Boolean

Convert the following types to Boolean type false, and convert other types to true

0 to false, 0.0 to false, white space character "" and string "0" to false, array () without members to false, NULL to false

3. PHP numbers (integer and floating point types can be converted to each other)

• Integer to floating point: the precision of the converted value does not change because the precision range of the floating point is greater than that of the integer.

• Floating point type conversion to integer: automatically discard the small part and only keep the integer part. Note: If a floating point type exceeds the valid range of the integer value, the result cannot be determined (the maximum integer value is about 2.147e9 ).

  1. $ Real_num = 3.1e9;
  2. Echo $ real_num;
  3. Echo (int) $ real_num; // output an uncertain value because the overflow part has been lost.
  4. ?>

4. PHP array

Convert a boolean, number, and string to an array to obtain an array containing the data elements of this type.

• Convert NULL to an array to obtain an empty array.

• The object is converted into an array. the obtained array key is the object property name and the value is the value of the corresponding object property.

Conversion between data types

One is forced conversion.

SetType (variable, type); // int, integer, float, double, and so on.

This function changes the type of the original variable, using var_dump (); you can view the variable in the form of (type) before the value assignment, without changing the type of the original variable

$ A = (int) "123abc ";

$ Variable = intval (variable or value );

$ Variable = floatval (variable or value );

$ Variable = stringval (variable or value );

Note: integer occupies 4 bytes in memory 2.147e9

Float occupies 8 bytes in memory

One is automatic conversion (the most common method), and the variables will be automatically converted in a running environment,Common functions related to variables and types:

Isset (); // determines whether a variable exists. if the value is null, it indicates null.

Empty (); // determines whether a variable is empty. "", null

Unset (); // delete a variable

SetType (); // set a variable type

GetType (); // Get a variable type var_dump (); get the type and value

Variable type test function

Is_bool (); // determines whether it is Boolean.

Is_int () is_integer () is_long () // determines whether it is an integer.

Is_float (), is_double () is_real ()//...

Is_array ()

Is_object ()

Is_resource ()

Is_null ()

Is_scalar () // determines whether it is a scalar.

Is_numberic () // determines whether it is a number or a numeric string.

Is_callable () // Determine whether the function name is valid

Constant declaration and use

1. a constant is the identifier of a simple value.

2. after a constant is defined, its value cannot be changed, or unset () or canceled by other functions.

3. constants can be defined and accessed anywhere, regardless of the variable range rules.

4. Use define ("constant name", value) for constants );

5. constant names are not used for declaration and use "$"

6. constant names are used in uppercase.

7. the constant value can only be of the scalar type (int, float, bool, string)

8. a constant must be given a value during declaration.

9. defined ("constant"); // determines whether a constant exists.

Predefined Constants and magic constants

Echo _ FILE __; // output current FILE name Directory _ Magic constant

Echo CASE_LOWER; // output fixed value _ pre-defined constant

  1. // This function changes the type of the original variable. you can use var_dump () to view the variable.
  2. $ Str = "100.12345abc ";
  3. SetType ($ str, int );
  4. Var_dump ($ str );
  5. // Use the (type) format before the value assignment
  6. $ Str = "100.12345abc ";
  7. $ A = (int) $ str;
  8. Var_dump ($ a); // output int (100)
  9. Var_dump ($ str); // The output value remains unchanged, "100.12345abc"
  10. // If the string does not start with a number, it is converted to 0.
  11. // Different types of operations
  12. $ A = 10;
  13. $ B = "100abc ";
  14. $ C = true;
  15. USD d = 12.34;
  16. $ Sum = $ a + $ c; // The Boolean type is automatically converted to 1 and the result is 11.
  17. $ Sum = $ a + $ B; // The result is 110.
  18. $ Sum = $ a + $ B + $ c; // The result is 111.
  19. $ Sum = $ a + $ B + $ c + $ d; // The result is 123.34. because of the large floating point memory space, the small memory is converted to the large memory.
  20. // Determine whether it is an array
  21. $ A = array ("one", "two", 1, 3, 6, 8 );
  22. If (is_array ($ )){
  23. Print_r ($ a); // Print the array
  24. } Else {
  25. Echo $;
  26. }
  27. // Define a constant and use a constant
  28. Define ("home", "this is a home ");
  29. $ A = 100;
  30. Function demo ()
  31. {
  32. Global $ a; // because $ a is a global variable, it must be called with a global tag.
  33. Echo $;
  34. Echo home; // A constant can be accessed or defined directly regardless of the range.
  35. }
  36. Demo ();
  37. // Determine whether a constant exists
  38. If (defined ("home ")
  39. {
  40. Echo home;
  41. }
  42. Else
  43. {
  44. Define ("home", "this is a home ");
  45. }

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.