Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've tried out some code where I use a for loop in PHP to add up numbers but it seems to not work, how can be?
$n = 3; //or anything else
for($i=0;$i<$n;$i++){
$n = $n+$n;
}
echo $n;
This is just a test code for something else, but I'd still like it to work, please help
You are increasing the loop ending condition $n inside the loop
this means it will never exit the loop.
this is an infinite loop, and wont ever print anything.
also your loop needs to be $i = 0; $i < $n; $i++
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In contradiction with the documentation, in the following cycle (I am creating a select) when the variable $retval reaches 2048 characters, it's set to null.
foreach ($cache_ecmcategories as $category) {
$retval.= '<option value="'.$category['rowid'].'"';
if (($isfilter ? $category['label'] : $category['rowid']) == $defaulttx)
$retval.= ' selected="selected"';
$retval.= '>'.$category['label'].'</option>'; <== This line generate the problem
}
Also, change the PHP version from 5.6.25 to 7.0.10, the problem remains the same
What is the cause of this strange behavior?
I can't reproduce your issue on my server with the following script:
<?php
$foo='';
for($i=0; $i < 1000; $i++) $foo .= "cur:$i ";
var_dump($foo);
Even an additional $foo .= NULL; doesn't trigger the bug for me.
Are you really sure that the bug happens on your posted code? Have you tried to add additional debug statements?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Please am looking for a code or php function that can help me know how many 5 is in 25
the code below only returns 5,10,15,20
i want something that will tell 4 instead of breaking the division point
thanks in advance
for($i = 1; $i < 25; $i++)
{
if ($i % 5 == 0)
{
echo $i;
}
};
The reason is you limit your loop to stop when it hits 25. It will only loop when value is less than 25, so as soon as it reaches 25 it wont loop and so wont get your final set of 5s.
Try this:
for ($i = 1; $i <= 25; $i++)
{
if ($i % 5 == 0) {
echo $i;
}
};
isn't this just a simple division (25%5=5)?
the reason why your code gives your 4 is because you are actually not getting to 25 (the loop stops when i=25), if you will usefor($i=0;$i<=25;$i++) the loop will still run when I=25, and so it will show you 5.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
$vaccinesArr=array("0","0","0","0","0","0","0","0","0","0","0");
for($i = 0; $i < 11 ; $i++){
while($row = mysqli_fetch_assoc($result)){
if ($row['vaccID']== $i){
$vaccineArr[$i]++;
}
}
}
This is the part of my code, I need to increment according to this condition but I keep on getting syntax error, I searched but all the results I find are using foreach and incrementing all the indexes of the array while here I want to increment only one specific index. Can anyone help?
Your code will only work on the first iteration of the for loop, because when the inner while loop finishes there won't be any more rows to fetch from the query. You could rewind the query, but a better way to do it would be without the for loop.
while ($row = mysqli_fetch_assoc($result)) {
$vaccinesArr[$row['vaccID']]++;
}
Also, if the elements of $vaccinesArr are supposed to be numbers, you shouldn't put them in quotes. PHP will convert the strings to numbers when you use ++, but why be confusing?
$vaccinesArr = array(0, 0, 0, ....);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to print a new line character in browser but '\n' in echo statement does nothing. Whats wrong in this code?
$n = 5;
for($i= 0; $i<=$n; $i++)
{
for($j=0; $j <= $n-$i-1; $j++)
echo " ";
for( ; $j <= $n; $j++)
echo '* ';
echo "\n";
}
It's actually working, if you see your html source you will find it, whereas HTML needs <br> instead of new line character. if you want to convert new line to <br> you can use php function nl2br.
echo nl2br($stingHasNewLineChar);
The "echo" command in PHP sends the output to the browser as raw html so even if in double quotes the browser will not parse it into two lines because a newline character in HTML means nothing. That's why you need to either use:
echo [output text]."<br>";
when using "echo", or instead use fwrite...
fwrite([output text]."\n");
This will output HTML newline in place of "\n".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I skip a loop and go to the next one? Lets say my query in a loop returns result 'Nothing', I want to skip updating the base and go to the next loop. If I have 7 loops and my 5th loop returns 'Nothing' from my base, skip it don't do nothing beyond that point and just go to the next, 6th loop.
I have a for loop like so:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
if ($name = 'Nothing')
{
go to next loop;
}
updating my database and stuff;
}
EDIT: I have a $_SESSION variable at the end that stores all of my loops and displays them. I also don't want results with 'Nothing' to be stored there.
continue will end the current iteration and start the next one:
if ($name == 'Nothing')
// -------^^ Make sure you're using double (==).
{
continue;
}