0

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.

Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63

5 Answers5

6

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)
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • 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? ;) – Daniel Cheung May 17 '14 at 08:11
  • @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 :) – Fluffeh May 17 '14 at 08:48
  • actually, my problem was `$u = isset($_GET["u"]);` – Daniel Cheung May 17 '14 at 08:48
  • @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. – Fluffeh May 17 '14 at 08:56
2

You need !empty()

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

Helpful Link

Community
  • 1
  • 1
Rikesh
  • 26,156
  • 14
  • 79
  • 87
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

Ahmad
  • 437
  • 1
  • 4
  • 12
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
} 
AlexB
  • 2,164
  • 6
  • 27
  • 61
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"];
Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63