Conversion between data types for php learning

  • 2020-05-07 19:19:28
  • OfStack

 
/* Data types convert to each other  
* 1 The type is cast  
* setType( variable , type ); //int,integer,float,double And so on.  
*  This function changes the type of the original variable var_dump(); You can look at variables  
* 
*  Used before assignment ( type ) In the form of , It does not change the type of the original variable  
* $a=(int)"123abc"; 
* 
* $ variable =intval( Variables or values ); 
* $ variable =floatval( Variables or values ); 
* $ variable =stringval( Variables or values ); 
* 
*  Note: integers are occupied in memory 4 bytes  2.147e9 
*  Float in memory 8 bytes  
* 
* 
* 1 The type is automatic conversion ( The most common way ), Variables are automatically converted with the running environment  

*  Related to variables and types 1 Some common functions  
* isset();// judge 1 Is there a variable , Value if it is null It also means empty.  
* empty();// judge 1 Are all variables null or not  "",null 
* unset();// delete 1 A variable  
* setType();// Set up the 1 Types of variables  
* getType();// To obtain 1 Types of variables  var_dump(); Gets the type and value  
* 
*  Variable types test functions  
* is_bool();// Determine whether it is Boolean or not  
* is_int() is_integer() is_long()// Determine if it is an integer  
* is_float(), is_double() is_real()//... 
* is_array() 
* is_object() 
* is_resource() 
* is_null() 
* is_scalar()// Whether it's a scalar or not  
* is_numberic()// To determine whether or not 1 A string of Numbers, or Numbers  
* is_callable()// Determines if the function name is valid  

*  Declaration and use of constants  
* 1. Constant is 1 An identifier for a simple value  
* 2. A constant cannot be changed or used after it is defined unset() Or some other function cancels  
* 3. Constants can be defined and accessed anywhere, regardless of the rules of the variable scope  
* 4. Constant use define(" Constant names ", value ); 
* 5. Constant names are not used when declared or used "$" 
* 6. Constant names are always capitalized  
* 7. The value of a constant can only be of a scalar type (int,float,bool,string) 
* 8. constant 1 You have to give the value when you declare it  
* 9.defined(" constant ");// Determine whether constants exist  
* 
*  Predefined constants and magic constants  
* echo __FILE__;// Output the current file name directory _ Magic constant  
* echo CASE_LOWER;// Output a fixed value _ Predefined constant  
* 

*/ 
// This function changes the type of the original variable var_dump(); You can look at variables  
$str="100.12345abc"; 
setType($str,int); 
var_dump($str); 

// Used before assignment ( type ) In the form of  
$str="100.12345abc"; 
$a=(int)$str; 
var_dump($a);// The output int(100) 
var_dump($str);// Output value is constant ,"100.12345abc" 
// If the string does not start with a number, convert to 0 

// Different types of operations  
$a=10; 
$b="100abc"; 
$c=true; 
$d=12.34; 
$sum=$a+$c;// Boolean automatically converts to 1 , the results for 11 
$sum=$a+$b;// The results for 110 
$sum=$a+$b+$c;// The results for 111 
$sum=$a+$b+$c+$d;// The results for 123.34 , because floating point memory space, small memory to large memory.  

// Whether it's an array or not  
$a=array("one","two",1,3,6,8); 
if(is_array($a)){ 
print_r($a);// Print the array  
}else{ 
echo $a; 
} 

// Define constants , Use a constant  
define("home","this is a home"); 
$a=100; 
function demo() 
{ 
global $a;// because $a It's a global variable so it's called with a global flag  
echo $a; 
echo home;// Constants can be accessed or defined without regard to scope  
} 
demo(); 

// Determine whether constants exist  
if(defined("home") 
{ 
echo home; 
} 
else 
{ 
define("home","this is a home"); 
} 


Related articles: