Home  >  Article  >  PHP Framework  >  What is the code of i method in thinkphp

What is the code of i method in thinkphp

WBOY
WBOYOriginal
2022-04-06 10:53:412380browse

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.

What is the code of i method in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

What is the code of the i method in thinkphp

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:

What is the code of i method in thinkphp

Note: Variable types are not case-sensitive.

Variable names are strictly case-sensitive.

Default value and filtering method are optional parameters.

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']

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn