Code for conversion between php Data types

Source: Internet
Author: User
1. convert a PHP string to a string in the original format. convert a string to a number. the converted number is a numeric string starting from the string, A numeric string contains positive and negative values and numbers expressed in scientific notation. [Example] Repeat the following code...

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.
[Example]
Copy the following code to the clipboard.

 


• 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
Convert the white space character "" and string "0" to false
Array () without members is converted to false
Convert 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 ).
[Example]

Copy the following PHP code to the clipboard.

 


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.

/* Data types are converted to each other
* Forced conversion
* SetType (variable, type); // int, integer, float, double, and so on.
* This function changes the type of the original variable. you can use var_dump () to view the variable.
*
* The format (type) is used before the value assignment. the type of the original variable is not changed.
* $ A = (int) "123abc ";
*
* $ Variable = intval (variable or value );
* $ Variable = floatval (variable or value );
* $ Variable = stringval (variable or value );
*
* Note: the integer occupies 4 bytes in the memory 2.147e9.
* The floating point type occupies 8 bytes in the memory.
*
*
* One is automatic conversion (the most commonly used method). variables are automatically converted in a more 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 () // determines 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. constants are used in uppercase.
* 7. constant values 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 the name of the current FILE. Directory _ Magic constant
* Echo CASE_LOWER; // output fixed value _ pre-defined constant
*

*/
// This function changes the type of the original variable. you can use var_dump () to view the variable.
$ Str = "100.12345abc ";
SetType ($ str, int );
Var_dump ($ str );

// Use the (type) format before the value assignment
$ Str = "100.12345abc ";
$ A = (int) $ str;
Var_dump ($ a); // output int (100)
Var_dump ($ str); // The output value remains unchanged, "100.12345abc"
// If the string does not start with a number, it is converted to 0.

// Operations of different types $ a = 10; $ B = "100abc"; $ c = true; $ d = 12.34; $ sum = $ a + $ c; // The Boolean type is automatically converted to 1 and the result is 11 $ sum = $ a + $ B; // The result is 110 $ sum = $ a + $ B + $ c; // The result is 111 $ 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.

// Determine whether it is an array

$ A = array ("one", "two", 1, 3, 6, 8); if (is_array ($ a) {print_r ($ ); // Print the array} else {echo $ ;}

// Define a constant and use a constant

Define ("home", "this is a home"); $ a = 100; function demo () {global $; // because $ a is a global variable, use the global tag to call echo $ a; echo home; // The constant can directly access or define the demo ();

// Determine whether a constant exists

if(defined("home"){ echo home;}else{ define("home","this is a home");}

Permanent link:

Reprint at will! Include the article address.

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.