Question

This below does not seem to work how I would expect it, event though $_GET['friendid'] = 55 it is returning NULL

<?PHP

$_GET['friendid'] = 55;

$friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

echo $friendid;
exit;

?>
Was it helpful?

Solution

Remove the !. You don't want to negate the expression.

$friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 'empty';

OTHER TIPS

As of PHP 7's release, you can use the null-coalescing operator (double "?") for this:

$var = $array["key"] ?? "default-value";
// which is synonymous to:
$var = isset($array["key"]) ? $array["key"] : "default-value";

In PHP 5.3+, if all you are checking on is a "truthy" value, you can use the "Elvis operator" (note that this does not check isset).

$var = $value ?: "default-value";
// which is synonymous to:
$var = $value ? $value : "default-value";

If you're lazy and risky, you can use error control operator @ and short form of ternary operator.

$friendid = @$_GET['friendid']?: 'empty';

Currently you're working with the ternary operator:

$friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

Break it down to an if-else statement and it looks like this:

if(!isset($_GET['friendid']))
   $friendid = $_GET['friendid'];
else
   $friendid = 'empty';

Look at what's really happening in the if statement:

!isset($_GET['friendid'])

Note the exclamation mark (!) in front of the isset function. It's another way to say, "the opposite of". What you're doing here is checking that there is no value already set in $_GET['friendid']. And if so, $friendid should take on that value.

But really, it would break since $_GET['friendid'] doesn't even exist. And you can't take the value of something that isn't there.

Taking it from the start, you have set a value for $_GET['friendid'], so that first if condition is now false and passes it on to the else option.

In this case, set the value of the $friendid variable to empty.

What you want is to remove the exclamation and then the value of $friendid will take on the value of $_GET['friendid'] if it has been previously set.

The best solution for this question, i.e. if you also need to 'check for the empty string', is empty().

$friendid = empty($_GET['friendid']) ? 'empty' : $_GET['friendid'];

empty() not only checks whether the variable is set, but additionally returns false if it is fed anything that could be considered 'empty', such as an empty string, empty array, the integer 0, boolean false, ...

From your reply to Philippe I think you need to have a look at the differences between empty and isset.

To summarise, isset() will return boolean TRUE if the variable exists. Hence, if you were to do

$fid = $_GET['friendid'] = "";
$exists = isset($fid);

$exists will be TRUE as $_GET['friendid'] exists. If this is not what you want I suggest you look into empty. Empty will return TRUE on the empty string (""), which seems to be what you are expecting. If you do use empty, please refer to the documentation I linked to, there are other cases where empty will return true where you may not expect it, these cases are explicitly documented at the above link.

if friendid is NOT set, friendid = friendid otherwise friendid = empty

Okay, I may have been having a similar issue not being familiar with the ! situation as jasondavis had.

Kind of confusing but finding out not having the ! as in... isset($avar) compared to !isset($avar) can make quite the difference.

So with the ! in place, is more stating a YES as in

    since $_GET['friendid'] = 55; has been initialized...
                                tell me 'no' - the opposite - that it hasn't and set it to empty.
              $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

    where not having the ! tells me yes it has something in it, leave it be.

               $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

Was far less confusing with if A$="" then.... work it. ( or if $A="" for those of PHP ).

I find this use of strings and variables all as strings to be very daunting at times. Even through the confusion, I can actually understand why... just makes things a tad difficult to grasp for me.

I am using Null coalescing operator operator in if condition like this

if($myArr['user'] ?? false){

Which is equivalent to

if(isset($myArr['user']) && $myArr['user']){
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top