How many multiple 5 is in 25 using php [closed] - php

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.

Related

strlen() not working as intended - no errors [closed]

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 5 years ago.
Improve this question
I'm trying to check if the $field['message'] is less than 5 letters and then check again if it's more than 100 letters, but I'm not passing the first rule - it's output the result for the $field['message'] < 5 - why?
} elseif (strlen($field['message'] < 5)) {
// some output for < 5 case
} elseif (strlen($field['message'] > 100)) {
// some output for > 100 case
}
And a result is that if I use less or more than 5 letters, I'm getting the same output for < 5 case.
What am I doing wrong here? Is it because of the array?
elseif (strlen($field['message'] < 5))
You put the parentheses in wrong places. Should be:
elseif (strlen($field['message']) < 5)

Why does $n = $n + $n not work [closed]

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++

PHP Pushing at the end of multidimensional array [closed]

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 8 years ago.
Improve this question
So I am trying to do the following.
I have an array that contains 12 numbers(months).
$array = array();
$array[1] = array();
and so on.
Now, I am trying to push numbers into second place like
$array[5][day should be pushed here] trying like so
$counter = 0;
while($counter <= $elements)
{
if($something == $something)
{
$startfrom = 14; //example
$month = 5; //example
while($startfrom <= 20)
{
array_push($array[$month], $startfrom );
$startfrom ++;
}
}
But its always returning an error. Like this
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in F:\xampp\htdocs\NewPassword\Testt\process_info.php on line 27
What I am trying to achieve is that each day goes to its responding month.
Thanks.
Edit:
Nwm, fixed I did biggest rookie mistake I could. I wasn't incrementing counter out of if statement... Thank you APerson.
Should be:
$counter = 0;
while($counter <= $elements)
{
if($something == $something)
{
$startfrom = 14; //example
$month = 5; //example
while($startfrom <= 20)
{
array_push($array[$month], $startfrom );
$startfrom ++;
}
$counter++;
}
I'm going to first point out in your referenced code that there's no indication that the outermost while loop is ever terminated (or even closed properly for that matter ;) so assuming infinite recursion is not actually not the source for PHP's memory leak, I'd encourage you to first:
Grab a solid debugger like Xdebug, or (if you've the money) PHPEd so that you can profile your scripts and get detailed information about any issues you may have in your code or php settings.
If you can affirm via debugging that there's no identifiable issues with your scripts (this is almost always sure to be the problem though), then you can simply increase the memory_limit in your php.ini file. I'd be weary of going to high here. If you have to surpass 512M then something is seriously wrong.

PHP Version - difficulty coding for a lower version [closed]

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 8 years ago.
Improve this question
My client is using PHP version 5.2.0, and I have some code that works perfectly, but uses 5.4.7 at the moment. When my code is run on their server, I receive a parse error. I've searched online for an alternative way to write this code, but I can't find anything. If anyone has any tips that would be a great help.
**$count = mysqli_query($con, "SELECT COUNT(*) FROM menuitem")->fetch_row()[0];** //I want to make sure the previous and next button are only displayed exactly to the number of items
//in my database. I have to count those items, and turn that mysqli_query object into data. fetch_row[0] stores them
//in an array, starting at 0, turning it into an integer I can use to count below.
$prev = $start - 3;
if ($prev >= 0) {
echo '<td>Previous Items</td>';// Set your $start - 3 since our offset is 3, and pass that to a variable.
} //if $start - 3 is still greater than 0, then display the previous button. Logically, that is page 2.
$next = $start + 3;
if ($next < $count) {
echo '<td>Next Items</td>';// Since i used fetch_row[0] I now know how many rows I have. If $start + 3 is less than the $count query
} // than you will have a next button. If that number exceeds the number of rows I have, then no next button.
echo "</tr>";
echo "</table>";
In the code above, the ...->fetch_row()[0]; is the section that is bringing back the error. PHP 5.2.0 does not like it.
Prior to PHP 5.4, you could not dereference arrays returned from function calls. Simply do:
$count = mysqli_query(...)->fetch_row();
$count = $count[0];

How do I run a loop with a variable number of iterations in constant time? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here I am rephrasing the question. Coz some of the people couldnt understand it and started giving negative votes
I have the following PHP code:
$count = 100000;
$array = array();
for($i=0,$i<=$count,$i++)
{
array_push($array,mt_rand(1111111111111111,9999999999999999));
}
As per current code the time to execute this code will increase as I increase $count.
I am looking for a solution / algo / technique by which I can keep the time to execute in seconds no matter whatever is the value for $count. I am running this on 8 cores cpu, I am OK changing language but not ok upgrading hardware.
This should give you approximately constant time for values of $count < $maxcount. However, I wouldn't ever do this - it is just timewasting.
$maxcount = 100000
$count = 100000;
$array = array();
// Create required array
for($i=0,$i<=$count,$i++)
{
array_push($array,mt_rand(1111111111111111,9999999999999999));
}
// Pad out time to $maxcount iterations, to keep time constant
for($i=$count,$i<=$maxcount,$i++)
{
$dummy = mt_rand(1111111111111111,9999999999999999);
}

Categories