Friday, April 12, 2013

PHP unset()

Unset destroys specified variables. I am going to show some good examples to explain how unset is applied to variables and the outcome.

Example 1 :
<?php
$foo = 'bar';
echo "At first : [$foo]";    // bar
modify_foo();
echo "<br>After modify_foo : [$foo]";  // Modified foo

destroy_foo();     // foo destroyed 

echo "<br> After destroy_foo foo is :: $foo";  
// Modified foo ... outside variable retains its value

destroy_foo_reference($foo);     // foo reference destroyed

echo "<br> At Last foo is :: $foo";  
// Modified foo ... outside variable retains its value

function destroy_foo()
{
    global $foo;
    echo "<br>Before [$foo]";  // Modified foo
    unset($foo);
    echo @"<br>After [$foo]";  

   // Outputs nothing as $foo is unavailable to this function
}

function destroy_foo_reference(&$foo)
{

    echo "<br>Before [$foo]";  // Modified foo
    unset($foo);
    echo @"<br>After [$foo]";  

   // Outputs nothing as $foo is unavailable to this function
}

function modify_foo()
{
    global $foo;
    $foo = "Modified foo";
}
?>


Output :
At first : [bar]
After modify_foo : [Modified foo]
Before [Modified foo]
After []
After destroy_foo foo is :: Modified foo
Before [Modified foo]
After []
At Last foo is :: Modified foo

The following points need to be noted.
a. Unlike some programming languages like Python, PHP functions can be declared anywhere within the code. C, C++ also have the similar structure. Functions may be declared after actual calls to them.
b. The keyword "global" brings the global version of a variable available for use and modification within any function. The function modify_foo() changes the value of $foo to "Modified foo".
c. In the destroy_foo() function, we get the modified value "Modified foo", but then the variable $foo is unset. As a result, the $foo variable in calling context is not destoyed, rather a local version destroyed so that $foo is no more available within destroy_foo() function after the unset call.
d. Next, we called a destroy_foo_reference() function which accepts $foo as a reference. In this function also, unset actually removes a local copy of $foo variable from the scope of called function only. After the function call, the $foo variable in the calling environment with old value is still available to the following part of the script. After unset call in both the above functions, we have used a "@" character with statement just to suppress the errors :

echo @"<br>After [$foo]";  // $foo is no more available. Hence this cause errors.

Example 2:
Unset() with Static variables bahave the same way as shown in previous examples. Static variables get their original value even they are unset within the function. Checkout the example below, it is self-explanatory.

<?php
function f()
{
 static $foo = 1;
 echo "<br>Beginning of f() function : [$foo]";
 $foo++;
 unset($foo);
 echo @"<br>End of f() function : [$foo]";
}

f();
f();
f();
?>


Output :
Beginning of f() function : [1]
End of f() function : []
Beginning of f() function : [2]
End of f() function : []
Beginning of f() function : [3]
End of f() function : []

$foo gets incremented in each function call and its original value is restored even after unset is called upon.

Note : Unset can not be used with variable functions. It is not a function rather a function construct.
 

<?php
$foo = "unset";
$foo();   //// Would give undefined function unset() error.
?>


Example 3:
Check out another example where unset has been used with objects and classes

<?php
class test
{
  var $foo = 1;             /// Instance Variable
 
  public function show()    /// Display
  {
    echo @"<br> \$foo is now : $this->foo";
  }
 
  public function incr()   /// Increment
  {
    $this->foo++;
    unset($this->foo);  /// Unset
  }
}

$a = new test();
$a->show();     /// 1     
var_dump($a);  
$a->incr();     /// Unset 
$a->show();     /// $foo is already gone
var_dump($a);
?>


Output:
$foo is now : 1
object(test)#1 (1) { ["foo"]=> int(1) }
$foo is now :
object(test)#1 (0) { }

The output is quite expected. The two var_dump calls show it all. The object variable $foo was removed in the incr() function call which is captured in the 2nd var_dump call.

No comments: