Cases cast function
Use the function settype(mixed var, string type)
There are 3 more specific conversion functions:
intval() //Convert integer
floatval()//Convert floating point type
strval() //Convert string
< php
$str=“123.9abc”;
$i=intval($str); //Convert to plastic
$flo=floatval($str); //Convert to floating point
$str=strval($flo); //Convert to string
>
Common functions about variables
1、var_dump(mixed var):
View the value and type of a variable or expression.
2、gettype (mixed var):
Check the type of a variable.
3、is_xxxtype(mixed var):
Check if it is of some type. Format: is_int(), is_string(), is_null()*
4、settype(mixed var, string type):
Cast a variable to a certain type. You can also use casting on it, which will affect the converted value.
Details of type conversion
1. Numbers (floating point numbers and integer types can be converted to each other)
Integers are converted to floating point types: Since the accuracy range of floating point types is much larger than that of integers, the converted accuracy will not change.
Floating point type is converted into integer type: the decimal part will be automatically discarded and only the integer part will be retained. If a floating point number exceeds the valid range of an integer number, the result will be uncertain.
The maximum value of the integer is approximately 2.147e9.
< php
$real_num=3.1e9;
echo $real_num;
echo (int)$real_num; //Output an uncertain value.
>
Because the overflowing part has been lost, an exact original result cannot be given.
2. Boolean and NULL
A boolean variable can be converted into an integer (previous)
Convert the NULL value to a string, the empty character ""
String
A string can be converted to a number. The converted number is a numeric string from the beginning of the string. Numerical strings include numbers expressed in scientific notation.
< php
$number=intval("5.6abc"); //intval gets the integer value of the variable
$number=(float)”+5.6abc”; +- is positive and negative
$number=floatval(“-1.2e3f4g5”);
$result=“12.3xy45”-6; =6.3
$result=“xy1234”/5;=0
$result=“1.2.3.4”*5;=6
$result=1+”-1.3e3”;=-1299
>
The array and object are converted into strings and the result will be "Array" and "Object".
Resource-based data is converted into strings. (Resource id#1)
$fp=fopen(“foo”, “w”);
echo get_resource_type($fp);//Output file a file’s resource.
Array
Convert a boolean value, number, or string into array data
Will get an array of data elements of the above type
Convert NULL into an array and you will get an empty array
Convert the object into an array, and the element of the resulting array is the object's attribute, and its key name is the member variable name.
External variables of PHP
One of the characteristics of PHP is that it can process form data in a simple way and obtain environment variables. In PHP, these external data or variables are stored in a special global array.
Form data
$_POST[“name”];
$_GET[“name”];
$_REQUEST["name"]; The above two things have been waiting for
print_r($_POST); print out the value of the external variable
Environment variables
Use $_ENV and $_SERVER in PHP to get the system's environment variables.
These environment variables contain some configuration information of the WEB server and some status information of the browser.
Use the function phpinfo() to return more environment information, which contains the contents of $_ENV and $_SERVER.
The program can set different output effects according to different environments, such as browser type, etc.
Expressions
An expression is a combination of variables, constants, and operator notation.
Expressions are important content in PHP, and almost everything in PHP can be called expressions.
$x=10;
$y=($x=10);
$y+=($x=10);
$z=&$x;
$condition $true_item:false_item;
condition? true and false
Quote
Reference operator & can be used in associative assignments, like an alias, such that all variables point to the same address in memory.
< php
$a=5;
$b=&$a;
echo $b; //Output 5
$a=7;
echo $b //Output 7
>
Reset the association between the variable and memory by unset($a) // After the association, the values of the two variables are equal.
Previous page12Read the full text