Home  >  Article  >  Backend Development  >  How to get parameters in url in PHP

How to get parameters in url in PHP

藏色散人
藏色散人Original
2018-11-30 17:31:5425123browse

How PHP gets the parameters in the url: First create a PHP sample file; then get the specified url parameters through the "parse_url($url);" method.

How to get parameters in url in PHP

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

php Obtaining the parameter information in the url is one of the common test points in the PHP interview process. It is also a function that often needs to be implemented in our daily project development process. We can use parse_url and other related functions in PHP to accomplish.

So for PHP novices, how to implement it may be difficult, but it is actually very simple.

Let's use a simple example to introduce how PHP gets the parameters in the url.

The code example is as follows:

<?php
$url = &#39;http://www.php.cn/vip.html&#39;;
$url=parse_url($url);
echo &#39;协议 : &#39;.$url[&#39;scheme&#39;]."<br>";
echo &#39;主机 : &#39;.$url[&#39;host&#39;]."<br>";
echo &#39;路径 : &#39;.$url[&#39;path&#39;]."<br>";

The result of obtaining the url parameters is as shown below:

How to get parameters in url in PHP

As shown in the figure, we obtained http://www.php.cn/vip.htmlAll parameters of this url, such as protocol, host, and path.

For example, we get the parameters of the URL http://www.php.cn/jishu/php/413303.html:

How to get parameters in url in PHP

where parse_url functionParses the URL and returns its components.

Note: For severely unqualified URLs, parse_url() may return FALSE.

This article is an introduction to the method of obtaining the parameters in the URL in PHP. It is very simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to get parameters in url in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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