⊗ppPmFmGQ 314 of 447 menu

GET Requests in PHP

You already know that when submitting a form using the GET method, form data appears in the browser's address bar after the ? sign. This data will be available in the PHP code in the array $_GET.

In fact, having a form on the page is not mandatory - we can simply manually type a question mark in the address bar, list parameters with their values after it, and press enter.

In this case, the data we entered will also be available in the array $_GET. That is, it will be an imitation of form submission. Such an imitation is called sending a GET request. These words mean that we should manually type a question mark and request parameters into the address bar.

Request parameters are listed in the following format: name, then an equals sign, then the parameter value. If there are several parameters, then they are separated by an ampersand sign &.

Let's try with examples. Suppose you have a certain PHP file. Access it in the browser, as you usually do. And then add ?par1=1 to the end of the address bar and press enter.

As a result, our parameter will be contained in $_GET['par1']:

<?php echo $_GET['par1']; // will output '1' ?>

Now let's send not one parameter, but two. To do this, add this to the end of the address bar: ?par1=1&par2=2 and press enter. Here is the result:

<?php var_dump($_GET); // ['par1' => '1', 'par2' => '2'] ?>

Send a number using a GET request. Display it on the screen.

Send a number using a GET request. Display the square of this number on the screen.

Send two numbers using a GET request. Display the sum of these numbers on the screen.

Suppose a number is sent via a GET request. Make it so that if the number 1 is passed, the word 'hello' is displayed, and if 2, then the word 'bye'.

Given an array:

<?php $arr = ['a', 'b', 'c', 'd', 'e']; ?>

Suppose a number can be passed via a GET request. Make it so that the array element with the number passed in the request is displayed on the screen.

byenru