In thinkphp, the name of the I method comes from "Input", which is used to obtain system input variables more safely and conveniently. The syntax is "I('Variable type.Variable name',['Default value' ],['Filter method'])"; where the variable type is not case-sensitive, but the variable name is strictly case-sensitive.
The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.
ThinkPHP’s I method is a new member among many single-letter functions. Its name comes from the English Input (input), and is mainly used for more convenience and security. Get system input variables, which can be used anywhere. The usage format is as follows:
I('Variable type.Variable name',['Default value'],['Filter method']) The variable type refers to the request mode or input type.
The meaning of each variable type is as follows: Variable type meaning
get gets the GET parameters
post gets the POST parameters
param automatically determines the request type to obtain GET, POST or PUT parameters
request obtains REQUEST parameters
put gets PUT parameters
session gets $_SESSION parameters
cookie gets $_COOKIE parameters
server gets $_SERVER parameters
globals gets $GLOBALS parameters
Note: Variable types are not case-sensitive.
Variable names are strictly case-sensitive.
Default value and filtering method are optional parameters.
1. Usage:
We take the GET variable type as an example to illustrate the use of the I method:
echo I('get.id'); // 相当于 $_GET['id'] echo I('get.name'); // 相当于 $_GET['name']
Support default value:
echo I('get.id',0); // 如果不存在$_GET['id'] 则返回0 echo I('get.name',''); // 如果不存在$_GET['name'] 则返回空字符串
Adopt Method filtering:
echo I('get.name','','htmlspecialchars'); // 采用htmlspecialchars方法对$_GET['name'] 进行过滤,如果不存在则返回空字符串
supports directly obtaining the entire variable type, for example:
I('get.'); // 获取整个$_GET 数组
In the same way, we can obtain variables of post or other input types, for example:
I('post.name','','htmlspecialchars'); // 采用htmlspecialchars方法对$_POST['name'] 进行过滤,如果不存在则返回空字符串 I('session.user_id',0); // 获取$_SESSION['user_id'] 如果不存在则默认为0 I('cookie.'); // 获取整个 $_COOKIE 数组 I('server.REQUEST_METHOD'); // 获取 $_SERVER['REQUEST_METHOD']
param variable type is a framework-specific variable acquisition method that supports automatic determination of the current request type. For example:
echo I('param.id');
If the current request type is GET, it is equivalent to $_GET['id']. If the current request If the type is POST or PUT, then it is equivalent to getting $_POST['id'] or PUT parameter id.
And param type variables can also use numerical index to obtain URL parameters (the PATHINFO mode parameter must be valid, whether it is GET or POST), for example:
The current access URL address Is
http://serverName/index.php/New/2013/06/01
Then we can pass
echo I('param.1'); // 输出2013 echo I('param.2'); // 输出06 echo I('param.3'); // 输出01
In fact, the writing method of param variable type can Simplified to:
I('id'); // 等同于 I('param.id') I('name'); // 等同于 I('param.name')
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use the I method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!