I've following code snippet, please go through it :

<?php
    // Top of the page, before sending out ANY output to the page.
        $user_is_first_timer = !isset( $_COOKIE["FirstTimer"] );

    // Set the cookie so that the message doesn't show again
        setcookie( "FirstTimer", 1, strtotime( '+1 year' ) );
?>

<H1>hi!</h1><br>


<!-- Put this anywhere on your page. -->
<?php if( $user_is_first_timer ): ?>
    Hello there! you're a first time user!.
<?php endif; ?>

In my coding experience, most of the time I've seen statements like !isset( $_COOKIE["FirstTimer"] ) with the if statement. For the first time in my life I'm observing such statement with the assignment operator.

In the above code I only want to understand what does the statement $user_is_first_timer = !isset( $_COOKIE["FirstTimer"] ); do?

What is the role of logical not(!) operator in this code line?

Please clear my doubts with good and reliable explanation.

Thank You.

1

There are 1 answers

0
Progrock On BEST ANSWER

By example.

Isset (isset: Determine if a variable is set and is not NULL):

$foo = '1';
$bar = '';
$baz = null;

var_dump(isset($foo));
var_dump(isset($bar));
var_dump(isset($baz));
var_dump(isset($bat));

Output:

bool(true)
bool(true)
bool(false)
bool(false)

Not operator:

var_dump(!true);
var_dump(!false);

Output:

bool(false)
bool(true)

Together:

$qux = 'something';
var_dump(!isset($qux));
var_dump(!isset($quux)); // Note quux isn't set.

Output:

bool(false)
bool(true)

So in your example, if the cookie value is not set (!isset), you are concluding they have not visited the site before.

With assignment you can have $true = !false. $true here will hold true, not false.