Home  >  Article  >  Backend Development  >  Function in php to get user input

Function in php to get user input

下次还敢
下次还敢Original
2024-04-27 13:39:24415browse

PHP provides a variety of functions for obtaining user input: 1. $_GET: Get data from the URL query string; 2. $_POST: Get data from the HTTP request body; 3. $_REQUEST: Combine $_GET and $_POST, handles various HTTP requests; 4. readfile(): Read data from the file; 5. stream_get_contents(): Get data from the file pointer or URL; 6. fgets() / freadline(): Read from the file pointer Get a row of data; 7. parse_str(): parse the query string.

Function in php to get user input

PHP functions to obtain user input

PHP provides a variety of functions to obtain user input, mainly including the following Several types:

1. $_GET

Gets data from the query string of the URL for GET requests. For example:

<code class="php"><?php
$name = $_GET["name"];
?></code>

2. $_POST

Gets data from the HTTP request body for POST requests. For example:

<code class="php"><?php
$email = $_POST["email"];
?></code>

3. $_REQUEST

merges $_GET and $_POST and can be used to handle various HTTP requests. For example:

<code class="php"><?php
$username = $_REQUEST["username"];
?></code>

4. readfile()

Read data from the file. For example:

<code class="php"><?php
$data = readfile("input.txt");
?></code>

5. stream_get_contents()

Get data from file pointer or URL. For example:

<code class="php"><?php
$handle = fopen("input.txt", "r");
$data = stream_get_contents($handle);
?></code>

6. fgets() / freadline()

Read a line of data from the file pointer. For example:

<code class="php"><?php
$handle = fopen("input.txt", "r");
$line = fgets($handle);
?></code>

7. parse_str()

Parse the query string into an array of key-value pairs. For example:

<code class="php"><?php
$data = "name=John&email=john@example.com";
parse_str($data, $params);
?></code>

The above is the detailed content of Function in php to get user input. 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