Introduction to conversion between data types in PHP learning

Source: Internet
Author: User
Tags define array constant constant definition empty functions variable variable scope
Copy CodeThe code is as follows:
/* data types are converted to each other
* One is a cast
* SETTYPE (variable, type); Int,integer,float,double and so on.
* This function changes the type of the original variable, with Var_dump (), you can view the variable
*
* Use the form of (type) before assigning, 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: The integral type occupies 4 bytes in memory 2.147e9
* Floating-point type occupies 8 bytes in memory
*
*
* One is automatic conversion (the most common way), the variable will be more operational environment automatic conversion

* Some common functions related to variables and types
* Isset ()//To determine whether a variable exists, the value if NULL also indicates null.
* EMPTY ()//To determine whether a variable is empty "", null
* unset ()//Delete a variable
* SetType ()//Set a variable type
* GetType ()//Get a variable type var_dump (); Get type and value
*
* Variable type test function
* Is_bool ()//To determine whether the Boolean type
* Is_int () Is_integer () Is_long ()//To determine whether it is an integral type
* Is_float (), is_double () is_real ()//...
* Is_array ()
* Is_object ()
* Is_resource ()
* Is_null ()
* Is_scalar ()//To determine whether the scalar
* Is_numberic ()//judge whether it is any kind of number, or numeric string
* Is_callable ()//To determine whether a valid function name

* Declaration and use of constants
* 1. A constant is an identifier for a simple value
* 2. The constant definition can no longer change its value, nor can it use unset () or other functions to cancel
* 3. Constants can be defined and accessed anywhere by ignoring the rules of variable scope
* 4. Constants using define ("Constant name", value);
* 5. The constant name does not use "$" when declared and used
* 6. Constant name habits all use uppercase
* 7. The value of a constant can only be scalar type (int,float,bool,string)
* 8. Constants must be given the value at the time of declaration
* 9.defined ("constant");//To determine whether a constant exists
*
* Predefined Constants and Magic constants
* Echo __file__;//output Current file name Directory _ Magic constant
* echo case_lower;//Output Fixed value _ predefined constants
*

*/
This function changes the type of the original variable, using Var_dump (), and can view the variable
$str = "100.12345ABC";
SetType ($str, int);
Var_dump ($STR);

Use the form of (type) before assigning a value
$str = "100.12345ABC";
$a = (int) $str;
Var_dump ($a);//output int (100)
Var_dump ($STR);//output value unchanged, "100.12345ABC"
If the string does not begin with a number, it is converted to 0

Different types of operations
$a = 10;
$b = "100ABC";
$c =true;
$d = 12.34;
$sum = $a + $c;//Boolean automatically converted to 1, 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, small memory to large memory.

To determine whether it is an array
$a =array ("One", "two", 1,3,6,8);
if (Is_array ($a)) {
Print_r ($a);//print array
}else{
echo $a;
}

Defining constants, using constants
Define ("Home", "This is a Home");
$a = 100;
Function Demo ()
{
Global $a;//Because $a is a global variable, call
echo $a;
The Echo home;//constant can be accessed or defined directly, regardless of the scope
}
Demo ();

To 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.