In PHP, we usually use HTTP protocol to transmit data. When we use HTTP protocol to transmit data, GET and POST are the two most commonly used methods. The GET method is used to request a specific resource from the server, while the POST method is used to submit data to the server.
It is very common to use the GET method to pass data in PHP, which uses a URL to pass data. When we need to pass one or more variables to the server, we can append these variables to the URL and use a question mark to separate them from the URL. For example, we can pass the variables "name" and "age" to the server using the following code:
$name = "John"; $age = 30; $url = "http://example.com/script.php?name=".$name."&age=".$age;
In this example, we append the variables "name" and "age" to the URL and use the characters The string concatenation operator "." combines them with the string "http://example.com/script.php?" Finally, we get a complete URL that can be accessed in the browser or passed to the server through other means.
In addition to appending variables to the URL, we can also use PHP's $_GET array to access the data passed through the GET method. When we pass data through the GET method, PHP will store these variables in the $_GET array and use them as keys by name. For example, if we pass the variables "name" and "age" to the server via the following URL:
http://example.com/script.php?name=John&age=30
Then in our PHP script, we can access these variables using the following code:
$name = $_GET["name"]; $age = $_GET["age"];
In this example, we use the $_GET array to access the variables "name" and "age" and store them in the variables $name and $age respectively.
So, can we use the GET method to pass an array? The answer is yes. In PHP we can convert an array to a URL encoded string and append it to the URL. Then, on the server side, we can use PHP's parse_str function to convert the URL-encoded string back into an array.
The following is a simple example that demonstrates how to pass an array using the GET method:
$data = array("name" => "John", "age" => 30, "city" => "New York"); $url = "http://example.com/script.php?data=".urlencode(http_build_query($data));
In this example, we create an array named $data and append it to in the URL. We used PHP's http_build_query function to convert the array into a URL encoded string and encoded it using the urlencode function. We then append the encoded string to the URL.
In our PHP script, we can use the following code to access this array:
$data = array(); parse_str($_GET["data"], $data);
In this example, we create an empty array named $data and use parse_str Function converts a URL encoded string to an array. The function stores the variable in the array we provide, so our array $data will be filled with the array passed in.
In summary, the GET method is a common method for passing data in PHP. We can append one or more variables to the URL as a query string, or we can encode an array into the URL. The encoded string is appended to the URL. Using the $_GET array, we can access the data passed through the GET method and process them on the server side.
The above is the detailed content of How to pass and obtain data in php GET method. For more information, please follow other related articles on the PHP Chinese website!