Viewed   171 times

Is there a way to timeout a function? I have 10 minutes to perform a job. The job includes a for loop, here is an example:

foreach($arr as $key => $value){
   some_function($key, $value); //This function does SSH and SFTP stuff

$arr has 15 elements and some_function() sometimes may take more than 1 minutes. In fact once it got hanged for 5 minutes.

Is there a way I can timeout the function call and move on with next element in $arr?

Thank you!!



It depends on your implementation. 99% of the functions in PHP are blocking. Meaning processing will not continue until the current function has completed. However, if the function contains a loop you can add in your own code to interrupt the loop after a certain condition has been met.

Something like this:

foreach ($array as $value) {

function perform_task($value) {
  $start_time = time();

  while(true) {
    if ((time() - $start_time) > 300) {
      return false; // timeout, function took longer than 300 seconds
    // Other processing

Another example where interrupting the processing IS NOT possible:

foreach ($array as $value) {

function perform_task($value) {
    // preg_replace is a blocking function
    // There's no way to break out of it after a certain amount of time.
    return preg_replace('/pattern/', 'replace', $value);
Monday, November 28, 2022

At the moment you are just returning the last data row. Change your code like this to return an array of all your rows from that function:

$rows = array()
foreach($data->result() as $row){

    if($row->Thumb_Url == NULL){
        $image = base_url().'assets/images/no_photo_thumb.png';
        $image = $row->Thumb_Url; 

    $new_data = new stdClass;
    $new_data->First_Name = $row->First_Name;
    $new_data->Last_Name = $row->Last_Name;
    $new_data->User_Name = $row->User_Name;
    $new_data->Thumb_Url = $image;

    $rows[] = $new_data;

return $rows;

This way every row returned from the database will be added to an array named $rows. At the end you have to return your new array.

Saturday, August 6, 2022

You can use the global keyword:

$a = "Hello World";
$b = "Hello World";

function outputStrings(){
  global $a, $b;
  echo $a." - ".$b;

$b = "Goodbye World";

outputStrings(); // ouputs "Hello World - Goodbye World"

However, its best not to use this structure. Its generally confusing and will make your code difficult to maintain. Wordpress uses this approach a lot in their code base and it makes for very tricky debugging. Other plugins and code can interject and modify global variables, changing the output of your script.

What would be better would be to either:

Use an OOP structure for your web app.

This way you can use objects instead of just random global variables. This gets around the issue of you accidentally overwriting a global variable in the course of a script. It also helps to organise the variables correctly, so all variables concerning users can be in the User class. It makes more sense to structure it like that.

class User {
  private $firstName;
  private $secondName;
  private $gender;

  public function __construct($fname, $sname, $gend){
    $this->firstName = $fname;
    $this->secondName = $sname;
    $this->gender = $gend;

  public function outputDetails(){
    echo $this->firstName." ".$this->secondName." is ".$this->gender;

$user = new User("Thomas", "Clayson", "Male");

Pass variables into functions

Just like you've shown in your example. This is the generally accepted standard way of doing this. You should always pass in variables like this, it helps you define scopes and a proper structure. Also it means you know what the value of variables is, as you own them and pass them to functions, rather than just plucking variables from global scope.

Wednesday, November 2, 2022

I have found a simple solution: do not include an update function in your extension of the widget.

The core simply returns the $new_instance variable as a default (wp-includes/widgets.php):

function update($new_instance, $old_instance) {
    return $new_instance;

I have tested this with success in my own widget and have determined that making your own update function is useful for filtering user input, but it does not seem necessary.

Wednesday, November 16, 2022

Not sure if this is what you want, as I didn't read all of that, but check out:

call_user_func_array('array_diff', $values)

Maybe that's what you want.

Saturday, December 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :