1

So I'm trying to see if the user inputted anything:

$test = $_GET["restaurantName"];

if(isset($test))
{
    echo "you inputed something";
    echo "$test";
}

if(!isset($test))
{
    echo "you did not input anything";
    echo "$test";
}

die("The End");

For some reason even when I don't input anything it still passes the first if statement and says that something has been inputted even when I don't I looked up the documentation about isset() and I'm pretty sure that this is how you are supposed to use it.

2
  • 1
    Test the original variable i.e. if (isset($_GET["restaurantName"])) thats what you are interested in being set to something afterall Commented Apr 6, 2016 at 20:59
  • also i wouldn't make a habit of placing variables in a string (especially if it serves no purpose at all) echo $test; works fine Commented Apr 6, 2016 at 21:10

3 Answers 3

2

You should do it this way if you want to keep the same layout style.

if(isSet($_GET["restaurantName"])) {
     $test = $_GET["restaurantName"];
}

if(isset($test))
    {
        echo "you inputed something";
        echo "$test";
    } else { //!isset($test)
        echo "you did not input anything";
        echo "$test";
    }

Your problem is that you are setting the variable, even if the GET doesn't exist.

How I would do it personally, as it makes the code much shorter with the same outputs:

if(isSet($_GET["restaurantName"])) {
    $test = $_GET["restaurantName"];
    echo "Your input: ".$test;
} else {
    echo "No Input";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well you dont really need $test at all if you think about this logically
@RiggsFolly correct, but I like to keep shorter variable names. I wouldn't really want to type $_GET["restaurantName"] every time I needed that variable.
1

You are setting it: $test = $_GET["restaurantName"]; the isset checks whether a variable has been set, not whether the variable contained is null or empty, you could use !empty

you can also check isset($_GET["restaurantName"];) but beware even if you have the get variable in your url as ?restaurantName= than it's still set, it's just empty

Best thing to do would be to check if it's set and not an empty string:

if(isset($_GET["restaurantName"]) && $_GET["restaurantName"] != "")
{
    echo "you inputed something";
    echo $_GET["restaurantName"];
} else {
    echo "you did not input anything";
}

die("The End");

i also removed the second if, cause you can just use an else clause instead of checking twice.

Some links to read: http://php.net/manual/en/function.isset.php http://php.net/manual/en/function.empty.php

3 Comments

Beware "!empty" condition... value "0" is considered as "empty"
true, but in case of a restaurant name i don't mind not allowing 0 as a restaurant name ;) but you're right.
@Random It's a weak reason though from my side though :p i edited my answer :)
0

If this $_GET['restaurantName'] comes from a submitted (GET) form input rather than a query string in a link (which may or may not be present), it will always be set. If the user did not enter anything, it will be set to an empty string. You can check it using empty instead of isset (empty includes a check for isset).

if (!empty($_GET['restaurantName'])) {
    echo "you input " . $_GET['restaurantName'];
} else {
    echo "you did not input anything";
}

It may be a good idea to also check the trimmed entry in case it is something like ' ', which you can do with

if (!empty($_GET['restaurantName']) && trim($_GET['restaurantName'])) { ...

But that is starting to get more into form validation, which is another topic in itself.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.