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.
By example.
Isset (
isset
: Determine if a variable is set and is not NULL):Output:
Not operator:
Output:
Together:
Output:
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.