1

I'm designing a semi-basic tool in php, I have more experience server side, and not in php.

My tool contains 10-15 pages, and I am doing the navigation between them with the $_GET parameter.

I would like to check if the query string is empty (to know if I'm in the home page). Is there any php function for this ? Of course I can do it manually, but still?

EDIT: My question is if there is a function that replaces

if(! isset("param1") && .....&& ! isset("paramN")){
...
}

1 Answers1

2

Try below

if(isset($_GET['YOUR_VARIABLE_NAME']) && !empty($_GET['YOUR_VARIABLE_NAME'])) {

}

isset() is used to check whether there is any such variable or not

empty() to check whether the variable is not empty or not

As per your comment, assume your URL as below

http://192.168.100.68/stack/php/get.php?id=&name=&action=delete&type=category

And your PHP script as below

<?php

$qs = $_GET;
$result = '';
foreach($qs as $key=>$val){
    if(empty($val)){
        $result .= 'Query String \''.$key.'\' is empty. <br />';
    }
}

echo '<pre>'; print_r($result);
?>

In my above URL I passed id and name as empty.

Hence, Result will be like below

id is empty.
name is empty.

but I dont think its standard way.

If you want to process something only if all parameters are having some values, they you can move those process inside a if as below

if(empty($result)) {
     // YOUR PROCESS CODE GOES HERE
} else {
     echo 'Some Required Parameters are missing. Check again';
}
Anto S
  • 2,448
  • 6
  • 32
  • 50
  • Thank you for your answer, but is there some function that returns empty() for ANY variable of the query string? –  Jan 03 '16 at 09:05
  • thank you again, yes it's interesting, but I said I could do it manually, and wanted to know if there is a php function for this. gave you an upvote :) –  Jan 03 '16 at 10:13
  • @wp42 upto my knowledge no such functions. Thanks for ur +1. I didn't guessed this is what you meant by manual. – Anto S Jan 03 '16 at 10:14
  • I think you are right, and there is no such function, so I will accept this answer. –  Jan 03 '16 at 10:28
  • @wp42 thanks. If I get update on this in future will update you too – Anto S Jan 03 '16 at 10:34
  • why combining isset and empty? Php empty functions also checks whether a variable is set.... – shock_gone_wild Jan 03 '16 at 11:20
  • @shock_gone_wild refer http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty – Anto S Jan 03 '16 at 11:23
  • @anto.nishanth well... the accepted answer supports my comment? ;) – shock_gone_wild Jan 03 '16 at 11:28
  • @shock_gone_wild I didn't get you – Anto S Jan 03 '16 at 11:30
  • i want to say there is no need to combine isset() and ! empty(). the question you posted in a previous comment supports this. – shock_gone_wild Jan 03 '16 at 11:31
  • @shock_gone_wild I couldn't say correctly but in my earlier stage i faced issue when I am using only !empty() when there is no such parameter at all, May be its depends on php version – Anto S Jan 03 '16 at 11:38