Home > Backend Development > PHP Tutorial > Detailed explanation of common methods of passing parameters in php

Detailed explanation of common methods of passing parameters in php

伊谢尔伦
Release: 2023-03-07 14:40:01
Original
4653 people have browsed it

There are three common methods of passing parameters in PHP: $_POST[], $_GET[], $_SESSION[], are used to obtain the form, URL and Session respectively Variables value.

1. $_POST[]global variable

Use PHP’s $_POST[]predefined Variable can get the value of the form element, the format is: $_POST[name]

Create a form form.php here, set the method attribute to POST, add a text box, Named user.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>form</title>
</head>
<body>
 <form action="index.php" method="post" name="form1">
  <input type="text" name="user" />
  <input type="submit" name="submit" value="提交" />
 </form>
</body>
</html>
Copy after login

The attribute action within the form directly specifies which page the form content is transferred to. method specifies the method of delivery. post stands for using messaging, just like how we send text messages.

Then we get the form element, the code is as follows:

<?php 
   $user = $_POST[&#39;user&#39;];       //应用$_POST[]全局变量获取表单元素中文本框的值
    echo $user;
?>
Copy after login

Description: In some PHP versions, you can call the value of the form element by directly writing $user. This is the same as php.ini The settings are related. The line of code register_globals=ON/OFF is retrieved in the php.ini file. If it is ON, it can be written directly as $user, but not vice versa. Although it is very convenient to apply the form name directly, there are certain security factors. It is generally recommended to use register_globals=OFF here.

2. $_GET[] global variable

PHP uses the $_GET[] global variable to obtain the data obtained through the GET() method. The value of the form element is in the following format: $_GET[name]

In this way, the value of the form element named name can be used directly.

Use hyperlinks to pass parameters. Many of our online operations involve clicking on hyperlinks to jump between web pages. Parameters can also be passed while clicking.

Create a form form.php here, set the method attribute to GET, add a text box, and name it user.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>form</title>
</head>
<body>
 <form action="index.php" method="get" name="form1">
  <input type="text" name="user" />
  <input type="submit" name="submit" value="提交" />
 </form>
</body>
</html>
Copy after login

The action attribute in the form directly specifies which page the form content is transferred to. method specifies the method of passing using get.

Then we get the form element, the code is as follows:

<?php 
   $user = $_GET[&#39;user&#39;];       //应用$_GET[]全局变量获取表单元素中文本框的值
    echo $user;
?>
Copy after login

Note: PHP can use $_POST[] or $_GET[] global variables to get the value of the form element. But it is worth noting that the obtained form element names differ in the case of letters. If friends neglect the capitalization of letters when writing web programs, they will not be able to obtain the value of the form element or an error message will pop up when running.

3. $_SESSION[] variable

Use the $_SESSION[] variable to get the value of the form element. Its format is: $_SESSION[name]

For example, create a form, add a text box, named user, and the code to obtain the form elements is as follows:

<?php
   $user = $_SESSION[&#39;user&#39;]
?>
Copy after login

Use $_SESSION[] to pass The variable value obtained by the parameter method can be used in any page after saving. However, this method consumes system resources. It is recommended that friends use it with caution. The $_SESSION variable can be referred to the PHP Chinese website php session application code example for detailed introduction.

The above is the detailed content of Detailed explanation of common methods of passing parameters in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template