title: PHP+fork(): How to run a fork in a PHP code
body: <p>I am running my code on CodeIgniter - Ubuntu Server. </p>
  
  <p>I have been researching for async ways to run functions. </p>
  
  <p>This is my function: </p>
  
  <pre><code>&lt;?php   
  
      // Registers a new keyword for prod to the DB. 
      public function add_keyword() {
  
          $keyword_p = $this-&gt;input-&gt;post('key_word');
  
          $prod      = $this-&gt;input-&gt;post('prod_name');
          $prod      = $this-&gt;kas_model-&gt;search_prod_name($prod);
          $prod      = $prod[0]-&gt;prod_id;
  
          $country   = $this-&gt;input-&gt;post('key_country');
  
          $keyword = explode(", ", $keyword_p);
          var_dump($keyword); 
          $keyword_count = count($keyword);
          echo "the keyword count: $keyword_count";
  
          // problematic part that needs forking
          for ($i=0; $i &lt; $keyword_count ; $i++) { 
  
              // get new vars from $keyword_count
              // run API functions to get new data_arrays
              // inserts new data for each $keyword_count to the DB 
  
          }
  
          // Redirect to main page. 
          redirect('banana/kas'); 
  
      }
  </code></pre>
  
  <p>The "foreach" uses variables with slow APIs, and updates the database. </p>
  
  <p>I saw some tutorials using fork but haven't quite understood the syntax part of it. Most of the things I found were just explanations of how it works (2 processes: parent-child ect') but non-gave a good explanation of how applying this on the code. </p>
  
  <p>Can someone explain how I work with the fork() syntax? </p>
  
  <p><a href="https://stackoverflow.com/questions/3833013/continue-php-execution-after-sending-http-response">Continue PHP execution after sending HTTP response</a></p>
  
  <p><a href="http://www.onlinetechtutorials.com/2014/06/how-to-run-php-code-asynchronously.html" rel="nofollow noreferrer">http://www.onlinetechtutorials.com/2014/06/how-to-run-php-code-asynchronously.html</a></p>
  
  <p><a href="http://php.net/manual/en/function.pcntl-fork.php" rel="nofollow noreferrer">http://php.net/manual/en/function.pcntl-fork.php</a> (more general)</p>
  
  <p>From the server side: <a href="https://www.youtube.com/watch?v=xVSPv-9x3gk" rel="nofollow noreferrer">https://www.youtube.com/watch?v=xVSPv-9x3gk</a></p>
  
  <p><strong>EDIT</strong>: </p>
  
  <p>Did I get it right? </p>
  
  <pre><code>&lt;?php   
  
  // Registers a new keyword for prod to the DB. 
  public function add_keyword() {
  
      $keyword_p = $this-&gt;input-&gt;post('key_word');
  
      $prod      = $this-&gt;input-&gt;post('prod_name');
      $prod      = $this-&gt;kas_model-&gt;search_prod_name($prod);
      $prod      = $prod[0]-&gt;prod_id;
  
      $country   = $this-&gt;input-&gt;post('key_country');
  
      $keyword = explode(", ", $keyword_p);
      var_dump($keyword); 
      $keyword_count = count($keyword);
      echo "the keyword count: $keyword_count";
  
      for ($i=0; $i &lt; $keyword_count ; $i++) { 
          // create your next fork
          $pid = pcntl_fork();
  
          if(!$pid){
              //*** get new vars from $keyword_count
              //*** run API functions to get new data_arrays
              //*** inserts new data for each $keyword_count to the DB 
              print "In child $i\n";
              exit($i);
              // end child
          }
      }
  
      // we are the parent (main), check child's (optional)
      while(pcntl_waitpid(0, $status) != -1){
          $status = pcntl_wexitstatus($status);
           echo "Child $status completed\n";
      }
  
      // your other main code: Redirect to main page. 
      redirect('banana/kas'); 
  
  }
  ?&gt;
  </code></pre>
  
  <p>This won't cause any problem with using this inside a loop? will it know to stack each process? </p>