Conversion between data types in php learning. The copy code is as follows: * conversion between data types * forced conversion * setType (variable, type); int, integer, float, double, and so on. * This function changes the type of the original variable.
The code is as follows:
/* 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.
// Different types of operations
$ A = 10;
$ B = "100abc ";
$ C = true;
USD 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 ($ )){
Print_r ($ a); // Print the array
} Else {
Echo $;
}
// Define a constant and use a constant
Define ("home", "this is a home ");
$ A = 100;
Function demo ()
{
Global $ a; // because $ a is a global variable, it must be called with a global tag.
Echo $;
Echo home; // A constant can be accessed or defined directly regardless of the range.
}
Demo ();
// Determine whether a constant exists
If (defined ("home ")
{
Echo home;
}
Else
{
Define ("home", "this is a home ");
}
The http://www.bkjia.com/PHPjc/323554.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323554.htmlTechArticle code is as follows:/* mutual conversion between data types * forced conversion * setType (variable, type); // int, integer, float, double and so on. * This function changes the type of the original variable...