Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8311877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:57:30+00:00 2026-06-08T19:57:30+00:00

I’m running a foreach loop in php which takes longer to execute than my

  • 0

I’m running a foreach loop in php which takes longer to execute than my maximum execution time of 30 seconds. The loop sends individual emails to users.

Instead of running cron jobs every 30 seconds and creating queues for records is it unethical to just restart the counter in the loop using set_time_limit(30) ?

$i = 0; //start count from 0

foreach ($users as $user): 

    //limit emails sent
    if(++$i == 100) break; //ends execution of loop

    set_time_limit(30); //restart timeout counter

    send_email($user); //send email to user

endforeach;

I’m new to this but with the code above I think I’m giving each email 30 seconds to complete but also breaking the loop when 100 emails are sent so the script doesn’t run forever.

Update: set_time_limit(0) goes against hosting TOS, I believed that restarting the timeout counter restarts the script as well as would CRON

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:57:32+00:00Added an answer on June 8, 2026 at 7:57 pm

    Running set_time_limit in foreach loop with brings and solves few problems at the same time.

    I see the greatest pro of this solution in making sure that no request will take more than 30 seconds long (and when you have a full cue I believe it’s even desirable to cut every script that takes that long) .

    The problem it brings it’s that no all jobs will be executed necessarily. Maybe you’ll experience some problems in the middle of jobs queue and it will all fail.

    I would go with this:

    # crontab
    0,30 * * * * php /path/to/your/script.php
    

    And would use your script.

    If you need to execute jobs as fast as possible, I’d create a bash script that would execute (without any timeout) php script as long as it wouldn’t finish with exit(0) (all jobs executed successfully) or wouldn’t return "Done!" or whatever you like.

    Exampe bash script: 1,2

    #!/bin/bash
    # Note that false sets $? to 1
    false
    while [ $? -ne 0 ]; do
        php /path/to/your/script.php >> log.log
    done
    

    And if you need to make sure no two instances will run at the same time, google one of those (just from the top of my head):

    • .pid file
    • mysql LOCK TABLE

    PS: If you’ll use some of the method make sure that your script will work if it crashes in the middle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.