Check If URL Contains Query String with PHP

We can easy to check whether a URL contains query string or not with PHP by using parse_url() function with PHP_URL_QUERY as a component argument:

test1.php
<?php

$url = 'http://your-domain.com/post?id=1&title=hello-world';

if (parse_url($url, PHP_URL_QUERY)) {
	echo 'has query string';	
} else {
	echo 'no query string';
}

// Output: has query string

or checking $_GET if you are in current request:

test2.php
<?php

if ($_GET) {
	echo 'has query string';	
} else {
	echo 'no query string';
}

// Output: has query string

guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Frank
Frank
4 years ago

It works for me. Thanks for the post.

1
0
Would love your thoughts, please comment.x
()
x