Merging Variable PHP - php

im confuse that...
like example:
$Q1 = "hello";
$Q2 = "world";
$Q3 = "StackOverflow";
$i = 1;
while($i < 3) {
$a = "$Q".$i; //I think this is wrong.
echo $a; // i tried ${$a} doesn't work =/
$i++;
}
then output format:
$Q1
$Q2
$Q3
but there is not output like this:
hello
world
StackOverflow
I want like $Q + $i become $Q1 to answer is: "hello"...

$varName = 'Q'.$i;
$a .= $$varName;
Or just
echo $$varName . "<br>\n";

To create the variable variable, use:
$a = ${'Q'.$i};

echo $Q1 . $Q2 . $Q3; will output what you're looking for.
Alternatively, you could do this:
$a = '';
for($i = 1; $i <= 3; $i++)
$a .= ${'Q' . $i};
echo $a;

What you are doing there is simply printing the string '$Q1', '$Q2' and '$Q3'. In PHP you use dynamic variable names this way:
<?php
$Q1 = 'hello';
$Q2 = 'world';
$Q3 = 'StackOverflow';
for ($i = 1; $i <= 3; $i++) {
echo ${'Q' . $i};
}
?>

PHP does support variable variable names, denoted using $$. This will do what you want.
$qvar = 'Q'.$i;
$a = $$qvar;
However, this is considered very poor practice -- almost as bad as using eval() (and for similar reasons).
The correct answer would be to create an array of $Q, and referencing array elements;
$Q = array(
"hello",
"world",
"StackOverflow")
$a = $Q[0] . $Q[1] . $Q[2];

Yeah. When you have double quoted strings, and you put a dollar sign and something else in it, it interprets it as a variable. (it also escape things like \n)
Example
$test = "hi";
echo "$test world"; //This outputs hi world
In your case, $Q doesn't exist. The default PHP behaviour is to ignore that error and just puts out nothing. This is why it's recommended to report all problems with your code. Check out
http://php.net/manual/en/function.error-reporting.php for details.
Solution to your problem would be using single quoted strings. do $a = '$Q'.$i;

$Q = array("hello", "world", "StackOverflow");
foreach($Q as $w) {
echo $w;
}
If you can't do something like this then you will need to use dynamic variables:
$var = 'Q' . $i;
echo $var;

Related

PHP Loop Dynamic Variable

I am trying to create a dynamic variable. I have a loop and I want it to loop through the records and create a variable for each record. My code:
$ct = 1;
foreach ($record as $rec){
$var.$ct = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
When I try to use the above code, it gives me an error saying the $var1 variable doesn't exist/undefined? Is it possible in PHP to create dynamic variables like the above example. If so, what am I doing wrong?
You're looking for variable variables.
Create the variable name as a string, and then assign it:
$ct = 1;
foreach( $record as $rec )
{
$name = 'var'.$ct;
$$name = $rec['Name'];
$ct++;
}
echo $var1;
It would be much better to create an array, though:
$names = [ ];
foreach( $record as $rec )
{
$names[] = $rec['Name'];
}
echo $names[0];
You can use different syntax with {}
$ct = 1;
foreach ($record as $rec){
${'var' . $ct++} = $rec['Name'];
}
echo $var1;
Although isn't it better just to use an array?
Working fiddle
You can with a double $.
$var = "variable";
$$var = "test";
echo $variable;
//echoes "test"
in your example:
$ct = 1;
foreach ($record as $rec){
$varname = "var" . $ct;
$$varname = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
Please try this, let me know if it works for you.
I use a prefix with the dynamic variable.
$ct = 1;
$prefix = 'var';
foreach ($record as $key=>$rec){
$temp = $ct;
$ct = $prefix.$ct;
$$ct = $rec;
$ct = $temp + 1;
}
echo $var1;
You can do that using array easily. But if you really want it to be in dyanamic vairable individually, in that case also , I would like to suggest you to get help in array way. This way, you can track you variables.
In the below mentioned way, you event don't need to take a extra variable like $ct. Just an array $var and applying extract method on it after the loop will do the trick.
$var = [];
foreach( $record as $k => $rec )
{
$var['var'.$k] = $rec['Name'];
}
extract($var);
echo $var0; //or $var_1 or $var_2

how to shorten variables

I have this
$terim1=$isaret1.$carpan1.$terim1;
$terim2=$isaret2.$carpan2.$terim2;
$terim3=$isaret3.$carpan3.$terim3;
I wnat to do like this:
for($i=0;$i<4;$i++){
$terim$i=$isaret$i.$carpan$i.$terim$i;
}
Is it possible? Or is there any solution?
Wrap it into curly brackets:
for($i=0; $i<4; $i++){
${"terim".$i} = ${"isaret".$i} . ${"carpan".$i} . ${"terim".$i};
}
See it online: https://eval.in/927730
<?php
$isaret1 = 'aa';
$carpan1 = 'bb';
$terim1 = 'cc';
$isaret2 = 'AA';
$carpan2 = 'BB';
$terim2 = 'CC';
$isaret3 = '11';
$carpan3 = '22';
$terim3 = '33';
$terim1=$isaret1.$carpan1.$terim1;
$terim2=$isaret2.$carpan2.$terim2;
$terim3=$isaret3.$carpan3.$terim3;
for($i=0; $i<4; $i++){
${"terim".$i} = ${"isaret".$i} . ${"carpan".$i} . ${"terim".$i};
}
echo PHP_EOL . $terim1;
echo PHP_EOL . $terim2;
echo PHP_EOL . $terim3;
will produce
aabbaabbcc
AABBAABBCC
1122112233
You could actually do this with var vars (like seen in other answers). The better way should actually be to use arrays instead.
for($i = 0; $i<4; $i++){
$terim[$i] = $isaret[$i] . $carpan[$i] . $terim[$i];
}
Doing it like this, you could also easy pass $terim easy to other functions without modifing everytime the whole signature. On top of this, you know exactly how much values you have inside the array and could replace the 4 with count($terim).
To say it again - using var vars solves your current problem, but not the structural problem at all.
Yes, it is called a variable variable
$$a
http://php.net/manual/en/language.variables.variable.php
e.g. for your case:
$testvar1=1;
$testvar2=2;
$testvar3=3;
$a='testvar';
for($i=1;$i<=3;$i++)
echo ${$a.$i};
Try this code:
for($i = 0; $i < 4; $i++) {
$var1 = 'retim' . $i;
$var2 = 'isaret' . $i . 'carpan' . $i . 'terim' . $i;
$$var1 = $$var2;
}

I need right syntax of variable

I tried to create this variable:
<?php
echo ${"product['id']"};
?>
But it is wrong. Who can help with right syntax?
<?php
echo $product['id'];
?>
You don't need to wrap the variable into string if you want to print just one variable. Php will do it for you
If you need to generate variable name in code you can try this:
$var1 = 'hello';
$var2 = ' ';
$var3 = 'world';
for ($i = 1; $i <= 3; ++$i) {
$varName = 'var'.$i;
print $$varName; // use $$ to use string as a variable name
}
Result will be hello world
This is the equivalent for previous example
$var1 = 'hello';
$var2 = ' ';
$var3 = 'world';
$vars = array(1, 2, 3);
foreach ($vars as $var) {
$varName = 'var'.$var;
print $$varName;
}

appending two variables in php having `$` symbol in the appending string

can I know how to create following types of string using loop.
$row[$opt1val]
$row[$opt2val]
$row[$opt3val]
$row[$opt4val]
//...
In general I create like this
`
foreach($myArray as $someVar => $value){
$i++;
${"opt".$i."val"} = $value;
}
But to how to create this `$row[$opt1val] kind of strings so the string should behave as variable.
I tried but failed as I might not be doing at $ symbol, please bear with me as I am one day old with php
Use variable variable names:
$varName = 'row' . $i . 'val';
$row[ $$varName ];
Use variable of variables -
$opt1val = '1';
$opt2val = '2';
for($i = 1; $i <= 2; $i++) {
$val = 'opt'.$i.'val';
echo $$val. " - ";
}
in your case it would be -
$val= 'opt' . $i . 'val';
$row[ $$val];

problem with declaring variable in php?

i have a variables like $srange0 , $srange1, $srange2 $srange3.
i am using to declare some value to each value using for loop.
for($i=0;$i<=3;$i++){
$srange.$i = $i;
}
but its not working ?
is there any alternative solution for this
for($i=0;$i<=3;$i++){
$var = 'srange'.$i;
$$var = $i;
}
But, whenever I see variables like that, I'd use an array instead.
Use an array:
$srange = array();
for ($i = 0; $i <= 3; ++$i)
$srange[$i] = $i;
For the purpose of this particular task, you can also do this:
$srange = range(0, 3);
That also builds the same array as my first code snippet.
The properway to add these dynamic variables will be like this
for($i=0;$i<=3;$i++){
$name = 'srange'.$i;
$$name = $i;
}
This may be helpful to you:
$srange0;
$srange1;
$srange2;
for($i=0;$i<=3;$i++) {
$range = "srange".$i;
$$range = $i;
}
echo $srange2."<br />";
exit;

Categories