Current Location: Home> Latest Articles> ThinkPHP Input Method Analysis and Use Cases

ThinkPHP Input Method Analysis and Use Cases

gitbox 2025-06-29

Introduction to ThinkPHP Framework

ThinkPHP is a fast, simple, and flexible PHP development framework that adopts object-oriented programming concepts. It encapsulates database operations, URL routing, template engines, and other features, greatly improving development efficiency. In ThinkPHP, the input method is a commonly used and important tool for handling HTTP request parameters.

Common Use Cases for the input Method

Get Request Parameters

The most common use of the input method is to retrieve parameters from HTTP requests. Whether it's a GET request or a POST request, we can easily retrieve the corresponding parameter values using the input method. Here's a simple example:

// Get GET request parameter
$param = input('get.name');

echo 'GET parameter name value: ' . $param;

// Get POST request parameter
$param = input('post.age');

echo 'POST parameter age value: ' . $param;

In the above code, the input method retrieves the 'name' parameter from the GET request and the 'age' parameter from the POST request, and outputs their values. This method is particularly common when handling form data.

Set Default Values

Sometimes, parameters may not exist. In such cases, we can set a default value for the parameter. You can specify the default value as the second argument to the input method:

// Get GET request parameter, return default value 'Lucy' if name parameter is missing
$param = input('get.name', 'Lucy');

echo 'GET parameter name value: ' . $param;

As shown above, when the 'name' parameter is missing in the GET request, the value of $param is set to the default value 'Lucy'.

Supports Filter Rules

The input method also supports filtering parameters. ThinkPHP provides several built-in filter rules, such as int (convert the parameter to an integer), float (convert the parameter to a float), strip_tags (filter out HTML tags), and more. Here's an example:

// Get GET request parameter and convert it to an integer
$id = input('get.id/d');

echo 'GET parameter id value: ' . $id;

// Get POST request parameter and filter out HTML tags
$content = input('post.content', '', 'strip_tags');

echo 'POST parameter content value: ' . $content;

By using different filter rules, the input method helps developers process parameters effectively, ensuring data security and accuracy.

Supports Retrieving Array Parameters

When dealing with forms or multiple parameters, we might need to retrieve array-type parameters. ThinkPHP provides a simple method to retrieve array parameters by appending '/a' to the parameter name:

// Get multi-choice parameters from GET request
$options = input('get.options/a');

echo 'GET parameter options value: ';
print_r($options);

As shown above, we can easily retrieve the array-type parameter 'options' from the GET request and print its values.

Conclusion

This article explained the usage of the input method in ThinkPHP framework, covering how to retrieve request parameters, set default values, apply filter rules, and get array parameters. The input method simplifies development, improves code readability, and maintainability, especially when handling form data and validation. Mastering the use of the input method will significantly improve PHP development efficiency and code quality.