ZeroSuf3r
ZeroSuf3r

Reputation: 2001

php address request

i have this request method:

if (!$_REQUEST['0'])  {
    echo "Address missing...";
    die();
} else {
    code
    code etc.
}

And address is: http://localhost/api.php?0=address

If I open: api.php?0=0 I should get another error which says address doesn't correct. But I get Address missing... In my opinion something wrong with:

if (!$_REQUEST['0']) 

Any ideas?

Upvotes: 1

Views: 285

Answers (4)

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

if (array_key_exists('0', $_REQUEST['0']) === true) {
    echo 'exists';
}
else {
    echo 'does not';
}

Sidenote: The best check to see if a key is set in an array is array_key_exists. Opposed to isset, this function does not return false when the value is NULL.

Sidenote 2: You should specify which superglobal to use, using $_REQUEST instead of specifying $_POST or $_GET is can be the cause of vague errors to occur.

Upvotes: 0

sint
sint

Reputation: 1

  if (empty ($_REQUEST['0'])) 
  {
      echo "Address doesn't provided...";
      die();
  } else {
     code
     code etc.

Upvotes: 0

Michael J.V.
Michael J.V.

Reputation: 5609

if(!isset($_REQUEST['0']))
{
    die("Address not provided");
}
else
{
    // code
}

Upvotes: 0

Arjen
Arjen

Reputation: 1321

You're checking if $_REQUEST['0'] is false. In PHP (and many other languages), 0 stands for false. Because 0 == false, your expression results in 'true' and the echo and die() are executed.

If you are trying to test if $_REQUEST['0'] exists, you should use isset() or empty()

Upvotes: 1

Related Questions