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")){
...
}
5
  • Take a look at php.net/manual/en/function.empty.php Commented Jan 3, 2016 at 8:41
  • 1
    use isset() or empty() Commented Jan 3, 2016 at 8:41
  • You can check if form data is posted or not if($_GET) (replace it by $_POST if you're using post method, I believe that is what you're asking ? Commented Jan 3, 2016 at 8:44
  • 1
    that's the problem, my architecture looks wrong because I dont wanna do if (isset[param1] && .....isset(param[n]).... Commented Jan 3, 2016 at 8:44
  • is your goal to check if the query string is completely empty? So that there are no $_GET variables at all ? Commented Jan 3, 2016 at 11:35

1 Answer 1

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';
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for your answer, but is there some function that returns empty() for ANY variable of the query string?
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 :)
@wp42 upto my knowledge no such functions. Thanks for ur +1. I didn't guessed this is what you meant by manual.
I think you are right, and there is no such function, so I will accept this answer.
@wp42 thanks. If I get update on this in future will update you too
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.