PHP concatenate variable - php

Might be an easy question for you guys. can't find it on google.
I am trying to concatenate two variables name;
$i=0;
for ($i=0;$i<5;$i++){
if($array[$i]>0){
$test.$i=//do something
}else{
$test.$i=//do something
}
}
//echo $test0 gives me nothing.
//echo $test1 gives me nothing.
I know I can't use $test.$i but don't know how to do this.Any helps? Thanks!

try ${$test.$i} = value
EDIT: http://php.net/manual/en/language.variables.variable.php

I'm assuming that the variables are called $test0, $test1, ..., $test5. You can use the following:
${"test".$i}
Though, might I suggest that you make $test an array and use $i as the index instead? It's very odd to use $i as an index to loop through a list of variable names.
As an example, instead of:
$test0 = "hello";
$test1 = "world";
Use:
$test[0] = "hello";
$test[1] = "world";

Try this:
for ($i=0;$i<5;$i++){
$the_test = $test.$i;
if($array[$i]>0){
$$the_test=//do something
}
else{
$$the_test=//do something
}
}

This might work:
$varName = $test . $i;
$$varName = ...
Can i ask where for this is neccesary?

Related

How to replace part of a multi array variable with a different variable?

Here is an example of what I am trying to accomplish:
$array['aaa']['bbb']['ccc'] = "value";
$subarray = "['bbb']['ccc']";
echo $array['aaa']$subarray; // these 2 echos should be the same
echo $array['aaa']['bbb']['ccc']; // these 2 echos should be the same
It should display the same as $array['aaa']['bbb']['ccc'] i.e., "value".
This doesnt work, of course. But is there some simple solution to this?
There could be some function and the $subarrayvalue may be used as a parametr and/or as an array itself like: $subarray = array('bbb','ccc'); I dont mind as long as it worsk.
You could try something like below.
$subarray = "['bbb']['ccc']";
$temp = parse_str("\$array['aaa']".$subarray);
echo $temp;
OR To ignore single quotes -
$subarray = "[\'bbb\'][\'ccc\']";
$temp = parse_str("\$array[\'aaa\']".$subarray);
echo $temp;
Also you may refer - http://php.net/manual/en/function.parse-str.php
Just try using array chunk function http://php.net/manual/en/function.array-chunk.php
here is what actually works!!
$array['aaa']['bbb']['ccc'] = "value";
$subarray = "['bbb']['ccc']";
$string = 'echo $array[\'aaa\']' . $subarray . ';';
eval($string);

PHP-separate the variables value from the position of character '+'

I have variable of type varchar having values $pointmoney=356+2311;
pointmoney is a field in database from where i am retreiving this value .
now i have separte two variable $point and $money
where i want to store $point=356 and $money=2311 from $pointmoney=356+2311.(i.e separate values from '+').
If any 1 knows any similar function then pls answer.
hope i ellaborated well to understand my query . if anything is unclear pls feel to comment .
Use explode():
$pointmoney = "356+2311";
list($point, $money) = explode('+', $pointmoney);
you explode function like
$pointmoney = "356+2311";
$arr_money = explode("+",$pointmoney);
echo $point = $arr_money[0];
echo $money= $arr_money[1].
hope this will sure help you,
You can use explode() reference PHP explode
$pointAndmoney = "356+2311";
list($point,$money) = explode('+',$pointAndmoney); // now you have different variable for each
$arr = explode("+","54+99");
$point = $arr[0];
$money = $arr[1];

how to call a numbered variable in a loop in php?

I know what i have implemented here is wrong i want it to do it correctly that is why asking help here.Don't know whether this is possible or not.
<?php
$test1="hello";
$test2="how";
$test3="are";
for($i=1;$i<=3;$i++)
{
echo $test.$i;
}
?>
When i run this i should get hello how are .i know string concatenation same thing i want to do it for variable also.Is this Possible, if possible by this i can easily access all those variable. Any help?
Try with following syntax:
echo ${'test'.$i};
I guess what you're looking for is the "array". You can use arrays in PHP like this:
$test = array('hello', 'how', 'are');
$len = count($test);
for($i=0; $i<$len; $i++) {
echo $test[$i];
}
Try this:
for($i = 1; $i < 4; $i++){
echo $test{$i};
}

PHP: New variable from string concatenated with variable (variable-variables)

I am having a basic php problem that I haven't been able to resolve and I would also like to understand WHY!
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= '$upperValue'.$passNodeMatrixSource;
echo $topValue;
OUTPUT
$upperValueCB
but I want OUTPUT as the variable's value 10.
How can I make PHP read the $dollar-phrase as a variable and not a string?
$varName = 'upperValue' . $passNodeMatrixSource;
$topValue = $$varName;
http://php.net/manual/en/language.variables.variable.php
This little example might illustrate what you are about to do:
<?php
$a = 'b';
$b = 10;
echo ${$a}; // will output 10
So you will have to change your example to:
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= 'upperValue'.$passNodeMatrixSource;
echo ${$topValue}; // will output 10
You can do it this way :
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= ${'upperValue'.$passNodeMatrixSource};
echo $topValue;
Because upperValueCB is the var name, if you want PHP to understand which var to use, you have to give the var name.
For that, you can use static way like $upperValueCB
or using a string like this : ${'upperValueCB'}
or using a third var containing the var name $var = 'upperValueCB'; $$var;
$upperValueCB = 10;
$passNodeMatrixSource = 'CB';
$topValue= 'upperValue'.$passNodeMatrixSource;
echo ${$topValue};

Concatenate strings for variable identifier in PHP

I know this is not the proper way to do this, however I am trying to put a quick fix on a form that was done by another developer. Basically I want to add an incremental number to a variable inside a while statement:
$count = 1;
while ($r = mysql_fetch_array($query)) {
$variable . $count = $r['somefield'];
$count++
}
So that makes the variables:
$variable1
$variable2
$variable3
....etc
$varname = 'variable' . $count;
$$varname = $r['somefield'];
http://www.php.net/manual/en/language.variables.variable.php
You'd be better off with an array...
$variable[] = $r['somefield'];
You can use variable variables, however it is probably not a good idea, especially for a trivial case like this one.

Categories