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];
Related
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
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 a way to do this
for($i=1;$i<=10;$i++){
$id$i = "example" . $i;
}
notice the second line has $id$i
so for the first loop $id1 will equal example1
in the second loop $id2 will equal example2
and so on...
Thank you very much!
You can use variable variable names to do this; however, it's probably much more convenient if you just used an array:
for($i = 1, $i <= 10, $i++) {
$id[] = "example" . $i;
}
You can convert a string into a variable (the name of the variable), if you put another $ in front of it:
$str = "number";
$number = 5;
$$str = 8;
echo $number; // will output 8
So in your example, you could do it like that:
for($i = 1; $i <= 10; $i++) {
$str_var = "id".$i;
$$str_var = "example".$i;
}
It would be much better to use an array, but you could do this:
for($i=1; $i<=10; $i++){
$var ="id$i";
$$var = "example" . $i;
}
Here's what I would recommend doing instead:
$ids = array;
for($i = 1; $i <= 10; $i++) {
$ids[$i] = "example" . $i;
}
You could create an array of size $i with a name of $id, and insert each element into a different index.
for($i=1;$i<=10;$i++){
$id[$i] = "example" . $i;
}
$var = array();
for($i=1; $i<=10; $i++) {
$var['id' . $i] = 'example' . $i;
}
extract($var, EXTR_SKIP);
unset($var);
but why not use a simple array?
I need to create a php file with a hundred variables, which are all identical except for their id.
PHP Code
$myvar1 = get_input('myvar1');
$myvar2 = get_input('myvar2');
$myvar3 = get_input('myvar3');
$myvar4 = get_input('myvar4');
...
$myvar100 = get_input('myvar100');
I wonder if it is possible to create only one line as a model, and is replicated 100 times?
Thanks.
Just store it in an array instead of 100 variables:
$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
$myvar[$i] = get_input('myvar' . $i);
}
Or if you want the indexes to start at zero:
$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
$myvar[$i - 1] = get_input('myvar' . $i);
}
You really must use Arrays for this, its far more appropriate:
$myvar = Array();
for($i=1;$i<101;$i++) $myvar[$i]=get_input("myvar{$i}"); // $myvar[1]=... etc...
Anyway, its possible to create variable names dynamically (notice the '$$'):
for($i=1;$i<101;$i++){
$varName="myvar{$i}";
$$varName=get_input($varName);
}
you could store your variables in an array
$myvarArr = array();
for($i=0;i<100;i++) {
$myvarArr[$i] = get_input('myvar' . ($i+1));
}
Looks like you just need an array, an array right from the outside variable.
if you make your form field name like this
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
you can easily get all the values by as simple code as
$myvar = $_POST['myvar'];
Just to show, that it's possible, use variable variables (i just love this php feature):
for ($i = 1; $i <= 100; $i++) {
$varName = "myvar{$i}"; // create variable name
$$varName = get_input("myvar{$i}"); // $$varName => $myvar5, when $i == 5 and so on
}
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;