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.
-
- $ Number = intval ("5.6abc"); // 5
- $ Number = (float) "+ 5.6abc"; // 5.6
- $ Number = floatval ("-1.2e3f4g5"); //-1.2e3
- $ Result = "12.3xy45"-6; // 6.3
- $ Result = "xy1234"/5; // 0
- $ Result = "1.2.3.4" * 5; // 6
- $ Result = 1 + "-1. 3"; //-1299
- ?>
• 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 ).
-
- $ Real_num = 3.1e9;
- Echo $ real_num;
- Echo (int) $ real_num; // output an uncertain value because the overflow part has been lost.
- ?>
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
- // 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 ");
- }