Home >Backend Development >PHP Problem >What is request in PHP
Request in PHP refers to request. It is a super global variable in PHP. It is used to collect data submitted by HTML forms and parameters in URLs. Data from GET and POST requests can be obtained at the same time. Note $_request is an associative array where the keys are the names of the form fields and the values are the values of the form fields. When using the $_request variable, user-entered data should always be validated and filtered to avoid security issues.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
Request in PHP means "request". It is a super global variable in PHP. It is used to collect data submitted by HTML forms and parameters in URLs. It can obtain GET and Data for POST request.
The following is a simple PHP code example that uses the $_REQUEST variable to get form data and print it out:
<!DOCTYPE html> <html> <body> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name"><br> Age: <input type="text" name="age"><br> <input type="submit" name="submit" value="Submit"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // 使用 $_REQUEST 变量获取表单数据 $name = $_REQUEST['name']; $age = $_REQUEST['age']; echo "Name: " . $name . "<br>"; echo "Age: " . $age; } ?> </body> </html>
In the above code, we first create a A form with two input fields (name and age) and a submit button. We then check if the user has submitted the form via the "POST" method (using $_SERVER["REQUEST_METHOD"] == "POST"). If so, we can use the $_REQUEST variable to get the form data submitted by the user.
In this case, we got the values of name and age via $name = $_REQUEST['name'] and $age = $_REQUEST['age'] and printed them out for refer to. Please note that data in both $_GET and $_POST can be obtained using the $REQUEST variable. This means that you can get data from the URL as well as from the form submission.
The above is the detailed content of What is request in PHP. For more information, please follow other related articles on the PHP Chinese website!