As I have really many variables inside my foreach loop, it would be great if I could reset them all at once instead of:
$variable_one = ''; // Reset to: It is blank for each loop
$variable_two = '';
...
$variable_hundred = '';
If I were you and had these number of variables which should be set to some value in a loop, I would use an array instead:
$arr = ['first value', 'second value','hundred value'];
Then you can access what you want by index in your loop, so instead of using:
$variable_one
You will use:
$arr[0]
And now you want to reset them all, so you can use array_map() like this:
$arr = array_map(function($val){ return '';}, $arr);
If you have such a high number of variables in your loop, you probably should refactor it to make it simpler. If you are sure that having 100 variables inside a loop is a way to go, you can use the following expression:
$variable_one = $variable_two = $variable_hundred = '';
This will set each variable to '' in one very long line.
Another option is to unset() all these variables in a single function call:
unset($variable_one, $variable_two, $variable_hundred);
But this will not set their value to '', but unset the variable itself.
Related
I'm trying to define three empty variables through a foreach loop to make my code cleaner. This is what I've tried, however I see the error:
Notice: Undefined variable: hi
foreach(['$hi','$bye','$hello'] as $key) {
$key = "";
}
$hi .= "hello";
When I remove the foreach loop and simply define each empty variable one by one, like this, it works:
$hi = "";
$bye = "";
$hello = "";
You're assigning to $key, not to the variable that's named by it. To indirect through a variable, you need to use $$key. But the value of the variable shouldn't include the $, just the variable name.
foreach (['hi', 'bye', 'hello'] as $key) {
$$key = "";
}
$hi .= "hello";
However, if you ever find yourself using variable variables like this, you're almost certainly doing something wrong. You should probably be using an associative array instead.
You have strings which are saved in $key. So the value of $key is a string and you set it to "".
Later you want to append something to a variable you never used.
Try to remove the ' and write
foreach([$hi, $bye, $hello] as $key) {
Generally thats not the best way to initialise multiple variables. Try this
Initializing Multiple PHP Variables Simultaneously
Easier way:
list($hi, $bye, $hello) = "";
foreach creates a new array variable in memory, so you only clear these values inside the array in memory which is useless out of the foreach sentence. the best way is:
$h1=$bye=$hello="";
I didn't think that a foreach process will work more fast than a Simple equal (=), foreach function uses more CPU resources than a simple =. That's because the math CPU exists.
I create a $values array and then extract the elements into local scope.
$values['status'.$i] = $newStatus[$i];
extract($values);
When I render an html page. I'm using the following
<?php if(${'status'.$i} == 'OUT'){ ?>
but am confused by what the ${ is doing and why $status.$i won't resolve
$status.$i means
take value of $status variable and concatenate it with value of $i variable.
${'status'.$i} means
take value of $i variable, append id to 'status' string and take value of a variable 'status'.$i
Example:
With $i equals '2' and $status equals 'someStatus':
$status.$i evaluated to 'someStatus' . '2', which is 'someStatus2'
${'status'.$i} evaluated to ${'status'.'2'} which is $status2. And if $status2 is defined variable - you will get some value.
I wanted to add to the accepted answer with a suggested alternate way of achieving your goal.
Re-iterating the accepted answer...
Let's assume the following,
$status1 = 'A status';
$status = 'foo';
$i = 1;
$var_name = 'status1';
and then,
echo $status1; // A status
echo $status.$i; // foo1
echo ${'status'.$i}; // A status
echo ${"status$i"}; // A status
echo ${$var_name}; // A status
The string inside the curly brackets is resolved first, effectively resulting in ${'status1'} which is the same as $status1. This is a variable variable.
Read about variable variables - http://php.net/manual/en/language.variables.variable.php
An alternative solution
Multidimensional arrays are probably an easier way to manage your data.
For example, instead of somthing like
$values['status'.$i] = $newStatus[$i];
how about
$values['status'][$i] = $newStatus[$i];
Now we can use the data like,
extract($values);
if($status[$i] == 'OUT'){
// do stuff
}
An alternative solution PLUS
You may even find that you can prepare your status array differently. I'm assuming you're using some sort of loop? If so, these are both equivalent,
for ($i=0; $i<count($newStatus); $i++){
$values['status'][$i] = $newStatus[$i];
}
and,
$values['status'] = $newStatus;
:)
I have a problem assigning value to a variable in php.
Example what I am trying to do:
Suppose I have 3 variables and I want to assign the value of 3rd variable to 4th variable but in 3rd variable I want to use first 2 variables.
$depth = 1;
$forumcat = _cat;
$f1_cat = 'some html code';
$new = '';
Now I want to assign $f1_cat to $new variable
I know it can be done like $new = $f1_cat but I want to do it like
$new = $f$depth$forumcat;
I mean in place of using '1_cat' I want to use the variables.
I tried the following
$new = "\$f{$depth}{$forumcat}";
but this statement assigns a string $f1_cat but I want to assign $f1_cat variable value.
How to do it?
$new = ${'f'.$depth.$forumcat}; // This will contain value of $f1_cat
Also you may want to change $forumcat = _cat; to $forumcat = '_cat';. please note the single quotes added to _cat
How is it possible to perform a foreach function without doing a loop for example
foreach($result['orders'] as $order) {
But I don't want to do a foreach I want something like
$result['orders'] == $order;
Or something like that instead of doing it inside an loop because $result['orders'] is only returning 1 result anyway so I don't see the point in doing it in a loop.
Thank you
You can get the first (and apparently only) element in the array with any array function that gets an element from the array, e.g. array_pop() or array_shift():
$order = array_shift( $result['orders']);
Or list():
list( $order) = $result['orders'];
Or, if you know it's numerically indexed, access it directly:
$order = $results['orders'][0];
Are you maybe just looking for this?
$result['orders'] = $result['orders'][0];
You have a comparison operator (==) rather than an assignment operator (=) in your second code example. If you are just trying to set a variable equal to a position in an array, you can use:
$order = $results['orders'];
I am not sure if that is what you are trying to accomplish though.
$prefix = 'some';
$name_of_variable = $prefix.'_var';
So I have a variable named $some_var.
How can I check the value of it?
if($name_of_variable) ...
will return the value of $name_of_variable instead of the value of $name_of_variable.
Variable variables. But you do NOT want to use them. They make for impossible-to-debug code. They're almost always a sign of bad design.
DO NOT use a variable which is partially created from a string.
Use arrays instead.
$prefix = 'some';
$name_of_variable = 'var';
echo $array[$prefix][$name_of_variable];
Variable variable usually used when you need to create variables from string,for example convert $_POST keys into variable with its value .
$allowed_var = array('name',..);
foreach( $_POST as $key => $value
{
if( isset($allowed_var[$key] ) )
${$key} = $value;
}
...