1

I want to check the GET variables are not empty, I tried ways but they didn't work.

So I had the code like this:

$u = isset($_GET["u"]);
$p = isset($_GET["p"]);
if ($u !== "" && $p !== "") {
    //something
} else {
    //do something
}

The I checked the code by sending create.php?u=&p=, but the code didn't work. It kept running the //do something part. The I tried:

echo $u;
echo $p;

It returned 1 and 1. Then I changed it to:

if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "") {
    //something
} else {
    //do something
}

But it continued to run //do something.

Please help.

2
  • isset returns a bool so it will never == "" Commented May 17, 2014 at 7:54
  • $u returns true or false Commented May 17, 2014 at 7:54

5 Answers 5

7

You can just use empty which is a PHP function. It will automatically check if it exists and whether there is any data in it:

if(empty($var))
{
    // This variable is either not set or has nothing in it.
}

In your case, as you want to check AGAINST it being empty you can use:

if (!empty($u) && !empty($p))
{
    // You can continue...
}

Edit: Additionally the comparison !== will check for not equal to AND of the same type. While in this case GET/POST data are strings, so the use is correct (comparing to an empty string), be careful when using this. The normal PHP comparison for not equal to is !=.

Additional Edit: Actually, (amusingly) it is. Had you used a != to do the comparison, it would have worked. As the == and != operators perform a loose comparison, false == "" returns true - hence your if statement code of ($u != "" && $p != "") would have worked the way you expected.

<?php 
$var1=false;
$var2="";
$var3=0;

echo ($var1!=$var2)? "Not Equal" : "Equal";
echo ($var1!==$var2)? "Not Equal" : "Equal";

echo ($var1!=$var3)? "Not Equal" : "Equal";
echo ($var1!==$var3)? "Not Equal" : "Equal";

print_r($var1);
print_r($var2);
?>

// Output: Equal
// Output: Not Equal

// Output: Equal
// Output: Not Equal

Final edit: Change your condition in your if statement to:

if ($u != "" && $p != "")

It will work as you expected, it won't be the best way of doing it (nor the shortest) but it will work the way you intended.

Really the Final Edit:

Consider the following:

$u = isset($_GET["u"]); // Assuming GET is set, $u == TRUE
$p = isset($_GET["p"]); // Assuming GET is not set, $p == FALSE

Strict Comparisons:

if ($u !== "") 
// (TRUE !== "" - is not met. Strict Comparison used - As expected)

if ($p !== "")
// (FALSE !== "" - is not met. Strict Comparison used - Not as expected)

While the Loose Comparisons:

if ($u != "") 
// (TRUE != "" - is not met. Loose Comparison used - As expected)

if ($p != "")
// (FALSE != "" - is met. Loose Comparison used)
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your answer, +1, but ;( i can't accept your answer because the error was not because of the !==. how about if you had it fixed, i will accept your answer? ;)
@DanielCheung No thank you, I love to get into the nitty-gritty of code, so spending a few moments to really think about how things work (while sipping a nice glass of red) was the perfect way to relax this evening :)
actually, my problem was $u = isset($_GET["u"]);
@DanielCheung PHP loose comparisons allow the comparison of boolean types to many things quite well. Have a read of the (really final lol) addition to my answer.
2

You need !empty()

if (!empty($_GET["p"]) && !empty($_GET["u"])) {
    //something
} else {
    //do something
}

Helpful Link

Comments

1
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "")
  1. why are you using "!==" and not "!=".
  2. to always simplify your problem solve the logic on paper once using the runtime $u and $p value.

To check if $_GET value is blank or not you can use 2 methods.

  1. since $_GET is an array you can use if(count($_GET)) if you have only u and p to check or check all incoming $_GET parameters.

  2. empty function @Fluffeh referred to.

  3. if($_GET['u']!=""&&$_GET['p']!="")

Hope it helps thx

Comments

0

In you code you should correctly check the variable existence like

if ($u != NULL && $p != NULL && $u != 0 && $p != 0) {
    //something
} else {
    //do something
} 

Comments

0

Wow! I was so dumb... isset returns a boolean. I fixed my problem now. Thank you for answering anyway :)

This fixes:

$u = $_GET["u"];
$p = $_GET["p"];

1 Comment

Please do read my answer though, it is the better way of doing it :)

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.