Home > Article > PHP Framework > What is the code of i method in thinkphp
The code for the i method in thinkphp is "I('Variable type.Variable name',['Default value'],['Filter method'],['Additional data source'])"; i method The name comes from "input", which means input. It is used to obtain system input variables more conveniently and safely. If necessary, the variable values can also be filtered and forced to convert.
The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.
is a new member of ThinkPHP’s many single-letter functions. Its name comes from the English Input (input), which is mainly used for more convenient and safe acquisition. System input variables can be used anywhere. The usage format is as follows:
I(‘变量类型.变量名/修饰符’,[‘默认值’],[‘过滤方法或正则’],[‘额外数据源’])
The variable type refers to the request method or input type, including:
Note: Variable types are not case-sensitive.
Variable names are strictly case-sensitive.
Default value and filtering method are optional parameters.
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']
Supports default values:
echo I('get.id',0); // 如果不存在$_GET['id'] 则返回0 echo I('get.name',''); // 如果不存在$_GET['name'] 则返回空字符串
Use 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 post or other input type variables, 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 type is POST Or PUT, then it is equivalent to getting $_POST['id'] or PUT parameter id.
And the param type variable can also use numeric 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 be simplified to:
I('id'); // 等同于 I('param.id') I('name'); // 等同于 I('param.name')
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What is the code of i method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!