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;
}
Related
I am trying to make this code small using for or while loop . It is working with the following code,I just want this to be small. 'ufile' is the input name.
if (!$_FILES['ufile']['name'][0])
{
echo "Upload 1st file";
}
else
{
// can this be in a for loop???
$path1= "../uploads/".$_FILES['ufile']['name'][0];
$path2= "../uploads/".$_FILES['ufile']['name'][1];
$path3= "../uploads/".$_FILES['ufile']['name'][2];
$path4= "../uploads/".$_FILES['ufile']['name'][3];
$path5= "../uploads/".$_FILES['ufile']['name'][4];
$path6= "../uploads/".$_FILES['ufile']['name'][5];
$path7= "../uploads/".$_FILES['ufile']['name'][6];
$path8= "../uploads/".$_FILES['ufile']['name'][7];
}
$path = array();
for($i=0;$i<=7;++$i)
$path[$i]="../uploads/".$_FILES['ufile']['name'][$i];
I would advise against your current coding style. Life would be simpler if you just stored the paths in an array, e.g.
$paths[1] = "../uploads/" . $_FILES['ufile']['name'][0];
$paths[2] = "../uploads/" . $_FILES['ufile']['name'][1];
Then you could do something like this:
$paths = array();
for ($i = 0; $i <= 7; $i++) {
$paths[$i + 1] = $_FILES['ufile']['name'][$i];
}
But to answer your question, you can do something like this instead, which is very similar:
$paths = array();
for ($i = 0; $i <= 7; $i++) {
$paths['path' . ($i + 1)] = $_FILES['ufile']['name'][$i];
}
extract($paths);
See the extract doc page for more info about what's going on here
You can use variable variables as well :
foreach(range(0,7) as $index){
$varname = "path".$index;
$$varname = "../uploads/".$_FILES['ufile']['name'][$index];
}
Not sure what you want to do with those paths afterwards but here is my go at it. I would use the length of the array, assuming it doesn't always contain the same amount of file names.
$paths = array();
for($i = 0; $i < count($_FILES['ufile']['name']); $i++)
{
$paths[] = "../uploads/".$_FILES['ufile']['name'][$i];
}
I'm try to print an array where every other value is reassigned, as examples (from this):
17.34502870451717,62.46137370987033
To this:
62.46137370987033,17.34502870451717
That part have I succeeded with, but now I have this structure:
[62.46137370987033,[17.34501402936927,]
[62.46123453616544,[17.34525377433593,]
[62.4610178881864,[17.34546663705899,]
This is where I get stuck and do not know how to write.
The structure I want looks like this:
[62.392628, 17.309413],
[62.393162, 17.309193],
[62.393403, 17.30922]
Here is my explode.php (GIST)
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i++) {
echo $wing . $minion[$i+1].",";
if($i%2==1) { echo "]<br />"; }
} echo $minion[0] . $wing;
?>
As I understand it, as long as there's always even pairs it should be as easy as;
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$eol = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
echo $eol.'['.$minion[$i+1].','.$minion[$i]."]";
$eol=',<br/>';
}
echo '<br/>';
>>> [62.46137370987033,17.34502870451717],
>>> [62.46123453616544,17.34501402936927]
Try This
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i+=2)
{
echo $kk = $wing . $minion[$i+1].",".$minion[$i]."],<br>";
}
Just a small modification to the given answers
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$str = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
$str.='['.$minion[$i+1].','.$minion[$i].'],<br/>';
}
echo rtrim($str,','); // to trim ',' at the end
How can I store this in a $_var variable ?
$s_number = 5;
$spn = '6';
echo "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++)
{
echo "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
echo "]";
Question is bit vague though it seems probably you are looking for this,
$string = "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++) {
$string .= "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$string .= "]";
echo $string;
Other suggestions that use strings are fine, but I prefer to create arrays for tasks like this:
$s_number = 5;
$spn = '6';
$landscapes_array = array();
for ($i = 1; $i <= $s_number; $i++) {
$landscapes_array[] = "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$landscapes = "'Landscapes':[" . implode('', $landscapes_array) . "]";
You could also try putting the for loop in a function passing your variables and then return the values to a new variable. That's like saving a loop in a variable.
I am new to the concept of variable variables and don't think I fully understand it.
What I am trying to do is create a for loop that will populate an array based on a variable number of variables.
I am trying to replace: (manually hard coded)
$numCorrectArray = array(1=>$q01TotalCorrect, 2=>$q02TotalCorrect, 3=>$q03TotalCorrect, 4=>$q04TotalCorrect, 5=>$q05TotalCorrect, 6=>$q06TotalCorrect, 7=>$q07TotalCorrect, 8=>$q08TotalCorrect, 9=>$q09TotalCorrect, 10=>$q10TotalCorrect, 11=>$q11TotalCorrect, 12=>$q12TotalCorrect, 13=>$q13TotalCorrect, 14=>$q14TotalCorrect, 15=>$q15TotalCorrect, 16=>$q16TotalCorrect, 17=>$q17TotalCorrect, 18=>$q18TotalCorrect, 19=>$q19TotalCorrect, 20=>$q20TotalCorrect, 21=>$q21TotalCorrect, 22=>$q22TotalCorrect, 23=>$q23TotalCorrect, 24=>$q24TotalCorrect, 25=>$q25TotalCorrect, 26=>$q26TotalCorrect, 27=>$q27TotalCorrect, 28=>$q28TotalCorrect, 29=>$q29TotalCorrect);
With: (dynamic)
$numCorrectArray = array();
for($i=1; $i <= $stats->numberOfQuestions; $i++) {
if($i < 10) {
$questionNumber = "0" . $i;
} else {
$questionNumber = $i;
}
$varName = "q" . $questionNumber . "TotalCorrect";
array_push($numCorrectArray, $$varName);
}
How would I accomplish this? Thanks
The below method i think is easier to understand because is similar to normal PHP code. Here you can learn more.
$numCorrectArray = array();
$prefix = 'q';
$sufix = 'TotalCorrect';
for($i=1; $i <= 30; $i++) {
if($i < 10) {
$questionNumber = "0" . $i;
} else {
$questionNumber = $i;
}
${$prefix . $questionNumber . $sufix} = $i;
$numCorrectArray[$i] = ${$prefix . $questionNumber . $sufix};
}
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?