Setting a value to variable - php

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

Related

Merge two variable values to get a new variable name (not value)

is possible in php to merge the first variable value with the second variable value to get a new variable name (not value).
$1 = test
$2 = 5
//do magic results in new variable name eg. $($1+$2)
$test5 = true
So if $2 is dynamic we get a dynamic variable name, not a dynamic value. The value "true" is just an example value. I don't know If it is possible, because I'm completely new to PHP and found nothing by searching.
Yes. Use {} after the $ sign to interpret the inner content as the variable name.
$a = "test";
$b = 5;
${$a . $b} = "new value";
echo "test5 = " . $test5; // prints test5 = new value
See manual page for more details.

Reset variables inside a 'foreach' loop

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.

composed variable after Object Operator in PHP

How can I build a composed variable while creating a variable in PHP?
(Sorry I'm not sure how to call the different elements)
This is what I'm trying to do:
$language = 'name_'.$this->session->userdata('site_lang');
for ($i=1;$i<=3;$i++) {
$data = $arraydata->$language_.$i; // problem is here
}
I would like $language_.$i to be equivalent to name_english_1, next loop name_english_2... The same way I built $language
If you want to use an expression in a computed property, you have to put the expression in braces. Also, you need to put the underscore in quotes.
$data = $arraydata->{$language."_".$i};
However, I suggest you redesign your data structure. Instead of having separate name_LANG_i properties, make a single name property whose value is a multi-dimensional array.
$lang = $this->session->userdata('site_lang');
for ($i=1;$i<=3;$i++) {
$data = $arraydata->name[$lang][$i];
// do something with $data
}
Whenever you find yourself using variable variables or variable properties, it's almost always a sign that you should be using an array instead.
First construct the field name and then use it for accessing the field value from the object $arraydata. So your code should be like this:
$language = 'name_'.$this->session->userdata('site_lang');
for ($i = 1; $i <= 3; $i++) {
$var = "{$language}_{$i}";
$data = $arraydata->$var;
// echo $data;
}

How to get the value of a variable which is partially created from a string?

$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;
}
...

reference variable array

I always use $text = $datasql[0];
where $datasql = array('0'=>array('some'=>'text', 'some2'=>'text2'), '1'=>$data, etc...);
and found work construction $datasql = &$datasql[0]; and work, why?
That really reference?? and how remember php in memory this solution.
Every variable is a reference to a value. Normally the value is copied when you use it, but with & the reference is copied.
Suppose you have the following variable:
$original = 'john';
If you assign the value from $datasql to a variable, that value is copied:
$text = $original;
If you assign a reference, the value is not copied but referenced:
$text = & $original;
This means that $text points to the value of $original. Now, if you unset $original, the contents of $text are still valid:
unset($original);
echo $text; // john
This is because PHP knows there is still a reference to the value of $original, so it deletes the $original variable as name, but not the contents.
Your example is similar, except that the variable is not explicitly unset, but overwritten. It is a reference to a value, just like any other variable.

Categories