Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to add something to PHP's loop variable and check the result? I have this simple code:
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " . $i + 1 . "<br/>";
}
?>
The displayed result in the page:
1
1
1
1
1
But if there's no addition with 1, the result is correct. How to do this addition?
You concat in your question use this:
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " . ($i + 1) . "<br/>";
}
?>
Change this line:
echo "Time: " . $i + 1 . "<br/>";
to this:
echo "Time: " . ($i + 1) . "<br/>";
Put the calculation in brackets:
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " . ($i + 1) . "<br/>";
}
?>
While adding parentheses to give the addition a higher precedence is an option, the easiest way is to turn $i + 1 into an expression, to be resolved to a singular value and then pass it to echo, using comma's:
for ($i=0; $i<5; $i++)
{
echo "Time: " , $i + 1 , "<br/>";
}
But the shortest way to write this is:
for ($i=0;$i<5;)//don't increment here
{
echo 'Time: ', ++$i, '<br/>';//increment here, and make sure to pre-increment
}
Or, more readable (and IMHO therefore better):
for($i=1;$i<6;$i++)
{//start with one, so you needn't increment in the loop body!
echo 'Time: ', $i, '<br/>';
}
Leaving out conactenation is marginally faster, but you'll hardly notice that. It's just an alternative. Think of the two options as this (if you're familiar with C++):
int i = 0;
std::stringstream toPrint;//create string-stream
toPrint << "Time: " << ++i << "<br/>";//pass substrings/int chunks to stream
std::COUT << toPrint.str();//create single string, and pass to output-stream
But that's just silly, considering you're actually creating a stream, just to pass your substrings/ints to, only to pass the resulting string to the output stream. You might as well pass all chunks to the output stream directly:
std::<COUT << "Time: " << ++i << "<br/>"; //pass string, int and string to output
Think of echo as a language construct (which it is) that is your access-point to the STDOUT stream. Whe concatenate a string manually, of you can just pass it to the stream as-is? without any overhead?
As an asside, you can get the output you need in just 2 lines of code, without (explicitly) looping and incrementing a variable:
$vals = range(1,5);//create array 1, 2,3, 4, 5
//repeat format Time: %d<br/> for every index in $vals
vprintf(str_repeat('Time: %d <br/>', count($vals)), $vals);
//pass resulting format to vprintf, allong with the array of values
The output is the same. In case you're thinking about using this: don't. It's just for fun, and an example of how you can take compacting code a bit too far
<?php
for ($i=1; $i <= 5; $i++) {
echo "Time: " . ($i) . "<br />";
}
You are looking to do something like this :
<?php
for ($i=0; $i<5; $i++) {
$j=$i+1;
echo "Time: " . $j . "<br/>";
}
?>
Like DanFromGermany suggested, why not start with $i one higher than currently?
Also if you have to do calculations, the meaning of the var usually differs from it's starting point. So for readability and reusability do what Munjal proposed, but leave out the concatenation;
<?php
for ($i=0; $i<5; $i++) {
$laterTime = $i + 1;
echo "Time: {$laterTime} <br />\n";
}
?>
<?php
for ($i=0; $i<5; $i++) {
$i++;
echo "Time: ".$i. "<br/>";
}
?>
or if you insist on concatenation
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " .++$i. "<br/>";
}
?>
Related
I'm working on pagination for my website, yet I'm stuck on the following piece of code. I've been messing around with it for at least an hour, and can't seem to wrap my head around what's going on with the output.
Of course the if statement executes once.
And as expected the first echo ... returns 1.
However, for some reason the second echo ... returns 0 as a float instead of <div>1</div> as a string...
$rowCount = 5;
$pgCount = ceil($rowCount / 10);
$pgParamArray["page"] = $pgCount;
$pgArray = array("", "", "", "", "");
for ($i = 0; $i < 5; $i++) {
if ($pgParamArray["page"] - $i > 0) {
echo $pgParamArray["page"] - $i;
$pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
echo $pgArray[$i];
}
}
I have tried setting $pgArray as array() and array($v1, $v2, $v3, $v4, $5) with no luck.
Even though var_dump($pgParamArray) returns float, I tried converting $rowCount, which is initially a string from the database, to a number anyways. No dice again.
echo $pgArray["0"] also returns 0.
var_dump($pgArray[0]) also returns float.
var_dump($pgArray) obviously returns array.
However, var_dump($pgArray) returns array(5) { [0]=> string(7) ...
I have absolutely zero idea why $pgArray[0] returns 0, yet var_dump($pgArray) returns array(5) { [0]=> string(7) .... That makes zero sense to me. Anybody know why $pgArray[0] is resolving to 0?
Wrapping your statement in parenthesis seems to work for me:
$pgArray[$i] = "<div>" . ($pgParamArray["page"] - $i) . "</div>";
Without the parenthesis, it seems that the value breaks completely; the page doesn't print the <div> tags at all, but rather just adds a trailing 0 to the already printed 1.
I would assume that it's due to how PHP processes string concatenation, though I wouldn't be able to give you an exact answer as to why this happens. Just to be safe, I'd always either store any equations in variables before you pass them in, or perform all operations inside of parenthesis.
That way, you won't have weird encounters like this (for example):
echo "<div>" . 1 + 1 . "</div>"; // returns 1
echo "<div>" . (1 + 1) . "</div>"; // returns 2
You need to group the arithmetic part as mentioned in the accepted answer:
$pgArray[$i] = "<div>" . ($pgParamArray["page"] - $i) . "</div>";
To understand what's going on here, you need to run it on the command line:
php > $rowCount = 5;
php >
php > $pgCount = ceil($rowCount / 10);
php >
php > $pgParamArray["page"] = $pgCount;
php >
php > $pgArray = array("", "", "", "", "");
php >
php > for ($i = 0; $i < 5; $i++) {
php {
php { if ($pgParamArray["page"] - $i > 0) {
php {
php { echo $pgParamArray["page"] - $i;
php {
php { $pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
php {
php { echo $pgArray[$i];
php {
php { }
php {
php { }
10</div>
php >
As you can see, the output of echo $pgParamArray["page"] - $i; is 1, immediately followed by 0</div> as the contents of $pgArray[$i].
So the real issue is, what is happening to the <div> in
$pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
After seeing the real output, the answer is a little more obvious now: it's a grouping issue. PHP is simply taking it left to right:
((("<div>". $pgParamArray["page"]) - $i) . "</div>")
((("<div>" . 1 ) - $i) . "</div>")
((("<div>1") - $i ) . "</div>")
((( 0 ) - $i ) . "</div>")
((( 0 ) - 0 ) . "</div>")
((( 0 )) . "</div>>")
((( "0</div>" )))
Im trying to get out an average value from a vote function.
<?php
$file = file("textfile.txt");
$textfil = file_get_contents("textfile.txt");
$textfill = str_split($textfil);
echo "Number of votes: " . count($textfill) . "<br>";
$sum = 0;
foreach ($textfill as $vote) {
$sum = $sum + intval($vote);
}
echo "Average: " . $sum;
?>
Simple by substitute (+) with a (/), and even tried a (%). But still getting error message.
Would appreciate alot if anyone could help me out and tell me what im doing wrong.
/thanks
Edit
Sidenote: Please read an explanation under "First answer given" further down below.
This version will take into account any blank lines in a file, if the content looks like:
1
2
3
// <- blank line
Sidenote: Please provide a sample of your text file. A comment has already been given to that effect.
PHP
<?php
// first line not required
// $file = file("textfile.txt");
$textfil = file_get_contents("textfile.txt");
$textfill = array_filter(array_map("trim", file("textfile.txt")), "strlen");
echo "Number of votes: " . count($textfill) . "<br>";
$sum = 0;
foreach ($textfill as $vote) {
$sum += intval($vote);
}
$avg = $sum / count($textfill);
echo "Average: " . $avg;
?>
First answer given
Using the following in a text file: (since no example of file content was given)
5
5
5
IMPORTANT NOTE: There should not be a carriage return after the last entry.
produced
Number of votes: 5
Average: 3
which is false, since there are 3 entries in the text file.
explode() should be used, and not str_split()
The following using the same text file produced:
Number of votes: 3
Average: 5
which is correct. In simple mathematics, averages are done by adding all numbers then dividing them by how many numbers there are.
In this case it's 3 numbers (all 5's) added equals 15, divided by 3 is 5.
Sidenote: The first line is not required $file = file("textfile2.txt");
<?php
// first line not required
// $file = file("textfile.txt");
$textfil = file_get_contents("textfile.txt");
$textfill = explode("\n", $textfil);
echo "Number of votes: " . count($textfill) . "<br>";
$sum = 0;
foreach ($textfill as $vote) {
$sum += intval($vote);
}
$avg = $sum / count($textfill);
echo "Average: " . $avg;
?>
Footnotes:
If the average comes out to 8.33333 and you would like it to be rounded off to 8, use:
echo "Average: " . floor($avg);
If the average comes out to 8.33333 and would like it to be as 9 you would use:
echo "Average: " . ceil($avg);
ceil() function
floor() function
You may be mixing in stuff that can't be divided, like text, etc. I don't know what your text file looks like. intval may be having a problem with arrays. You may try:
foreach ($textfill as $vote) {
if(is_int($vote) {
$sum += $vote;
}
}
echo "Average: " . $sum;
Lower school math says:
foreach ($textfill as $vote) {
$sum += intval($vote);
}
$avg = $sum / count($textfill);
The average value is calculated by divide the sum with the number of votes. This line will print the average value:
echo "Average: " . $sum/count($textfill);
for ($i=1; $i<=4; ++$i) {
echo "The number is " . $i . "\n";
}
This will output:
The number is 1
The number is 2
The number is 3
The number is 4
How can i make a loop that will give me output like so:
The number is 1
The number is 1
The number is 1
The number is 1
The number is 2
The number is 2
The number is 2
The number is 2
etc
Thanks for any help.
Without nested loops: this would suffice for a single loop.
for($i=0;$i<9*4;$i++)
{
echo "The number is ".(1+floor($i/4));
}
So you want
for ($i=1; $i<=2; ++$i) {
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
}
But let's avoid the repetition with a loop!
for ($i=1; $i<=2; ++$i) {
for ($j=1; $j<=4; ++$j) {
echo "The number is " . $i . "\n";
}
}
Essentially, you want to print something four times inside the loop... so you can write four echo statements. A better way to do this would be to use nested for loops.
for ($i=1; $i<=4; ++$i) {
for ($j=1; $j<=4; ++$j) {
echo "The number is " . $i . "\n";
}
}
For every iteration of the outer loop, the inner one prints the statement four times. One thing to be careful with nested loops is the variables used in the conditions. If you mix them up, you could have weird issues including an infinite loop.
You need two nested loops, like so:
for( $i = 1; $i <= 4; ++$i) {
for( $j = 1; $j <= 4; ++$j) {
echo "The number is " . $i . "\n";
}
}
One of a million of possible solutions could be using single loop and str_repeat() function.
for ($i=1; $i<=4; $i++)
echo str_repeat("The number is $i\n", 4);
which is probably the best way to make multiple repeats of same string.
you can make two loops
for($i = 1; $i <= 4; $i++) {
for($j = 1; $j <= 4; $j++) {
echo 'The number is '.$i."\n";
}
}
You've got the same loop, but four iterations within:
for ($i=1; $i<=4; ++$i) {
for($j=0;$j<4;$j++) {
echo "The number is " . $i . "\n";
}
}
I am just a beginner in PHP. I am trying to write program to print numbers like following.
1 1
12 21
123 321
1234 4321
1234554321
I have written the following code.
<?php
$n=5;
for($i=1; $i<=$n; $i++)
{
echo "<br />";
for($j=1; $j<=$i; $j++)
{
echo $j;
}
}
?>
The result displays the following.
1
12
123
1234
12345
I could not reverse it like
1
21
321
4321
54321
How can I do this?
The simplest, hard coded version:
<?php
$text = "1 1
12 21
123 321
1234 4321
1234554321";
echo $text;
?>
Edit
A more generic solution:
<?php
$n = 5;
$seq1 = '';
$seq2 = '';
$format1 = sprintf("%%-%su", $n); //right justified with spaces
$format2 = sprintf("%%%su", $n); //left justified with spaces
for($i=1; $i<=$n;$i++){
$seq1 .= $i;
$seq2 = strrev($seq1);
echo sprintf("$format1$format2\n", $seq1, $seq2);
};
?>
Okay. What you wrote is pretty good. There need to be several changes in order to do what you wanted though. The first problem is that you are rendering it to HTML - and HTML does not render spaces (which we'll need). Two solutions: you use for space, and make sure you use a proportional font, or you wrap everything into a <pre> tag to achieve pretty much the same thing. So, echo "<pre>"; at the start, echo "</pre>"; at the end.
Next - don't have the inner loop go to $i. Let it go to 5 every time, and print a number if $j <= $i, and a space otherwise.
Then, right next to this loop, do another one, but in reverse (starting with 5 and ending with 1), but doing the very same thing.
Viola is a musical instrument.
Here is my solution to your problem.
It isn't the best solution because it doesn't take into account that you could be using numbers higher than 9, in which case it will push the numbers out of line with each other.
But the point is that it is still the start of a solution that you could work on if needed.
You can use an array to store the numbers you want to print.
Because the numbers are in an array it means we can just use a foreach loop to make sure all of the numbers get printed.
You can use PHP's str_repeat() function to figure out how many spaces you need to put in between each string of numbers.
The below solution will only work if you use an array with the default number indicies as opposed to an associative array.
This is because it uses the $key variable in part of the calculation for the str_repeat() function.
If you would rather not use the $key variable then you should be able to figure out how to change that.
When it come to reversing the numbers they have already been stored in a string so you can just use PHP's strrev() function to take care of that and store them in another variable.
Finally you just have to print a line to the document with a line break at the end.
Note that the str_repeat() function is repeating the HTML entity.
This is because the browser will just compress normal white space down to 1 character.
Also note that I have included a style block to change the font to monospace.
This is to ensure that the numbers all line up with each other.
<style>
html, body {
font: 1em monospace;
}
</style>
<?php
$numbers = array(1, 2, 3, 4, 5);
$numbers_length = count($numbers);
$print_numbers = '';
$print_numbers_rev = '';
foreach($numbers as $key => $value) {
$spaces = str_repeat(' ', ($numbers_length - ($key + 1)) * 2);
$print_numbers .= $value;
$print_numbers_rev = strrev($print_numbers);
echo $print_numbers . $spaces . $print_numbers_rev . '<br />';
}
Edit:
Solution without array:
<style>
html, body {
font: 1em monospace;
}
</style>
<?php
$numbers = 9;
$numbers_length = $numbers + 1;
$print_numbers = '';
$print_numbers_rev = '';
for($i = 0; $i <= $numbers; ++$i) {
$spaces = str_repeat(' ', ($numbers_length - ($i + 1)) * 2);
$print_numbers .= $i;
$print_numbers_rev = strrev($print_numbers);
echo $print_numbers . $spaces . $print_numbers_rev . '<br />';
}
$n = 5;
for ($i = 1; $i <= $n; $i++) {
$counter .= $i;
$spaces = str_repeat(" ", ($n-$i)*2);
echo $counter . $spaces . strrev($counter) . "<br/>";
}
<div style="position:relative;width:100px;height:auto;text-align:right;float:left;">
<?php
$n=5;
for($i=1; $i<=$n; $i++)
{
echo "<br />";
for($j=1; $j<=$i; $j++)
{
echo $j;
}
}
?>
</div>
I'm trying to modify a joomla module and I have a small problem (which is not joomla related, thus no code necessary).
I have a foreach loop which has a block of code in it which displays an article. It repeats itself as many times as you set it up in the admin panel. I want to add the feature that makes this module display items on more than 1 column. All I need is the perk, I think I have everything else covered.
Basically how do I modify a simple foreach loop so that it displays articles on more than one column?
Instead of this
a
b
c
d
e
I want this
a ........ d
b ........ e
c
You can get the count of results and work from the middle if you're sticking with tables
$half_count = floor(count($entries) / 2);
for($i=0;$i<$half_count;$i++)
{
echo '<tr>';
echo '<td>' . $entries[$i] . '</td>';
echo '<td>' . (isset($entries[$half_count + $i]) ? $entries[$half_count + $i]: '') . '</td>';
echo '</tr>';
}
Here a simple way to do it :
php > $arr = array(1,2, 3, 4, 5, 6, 7, 8, 9, 10);
php > for ($i=0; $i<count($arr); $i+=2) { print $arr[$i] . "\t" . $arr[$i+1] . "\n"; }
1 2
3 4
5 6
7 8
9 10
As far as I know, you won't be able to do it with a foreach statement but with a for.
For example:
$iterations = (count($array_of_items) % 2) ? (count($array_of_items) / 2) + 1 : count($array_of_items) / 2;
for ($i = 0; $i <= $iterations; $i++) {
if (isset($array_of_items[$i+3]))
echo $array_of_items[$i].'........'.$array_of_items[$i+3];
else
echo $array_of_items[$i];
}
Really simple code without so little info but could make the trick!
Instead of outputting in the for loop, I would create two arrays of the articles in the for loop. Then loop through those arrays to create your columns outside of the main loop.
You have little info in the question, but here's something you could use
$arr = array("a","b","c","d","e","f");
for ($i = 0; $i<count($arr); $i++){
echo $arr[$i]." ". $arr[$i+3] ."\n";
if($i == 2){ break;} //Modify 2 as more alphabets are added
}
Outputs
a d
b e
c f
For a generic solution, assuming a packed array....
function show_table($data, $columns)
{
$items=count($data);
$iters=$items/$columns + ($items % $columns) ? 1 : 0;
for ($y=0; $y<$iters; $y++) {
for ($x=0; $x<$columns; $x++) {
$offset=$y*$columns + $x;
if ($offset<$items) print $data[$offset] . ' ';
}
print "\n";
}
}