-3

I would create my own personalized unset function but it does not work. Here is an example of code that does not work.

<?php
function remove_var($mixedVar)
{
unset($mixedVar);
}
$sExample='Php';
remove_var($sExample);
echo $sExample;
?>
Jérémy Dupont
  • 275
  • 1
  • 4
  • 11
  • 1
    If i'm not mistaken , the unset relates to the arg $mixedVar and not to the $sExample var. Take a look: http://php.net/manual/en/function.unset.php – Ofir Baruch Feb 25 '12 at 16:20
  • 1
    You are unsetting the local `$mixedVar` variable. As far as I can tell there is no way to do what you are trying to, short of making `$sExample` global. See the [PHP documentation](http://php.net/manual/en/function.unset.php). – spencercw Feb 25 '12 at 16:23

2 Answers2

1

Your function won't unset $sExample because it exists outside of that function. Wonder why don't you use unset instead.

function remove_var($mixedVar)
{
   unset($mixedVar); // $mixedVar is unset not actual variable
}

$sExample='Php'; // this is outside variable
remove_var($sExample);
echo $sExample;

Even if you pass variable by reference, the unset won't remove that variable. For example:

function remove_var(&$mixedVar)
{
  unset($mixedVar);
}

$sExample='Php';
remove_var($sExample);
echo $sExample; // Php

So you should use unset instead.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

The first issue is that you're passing the argument by value, so what you do to it within remove_var() doesn't affect the original variable, outside the function. To pass by reference instead, change the function declaration as follows:

function remove_var(&$mixedVar)

However, there's still a quirk using unset() this way -- it doesn't work for me, even when the variable is passed by reference. However, it does work if you set the variable to null instead, which is mostly equivalent. When the variable in the function is unset(), it removes that reference but leaves the original reference and doesn't change the value. When you change the value to null instead, the references are left alone but the value is obviously changed to null. Since getting the value of an unset variable gives you null anyway, that should be effectively equivalent. So try:

function remove_var(&$mixedVar)
{
  $mixedVar = null;
}
Dmitri
  • 9,175
  • 2
  • 27
  • 34
  • That's true but `null` still keeps that reference in memory which defies the purpose of `unset`. The point here is that we should explain to OP that he should use `unset` which does what he is after and thereby avoid unnecessary code or rather such bad practice :) – Sarfraz Feb 25 '12 at 16:47
  • I'd figured he wanted to do something else in that function as well, that he hasn't shown -- otherwise there'd be no point in a personalized version. But I suppose it'd be best just to have a function for that other, unmentioned stuff and call `unset()` *after* anyway. – Dmitri Feb 25 '12 at 16:53
  • I agree with you in that case but guess what a wrong answer has been accepted by OP, funny :) – Sarfraz Feb 25 '12 at 16:55