Limiting Number of Times a Loop Runs in PHP

limiting number of times a loop runs in php

If you want to use foreach, you can add an additional variable to control the number of iterations. For example:

$i=0;
foreach ($butters->users->user as $user) {
if($i==10) break;
$id = $user->id;
$name = $user->screen_name;
$profimg = $user->profile_image_url;
echo "things";
$i++;
}

Is there a maximum amount of iterations PHP can loop?

The browser would freeze since it won't get a response, unless it get's a 500 or any other error status/response. PHP, in theory, could run indefinitelly.

For example, I use something like this to handle some "jobs" in the background:

while(true) {
// and I do some wonders here
// if some conditions are met
}

When it comes to your browser, yes - it will freeze whilst awaiting the 200 OK response code which will definitelly not arrive because of the web server TimeOut configuration, PHP's max_execution_time setting (and others) ... and many other factors, not mentioning the size of the data transmitted into your browser, which will do the actual "crash" of your browser in a case where you iterate and echo each number from 0 to 999999999999999999.

Your for loop will reach it's destination of 999999999999999999 (probably not in your Browser, but definitelly in the terminal), unless something else stops it or breaks it (ie. CTRL+C, condition, max_execution_time, etc).

how to limit foreach loop to three loops

Slice the array.

foreach(array_slice($section['Article'], 0, 3) as $article ):

PHP loop X amount of times

for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }

how to limit foreach loop to four loops

Just use a dummy count variable like this:

$Count = 0;
foreach($Array as $Key => $Value){
//your code

$Count++;
if ($Count == 4){
break; //stop foreach loop after 4th loop
}
}

PHP looping limit the number

// Per-request limit
$limit = 300;

// Get array of numbers
$numbers = explode(';', $_REQUEST['string_of_phone_number_eg_0123456;0124357;0198723']);

// Get message
$message = trim($_REQUEST['message']);

// Loop numbers
while ($numbers) {

// Get a block of numbers
$thisBlock = array_splice($numbers, 0, $limit);

// Build request URL
$url = "http://smsexample.com/sms.php?destinationnumber=".urlencode(implode(';', $thisBlock))."&messagetosms=".urlencode($message);

// Send the request
$response = file_get_contents($url);

}

How to limit items from while loop

In SQL:

$select = "SELECT * FROM nk_showcase LIMIT 0,10";

or in PHP:

$counter = 0;
$max = 10;

while (($user = $db->fetch($query)) and ($counter < $max))
{
... // HTML code here....

$counter++;
}

As to the rotating, see @Fayden's answer.

How to decrease time of loop

Here is one solution that does not require PHP loop and string concatenation at all. But you still need PHP code (left as exercise) to convert this:

$post = array(1, 2, 3);

Into this:

SELECT 1 AS val UNION ALL
SELECT 2 UNION ALL
SELECT 3

And this is the one query you need to execute:

INSERT INTO next_table
SELECT main_table.id, userval.val
FROM main_table
JOIN (
SELECT 1 AS val UNION ALL
SELECT 2 UNION ALL
SELECT 3
) AS userval

Spoiler:


$post = array(1, 2, 3);
$sql = array_reduce($post, function($acc, $value){
$cur = $acc === "" ?
sprintf("SELECT %d AS val", $value) :
sprintf("\nUNION ALL SELECT %d", $value);
return $acc . $cur;
}, "");

PHP loop x number of times, using while and if

The following change should do it:

$count = 0;
while (($row = mysql_fetch_array($result)) && ($count < 6))
{

$city = $row['city'];
$id= $row['id'];
$miles = calculateDistance($lat, $lon, $point2_lat,$point2_lon);

if ($miles < 100.0) {
echo $city . " is about " . round($miles) . " miles away.<br />";
$count = $count + 1;
};
};

There are more elegant ways...

PHP: Limit foreach() statement?

You can either use

break;

or

foreach() if ($tmp++ < 2) {
}

(the second solution is even worse)



Related Topics



Leave a reply



Submit