So we continue our study again PHP basics and in this article we will get acquainted with ways of passing variables in PHP, namely with GET and POST methods. Each of them has its pros and cons, and is used in appropriate situations, which will be discussed in this article. We'll also look at code examples that demonstrate how the POST and GET methods work.

Passing Variables Using the GET Method

This variable passing method is used in PHP to pass variables to a file using address bar. That is, variables are transmitted immediately through the browser address bar. An example would be, for example, a link to an article in WordPress without using CNC (SEF), which looks something like this:

Https://site/?p=315

That is, in this case, the $p variable with the value 315 is passed. Now let's look at the GET method in more detail using an example. Let's say we need to pass three variables $a, $b and $c to the file GET method and display their sum on the screen. You can use the following code for this.

$a = $_GET["a"]; $b = $_GET["b"]; $c = $_GET["c"]; $summa = $a + $b + $c; echo "Sum $a + $b + $c = $summa";

Since all variables will be placed in global array GET(), then we first assign our variable values corresponding elements of the GET array. We do this at the very beginning to avoid various errors when passing variables. Next, to demonstrate the work, we write an arbitrary formula and display the result on the screen.

To test the GET method, simply add a question mark “?” to the file link. and through the ampersand “&” list the variables with their values. Let us have a file get.php, which lies at the root of the site. In order to transfer variables to a file, just write the following in the address bar.

Https://site/get.php?a=1&b=2&c=3

As you can see from the example, first we add a question mark immediately after the file name. Next, we register the variable and indicate its value using equals. After this, we list other variables in the same way through the ampersand. Now, when we follow this link, we will see the sum of the variables $a, $b and $c.

This method is very simple and does not require creating additional files. All necessary data comes directly through the address bar of the browser.

Well, now let's move on to the second method of passing variables in PHP - to the POST method.

Passing Variables to PHP Using the POST Method

This method allows you to secretly transfer variables from one file to another. As you already understood, two files are usually used for these purposes. The first contains a form for entering initial data, and the second contains an executive file that accepts variables. For demonstration, let's look at the following code.

Code of the first file with the form for submitting data. Let's give it the name post-1.php

  • action – specify the file to which the variables will be transferred.
  • method – method of passing variables. In our case, this is the POST method.
  • name – name of the form. At the same time, a variable with the same name will be transferred to the file.

Text fields:

  • name – variable names. In our case, this is the first and last name (name and lastname variables).
  • type – field type. In our case, this is a text field.
  • name – the name of the button and the variable that will be passed along with other variables.
  • type – button type. In our case, this is a button for sending data.
  • value – text on the button.

The code of the second file, which will serve as a variable receiver. Let's call it post-2.php

$name = $_POST; $lastname = $_POST; echo "The values ​​of the variables passed by the POST method are $name and $lastname";

As with the GET method, we first assign the values ​​of the corresponding elements to the variables global arrayPOST. Next, for clarity, we display these variables on the screen using .

Now when we load the first file, the form will load. After entering the data, click on the “Submit” button, as a result of which a page with a second file will open in a new tab, which will display the values ​​​​written in the form on the previous page. That is, the values ​​of variables from the first file will be transferred to the second file.

This concludes this article about passing variables in PHP. If you do not want to miss the appearance of other articles on the blog, I recommend subscribing to the newsletter by any in a convenient way in the “Subscription” section or use the form below.

That's all. Good luck and success in mastering the basics of PHP.


One of the most important and popular tasks in PHP is data transfer. You've probably come across a URL like this more than once site.ru/page.php?id=114841 or form with input fields text (registration, new comment, etc.). In this and the next lessons we will talk about passing data to PHP.

POST Method in PHP

The method consists of two parts: HTML with a form and fields and a PHP handler file.

Attention! To work, we just need to create accordingly only two files.

Demonstration Download sources
Let's look at an example with comments - implementing user authorization on the site:

HTML code(file post.html)



Form





Your login:

Your password:







Now more about data transfer methods. There are two types of GET (via url, open - you can change the url manually) and POST (via form, closed). The difference will be in the content of the address bar, that is, the url.

When receiving data transferred in one of these ways, the data is collected into an array of the type corresponding to the method ($_GET or $_POST). There is also an array called $_REQUEST, which can contain both $_GET and $_POST at the same time. But this is for other examples. I recommend transmitting most data using the POST method.

Now let's look at the handler code.

PHP code(in file test_reg.php)

$login = $_POST["login"]; // accept data sent by POST
$pass = $_POST["pass"]; // login and pass are the names of the input fields

If (($login == "Admin") && ($pass == "Pass"))
echo "Hello, Admin! Is it coffee or tea today?)";
else echo "You entered an incorrect login-password combination. Try again Back";
?>
You can see how we record the values ​​of the fields with the corresponding name in the $_POST array into separate variables, although it could also be collected in $_REQUEST.

In the handler, perhaps, we will work out the authorization condition - whether the login and password in the form match our fictitious one (Admin and Pass, respectively). And either we’ll say hello to the incoming person (Hello, Admin! Is it coffee or tea today?), or not (You entered an incorrect login-password combination. Try again). However, to create a full-fledged authorization, you still need to familiarize yourself with cookies, sessions, and . But more on that later.

Directly in the script, you can process the data as you wish: append, erase, encrypt, and so on. The main thing is to know the name of the variable (set in the HTML form) and then collect them using the $_POST, $_GET and $_REQUEST arrays.

Thank you for your attention!

  1. Husband (spouse)- a man in relation to the woman he is married to
  2. Wife (spouse)- a woman in relation to the man she is married to. Married woman.
  3. Father-in-law- wife's father
  4. Mother-in-law- wife's mother
  5. father-in-law- husband's father
  6. Mother in law- husband's mother
  7. brother-in-law- Brother husband
  8. Brother-in-law- brother-in-law
  9. Sister-in-law- husband's sister
  10. Brother-in-law- sister-in-law's husband
  11. sister-in-law- wife's sister
  12. Son-in-law- daughter's husband, sister's husband, sister-in-law's husband
  13. Daughter-in-law- a brother’s wife, a son’s wife for his mother, one brother’s wife in relation to another brother’s wife; also used instead of daughter-in-law, sister-in-law, sister-in-law
  14. Daughter-in-law- son's wife in relation to father
  15. Matchmaker- the father of one of the spouses in relation to the parents of the other
  16. Matchmaking- the mother of one of the spouses in relation to the parents of the other
  17. Grandfather (grandfather)- father of father or mother.
  18. Grandmother (grandmother)- mother of father or mother.
  19. great uncle- father's or mother's uncle.
  20. Great-aunt- father's or mother's aunt.
  21. Grandson, granddaughter)- son (daughter) of a daughter or son in relation to a grandfather or grandmother. Accordingly, a cousin’s grandson (granddaughter) is the son (daughter) of a nephew or niece.
  22. Great-nephew (niece)- grandson (granddaughter) of a brother or sister.
  23. Uncle (uncle, uncle)- brother of father or mother, husband of aunt.
  24. Auntie (auntie, auntie)- sister of father or mother in relation to nephews. An uncle's wife in relation to his nephews.
  25. Nephew niece)- son (daughter) of a brother or sister (siblings, cousins, second cousins). Accordingly, the child of a cousin (sister) is a cousin nephew, and a child of a second cousin (sister) is a second cousin.
  26. Fraternal (brother, sister)- having a common mother.
  27. Half-blooded (brother, sister)- having a common father, but different mothers.
  28. Stepbrothers (brother, sister)- being a brother (sister) by stepfather or stepmother.
  29. Cousin- the son of his own uncle or aunt.
  30. Cousin- daughter of a native uncle or native aunt.
  31. Second cousin- son of a great uncle or great aunt.
  32. Second cousin- daughter of a great uncle or great aunt.
  33. Godfather, godfather- godfather and mother in relation to the godson's parents and to each other.
  34. Stepfather- the mother’s husband in relation to her children from another marriage, stepfather.
  35. Stepmother- the father’s wife in relation to his children from another marriage, step-mother.
  36. Stepson- a step-son of one of the spouses who is related to the other spouse.
  37. Stepdaughter- a stepdaughter of one of the spouses who becomes the natural daughter of the other spouse.
  38. Adoptive father (mother)- adopted, adopted someone.
  39. Adopted son (daughter)- adopted, adopted by someone.
  40. Adoptive son-in-law (primak)- a son-in-law adopted into the wife's family, living in the wife's house.
  41. Widower- a man whose wife died.
  42. Widow- a woman whose husband died.
  43. Twin Cities- brothers, mostly cousins, friends who happened to help each other out in difficult times.