Here is my code
for ($i=0; $i<$Percentile["Parameter_length"]; $i++)
{
echo "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
}
The above code will display
Eqt_Param0=2.00
Eqt_Param1=3.00
Eqt_Param2=1.00
Eqt_Param3=5.00
If I put echo() outside the for loop, I need the same result. Please help me to fix this...
How about this? Simply concatenating the result of the foreach into a variable which can be echoed.
$output = "";
for ($i = 0; $i < $Percentile["Parameter_length"]; $i++)
{
$output .= "Eqt_Param" . $i . "=" . $Percentile["Eqt_Param" . $i];
}
echo $output;
You need to store your values in a viarable that exists outside of the scope of the for
like:
$accumulatedString = '';
for ($i=0; $i<$Percentile["Parameter_length"]; $i++) {
echo "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
$accumulatedString .= "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
}
echo $accumulatedString;
Thats if you want it all as one string.
Related
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 am making a website that has x amount of input fields. Each of the input fields is labeled team0, team1, team2 ... teamx and they are wrapped in a post form. Upon posting, I would like to check the POST variables, store them in an array, print out their values, but it seems I cannot use a variable when calling $_POST. I have tried it like so:
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team$i'];
echo $teamNames[$i] . "<br>";
}
What is the correct way to do this?
Try double quotes
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST["team$i"];
echo $teamNames[$i] . "<br>";
}
OR
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team'.$i];
echo $teamNames[$i] . "<br>";
}
<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}
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 have this simple for loop to echo an array:
for ($i = 0; $i < count($director); $i++) {
echo ''.$director[$i]["name"].'';
}
The problem here is that when more than one element is in the array then I get everything echoed without any space between. I want to separate each element with a comma except the last one.
I can't use implode so I'm looking for another solution
This should work. It's better I think to call count() once rather than on every loop iteration.
$count = count($director);
for ($i = 0; $i < $count; $i++) {
echo ''.$director[$i]["name"].'';
if ($i < ($count - 1)) {
echo ', ';
}
}
If I remember PHP syntax correctly, this might also help:
$str = "";
for ($i = 0; $i < count($director); $i++) {
$str .= ''.$director[$i]["name"].', ';
}
$str = trim($str, ", ");
A better solution is to avoid looping altogether. I've ignored building the links for the sake of clarity. Note that I don't believe the inability to use implode is a condition. I believe it's a simple statement of, "I can't see how to make this work using implode, so I did it this way instead."
$last_entry = array_pop($director);
if(count($director) > 0) {
echo implode(", ", $director) . " and " . $last_entry;
} else {
echo $last_entry;
}
My preferred method:
$links = [];
for ($i = 0; $i < count($director); $i++) {
$links[] = '<a href="person.php?id='.$director[$i]["id"].'">' .
$director[$i]["name"] . '</a>';
}
echo implode(', ', $links);
Or
$output = "";
for ($i = 0; $i < count($director); $i++) {
if ($output) {
$output .= ", ";
}
$output .= '<a href="person.php?id='.$director[$i]["id"].'">' .
$director[$i]["name"].'</a>';
}
echo $output;
for ( $i=0 ; $i < count($arr)-1 ; $i++ )
{
echo ( $arr[$i]."," );
}
echo ( $arr[count($arr)-1] );
$number = count($director);
for ($i = 0; $i < $number; $i++) {
echo ''.$director[$i]["name"].'';
if($i < $number - 1){
echo ', ';
}
}
Oops, I didn't saw the reply by Tom Haigh, we came with practically the same.
How about something like this? You may want to store the result of "count($director)" in a variable outside the loop so that you do not have to waste resources recalculating it each time the loop is run.
for($i=0; $i<count($director);$i++){
echo ''.$director[$i]["name"].'';
if($i!=count($director)-1){echo ',';}
}
Well, foreach contains for :-)
foreach ($director as $key => $person) {
if ($key !== 0) echo ', ';
echo ''.$person['name'].'';
}
// RENAMED $director to $directors
$links = '';
foreach ($directors AS $director) {
$links .= "{$director['name']}";
if (true !== empty($links)) {
$links .= ', ';
}
}
echo $links;
foreach ($strings as $string){
$superstring .= $string . ', ';
}
$echostring = substr_replace($superstring ,"",-2);
echo $echostring;
Here's my 2 lines solution
// Create your Array
$cities = Array("Rome", "Florence", "Venice");
// implode them
$list = trim(implode (", ", $cities)) ;
// remove last comma
$list = substr ( $list,0 ,strlen( $list ) );
//check result
die ($list);
$count =1;
for ($i = 0; $i < count($director); $i++) {
if ($count!=1) {
echo ' , ';
}
echo ''.$director[$i]["name"].'';
$count++;
}