Let's say I have some variables declared - but I don't know exactly which, I just have an array with variable names.
$variable_list = array('var1', 'var2', 'var3', 'var4');
We go ahead and assign some values.
foreach($variable_list as $var_name){
$$var_name = rand(100,1000);
}
Now I want to unset these variables in a similar fashion. Not remove them from list, but unset the ACTUAL variable.
foreach($variable_list as $var_name){
unset($var_name);
}
this does not work. any ideas?
foreach($variable_list as $var_name){
unset($$var_name);
}
PHP Manual
Why do you set them using variable variables ($$var_name) and don't unset them like that? This should work:
foreach($variable_list as $var_name){
unset($$var_name);
}
However, since you say:
Now I want to [...] not remove them from list, but unset the ACTUAL variable.
Simply use:
foreach($variable_list as $var_name){
$$var_name = null;
}
Related
I want to append some $variables automaticly and set their names numeric
I have a script look like this:
<?php
$i=0;
while($i<=100){
$variable_[$i]=$i;
$i++;
#with "[$i]" I mean their name will be $variable_1 , $variable_2, $variable_3 ...
#they will be automatic increased variables non manual!
}
?>
This is called variable variables.
You can set a variable variable by defining its name inside a variable, such as:
$name = 'variable_' . $i;
and then assign a value to it by doing:
$$name = $i;
Note that variable variables can easily be misused. Make sure you completely understand the repercussions of this feature on your code and the risk of having bugs, and ensure this is the only solution you have, i.e. you can't use an array ($variables[$i] = $i;) instead.
Its better to use Array with key=> value pair. You can build this array dynamically and then loop through it by using foreach.
I'm trying to do a simple function here, but it's not working.
I want to check if the variable is indeed in the URL, and if it is I want to define a variable with it.
if(isset($_GET['ref'])){
$ref = $_GET['ref'];
}
Could someone point out the errors?
It's probably that isset only checks if the variable has been declared. That variable could still be empty. so this will set $ref even when $_GET['ref'] = ""; Try this instead:
if(isset($_GET['ref'])){
if(!empty($_GET['ref'])))
{
$ref = $_GET['ref'];
}
}
The Get and Post variables are accessible to you as Arrays. I would loop over the array and treat it as a Key/Value pair. This will give you the ability to do what ever you would like with the key and the value.
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
See this previous Stack Overflow thread.
How do I get the key values from $_POST?
How do you unset a variable variable representing an array element?
function remove($var) {
unset($$var);
}
$x=array('a'=>1,'b'=>2);
remove('$x["a"]');
var_dump(isset($x['a']));
The code above doesn't unset the array element x['a']. I need that same remove() function to work with $_GET['ijk'].
Just use unset() or the (unset) cast.
If you want to use a function to unset, something like this would be better.
function removeMemberByKey(&$array, $key) {
unset($array[$key]);
}
It works!
You can try,
function remove(&$var,$key) {
unset($var[$key]);
}
$x=array('a'=>1,'b'=>2);
remove($x,'a');
var_dump(isset($x['a']));
Variable variables cannot be used with superglobals, so if you need it to work for $_GET as well, you need to look at using a different method.
Source: http://php.net/manual/en/language.variables.variable.php
Try This:
<?php
/* Unset All Declair PHP variable*/
$PHP_Define_Vars = array_keys(get_defined_vars());
foreach($PHP_Define_Vars as $Blast) {
// or may be reset them to empty string# ${"$var"} = "";
unset(${"$Blast"});
}
?>
unset is easier to type then remove
When using it with an array element, the array will still exist
You could rewrite your function to treat the parameter as a reference;
EDIT: updated to use alex's code
function remove(&$array, $key){
unset($array[$key]);
}
remove($x,'a');
I have a cookie which stores info in an array.
This is for a classifieds website, and whenever users delete their 'ads' the cookie must also be removed of the ad which was deleted.
So I have this:
if (isset($_COOKIE['watched_ads'])){
$expir = time()+1728000;
$ad_arr = unserialize($_COOKIE['watched_ads']);
foreach($ad_arr as $val){
if($val==$id){ // $id is something like "bmw_m3_10141912"
unset($val);
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
}
}
This doesn't work... any idea why? I think its a problem with the unset part...
Also, keep in mind if there is only one value inside the array, what will happen then?
Thanks
You got two bugs here: 1) you unset the $val instead of the array element itself. 2) You set the cookie within the loop to the unknown $ad_arr2 array.
foreach($ad_arr as $key => $val){
if($val==$id){ // $id is something like "bmw_m3_10141912"
unset($ad_arr[$key]);
}
}
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
array_filter seems appropriate:
$array = array_filter($array, create_function('$v', 'return $v != '.$id.';'));
You are correct that you are using unset incorrectly. The manual on unset states:
If a static variable is unset() inside
of a function, unset() destroys the
variable only in the context of the
rest of a function. Following calls
will restore the previous value of a
variable.
When you use 'as' you're assigning the value of that array element to a temporary variable. You want to reference the original array:
foreach ($ad_arr as $key => $val)
...
unset($ad_arr[$key]);
...
I'm having trouble with the following code. What it should do is echo cats.php followed by example.php but it's not echoing the example.php. Any ideas why this might be happening?
$bookLocations = array(
'example.php',
'cats.php',
'dogs.php',
'fires.php',
'monkeys.php',
'birds.php',
);
echo $bookLocations[1];
function findfile($filenumber)
{
echo $bookLocations["$filenumber"];
}
findfile(0);
Try changing,
echo $bookLocations["$filenumber"];
to:
echo $bookLocations[$filenumber];
Edit* To expand on Thomas's correct answer, instead of using global variables, you could change your method to:
function findfile($filenumber, $bookLocations)
{
echo $bookLocations[$filenumber];
}
i believe you may also need to declare the global variable in your function.
global $bookLocations;
Ok, there are two issues.
Variable Scope
Your function doesn't know the array $bookLocations, you need to pass it to your function like so:
function findfile($filenumber, $bookLocations)
Array key
You don't want to wrap your array key in quotes:
wrong: $bookLocations["$filenumber"];
right: $bookLocations[$filenumber];
The quotes in "$filenumber" turn your key into a string, when the keys to your array are all numbers. You are trying to access $bookLocations["1"] when in fact you want to access $bookLocations[1] -- that is to say, 1 is not the same as "1". Therefore, like others have said, you need to get rid of the quotation marks around the key (and check your variable scope too).
function findfile($filenumber)
{
global $bookLocations;
echo $bookLocations[$filenumber];
}
Good-style developers usually avoid global variables. Instead, pass the array to the function as the parameter:
function findfile($files, $filenum)
{
echo $files[$filenum];
}
$bookLocations is out of scope for your function. If you echo $filenumber you will see that it's in scope because you passed it in by value. However, there is no reference to $bookoLocations.
You should pass in $bookLocations
declaration: function findfile($filenumber, $bookLocations){
call: findfile(1, $bookLocations);
You could also to declare $bookLocations as global, but globals should be avoided if possible.