So we continue our study PHP fundamentals and in this article we will get acquainted with the 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 method of passing variables is used in PHP to pass variables to a file using address bar. That is, the variables are transmitted immediately through the address bar of the browser. An example would be, for example, a link to an article in WordPress without the use of CNC (SEF), which looks something like this:

https://website/?p=315

That is, in this case, the $p variable with the value 315 is passed. Now let's take a closer look at the GET method 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 of $a + $b + $c = $summa";

Since all variables will be placed in before passing global array GET(), then we first assign our variable values corresponding elements of the GET array. We do this at the very beginning in order 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 operation of the GET method, simply add a question mark "?" and through the ampersand "&" list the variables with their values. Let's have a file get.php, which lies in the root of the site. In order to transfer variables to a file, it is enough to write the following in the address bar.

https://website/get.php?a=1&b=2&c=3

As you can see from the example, first we add a question mark right after the file name. Next, we prescribe a variable and specify its value through equals. After that, through the ampersand, we list other variables in the same way. Now, when we click on this link, we will see the sum of the variables $a, $b, and $c.

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

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

Passing Variables in PHP Using the POST Method

This method allows you to covertly 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 executable file that accepts variables. To demonstrate, let's look at the following code.

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

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

Text fields:

  • name - variable names. In our case, this is the name and surname (variables name and lastname).
  • 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 submitting data.
  • value - text on the button.

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

$name = $_POST; $lastname = $_POST; echo "Values ​​of variables passed by POST method - $name and $lastname";

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

Now, when the first file is loaded, the form will load. After entering the data, click on the "Send" button, as a result of which a page with the 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 the variables from the first file will be transferred to the second file.

This concludes this article on 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 in any convenient way in the “Subscription” section or using 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 have probably come across a url like site.ru/page.php?id=114841 or form with input fields text (registration, new comment etc). In this lesson and the next one, we'll 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! For work, it is enough for us to create, respectively just two files.

Demonstration Download sources
Consider an example with comments - the implementation of user authorization on the site:

HTML Code(post.html file)



The form





Your login:

Your password:





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

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

Now let's look at the handler code.

PHP code(in file test_reg.php)

$login = $_POST["login"]; // receive data sent by POST
$pass = $_POST["pass"]; // login and pass are the name 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 write the value 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 with our fictitious one (respectively, Admin and Pass). And either we will say hello to the incoming one (Hello, Admin! Today is coffee or tea?), Or not (You entered the wrong 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 data as you like: 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 (husband)- a man in relation to a woman with whom he is married
  2. Wife (wife)- a woman in relation to a man with whom she is married. 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- the wife of a brother, the wife of a son for his mother, the wife of one brother in relation to the wife of another brother; 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. Svatya- the mother of one of the spouses in relation to the parents of the other
  17. grandfather (grandfather)- the father of the father or mother.
  18. Grandmother (grandmother)- the mother of the father or mother.
  19. Great Uncle- father's or mother's uncle
  20. Great-aunt- mother's or father's aunt
  21. Grandson, granddaughter)- son (daughter) of a daughter or son in relation to a grandfather or grandmother. Accordingly, a cousin (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)- the brother of the father or mother, the husband of the aunt.
  24. Aunt (aunt, aunt)- the sister of the father or mother in relation to the nephews. Uncle's wife in relation to his nephews.
  25. Nephew niece)- son (daughter) of a brother or sister (relatives, cousins, second cousins). Accordingly, the child of a cousin (sister) is a cousin, a second cousin (sister) is a second cousin.
  26. One womb (brother, sister)- having a common mother.
  27. Half-blooded (brother, sister) having a common father but different mothers.
  28. Consolidated (brother, sister)- being a brother (sister) by stepfather or stepmother.
  29. Cousin- the son of a native uncle or native aunt.
  30. Cousin- the daughter of a native uncle or native aunt.
  31. Second cousin- the son of a great-uncle or great-aunt.
  32. Second cousin- the daughter of a great-uncle or great-aunt.
  33. Kum, kuma- godfather and mother in relation to the parents of the godson and to each other.
  34. Stepfather- mother's husband in relation to her children from another marriage, stepfather.
  35. Stepmother- father's wife in relation to his children from another marriage, stepmother.
  36. Stepson- a non-native son of one of the spouses, who is related to the other spouse.
  37. Stepdaughter- a step-daughter of one of the spouses, coming native to the other spouse.
  38. Adoptive father (mother)- adopting, adopting someone.
  39. Adopted son (daughter)- adopted, adopted by someone.
  40. Adoptive son-in-law (primak)- son-in-law adopted into the wife's family, living in the wife's house.
  41. Widower A man whose wife has died.
  42. Widow A woman whose husband has died.
  43. twin cities- brothers, mostly cousins, friends who happened to help each other out in difficult times.