-1

I'm setting the following variables at the top of a page:

$pin = $_GET['Pin'];
$action = $_GET['action'];
$bookingref = $_GET['bookingref'];
$phone1 = $_GET['number1'];
$phone2 = $_GET['number2'];

I'm using WAMP to develop my site, and when the page loads, I get an 'Undefined Index' warning for each one.

So I thought I'll put them inside an isset, to check if they exist.

$pin = isset($_GET['Pin']);
$action = isset($_GET['action']);
$bookingref = isset($_GET['bookingref']);
$phone1 = isset($_GET['number1']);
$phone2 = isset($_GET['number2']);

But now when I try to echo the values I just get either a blank space or 1.

How can I echo the value of the variable, and keep the isset check?

Lee
  • 4,187
  • 6
  • 25
  • 71
  • 1
    Hi, can i ask that comments are left when downvotes are made? Otherwise.. it's just unhelpful. Thanks. – Lee Jun 13 '14 at 14:26
  • Can the downvoter please comment? Mods - what I can do about this? – Lee Jun 25 '14 at 10:03
  • I wouldn't get too concerned about -1. It's probably there because it's very evident you didn't RTFM which to be honest is why I haven't upvoted to fix it. http://www.php.net/manual/en/function.isset.php#refsect1-function.isset-returnvalues – Popnoodles Jun 25 '14 at 19:07
  • I don't use php.net very much at all because to me, it's just a bit too much of a confusing layout. Doesn't really easily explain most things. I tend to use W3Schools as they have a much better website of definitions. – Lee Jun 26 '14 at 10:02
  • please don't use w3schools, it can't be trusted http://www.w3fools.com but yh all you needed to do was google `php isset` to find what the function does and what it returns. – Popnoodles Jun 30 '14 at 16:22
  • Because if isset returned the value, and the value happened to be FALSE, what do you do then? What value would you want it to return when it was not set? – Jonathon Apr 10 '15 at 20:57
  • What exactly are you trying to do? Like others have pointed out `$pin = (!isset($_GET['Pin'])) ?: $_GET['Pin'];` is exactly what you are asking for, but is almost certainly not what you want to do. This line will only give you one more variable that might be set, or not, we don't know. Maybe you want to fill them with the empty string? Or some other default value? – Jonathon Apr 10 '15 at 21:01
  • I marked this as answered a long time ago. I ended using a simple if else statement setting the value as empty if it didn't exist. – Lee Apr 10 '15 at 21:15

5 Answers5

3

isset returns true or false, not the value of the variable. That's its job, that's what it says on the can. The typical idiom is:

$pin = isset($_GET['Pin']) ? $_GET['Pin'] : null;
deceze
  • 510,633
  • 85
  • 743
  • 889
3

Because you're assigning the return value of isset(), which is TRUE or FALSE.

You need to use isset() to check if they exist, then assign the variable if it does.

if(isset($_GET['Pin'])){
    $pin = $_GET['Pin']
}

Or a shorthand ternary alternative

$pin = (!isset($_GET['Pin'])) ?: $_GET['Pin'];
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
2

This is because isset() returns a boolean value (true or false), and not the value of the variable itself. There isn't a built-in way to achieve this, but you could easily mimic this behaviour with a user-defined function:

Taken from my answer here:

function ifsetor(&$value, $default = null) {
    return isset($value) ? $value : $default;
}

Now you can simply use it like this:

$pin        = ifsetor($_GET['Pin']);
$action     = ifsetor($_GET['action']);
$bookingref = ifsetor($_GET['bookingref']);
$phone1     = ifsetor($_GET['number1']);
$phone2     = ifsetor($_GET['number2']);

You can even specify a default value using the second parameter, if you want.

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I have similar functions that cut down on duplicate code but won't `ifsetor($_GET['Pin'])` cause an undefined index notice if it is not set? – Popnoodles Jun 13 '14 at 16:00
  • @Popnoodles: Nope. The variable is being passed by reference, so it will not cause a notice. – Amal Murali Jun 13 '14 at 17:48
2

isset return true or false based on variable is set or not. What you need is to do for all variables like below,

$pin = isset($_GET['Pin']) ? $_GET['Pin'] : '';

Definition: Determine if a variable is set and is not NULL

Rikesh
  • 26,156
  • 14
  • 79
  • 87
0

isset() returns a Boolean value that is True or False. It is the echo that displays "1" for true, or "" for false.

Here is I guess is the best method to use isset() is

  <?php

  $data =$_POST["firstname"];
  if(isset($data))
   {        
       trim($data);
       echo $data;
   }
 else
  {
       $data="";
  }


  ?> 
user2864740
  • 60,010
  • 15
  • 145
  • 220
MixedVeg
  • 319
  • 2
  • 15