Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Noob learning to become a Web developer here. On a PHO module of my study course right now and hitting a bit of a bug.

Anyone able to explain to me why unset($name); is causing an error message to get thrown?

Any help would be greatly appreciated :)

<?php

$myArray=array("pizza","chocolate","coffee");

print_r($myArray);

echo $myArray[1];

echo "<br /><br />";

$anotherArray[0]="pizza";
$anotherArray[1]="yoghurt";

print_r($anotherArray);

echo "<br /><br />";

$thirdArray=array(

    "France" => "French",
    "USA" => "English",
    "Germany" => "German",

);

print_r($thirdArray);

$anotherArray[]="salad";

echo "<br /><br />";

print_r($anotherArray);

echo "<br /><br />";

unset($thirdArray["Germany"]);

print_r($thirdArray);

echo "<br /><br />";

$name="Rob";

unset($name);

echo $name;

?>
share|improve this question
8  
after unset($name) you are trying to echo it so it throws error –  Manadh 19 hours ago
    
you are echoing unset variable thats why you got error –  Navneet Garg 19 hours ago

1 Answer 1

up vote 0 down vote accepted

You cannot use any variable you have just unset(). unset() is here to explicitly destroy a variable that cannot be longer used.

Therefore, it should be:

<?php
// first, set $name
$name = 'Rob';
// then, use it
echo $name;
// at last, unset it
unset($name);

echo $name // will finally output an error.

Instead of the other way around.

share|improve this answer
    
Okay I understand now! Thanks very much :) –  Sean Ravenhill 18 hours ago
    
Glad you made it work! Please also consider upvoting the answer if it helped you. –  D4V1D 18 hours ago
    
@SeanRavenhill I just upvoted your question btw :) –  D4V1D 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.