Conversion code between data types learned by php

Source: Internet
Author: User
The conversion code between data types learned by php mainly solves the mutual conversion between some data types in php.

The conversion code between data types learned by php mainly solves the mutual conversion between some data types in php.

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 ");
}

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.