Forum Moderators: coopster

Message Too Old, No Replies

What does "unset()" do in this given code?

         

yellow_nemo

3:46 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



Hi, I am trying to learn PHP better. I came across this code (which is used for a sticky form) and I did several testings on it, but I still couldn't see what the point is having the unset() line in the code. I took it out for one of my testings and it didn't seem to make any difference.

So what does the unset() line do? Why put it there? I know that unset() basically resets the variables to empty. But like I said, taking it out or leaving it in makes no difference in my testings.

Thanks!


<?php
session_start();

unset($sender_error, $address_error, $whichForm);

if(isset($sender)) {
if(empty($sender))
$sender_error = TRUE;
else
$senderName = $sender;
}

if (isset($sender_address)) {
if(empty($sender_address))
$addressForm_error = TRUE;
else
$address = $sender_address;
}

if(isset($submission)) {
if(!($sender_error ¦¦ $addressForm_error)) {
$whichForm = "confirmation";
}
}

unset($submission);

?>

<script language="JavaScript" type="text/JavaScript">
<!--

function errormsg(theField){
if(theField == "sender")
alert("Please write your name in the first blank before sending your message.");
else
alert("Please write your email address in the second blank before sending your message.");

}

//-->
</script>

<?php
if($sender_error) {
echo "<body onload=errormsg('sender')>";
}
else if ($addressForm_error){
echo "<body onload=errormsg('addressForm')>";
}
else{
echo "<body>";
}
?>

<?php

if($whichForm!= "confirmation") {
?>
<form action="<?php echo $PHP_SELF;?>" name="formMail">


<p>Your name:<br>
<?php if(isset($senderName) && $senderName!= "") {?>
<input name="sender" value ="<?php echo $senderName;?>" type="text" size="55">
<?php } else {?>
<input name="sender" type="text" size="55">
<?php }?>
</p>
<p>Your email address:<br>
<?phpif(isset($address) && $address!= "") {?>
<input name="sender_address" value="<?php echo $address;?>" type="text" size="55">
<?php } else {?>
<input name="sender_address" type="text" size="55">
<?php }?>
</p>

<p>

<input type="submit" name="submission" value="Send">
</p>
<?php
}
//if this should display the confirmation message
else {
print "Hello World!";
}
?>

supermanjnk

6:11 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



A lot of times unset is used so that the data doesn't get passed again if the person refreshes the page, that looks like what it's used for in this, but I could be wrong.