Loading ...
Sorry, an error occurred while loading the content.

Creating Standalone PHP Executables

Expand Messages
  • James
    Problem You want to create a standalone PHP executable that can be run at the console without requiring the presence of the PHP interpreter. Solution Create
    Message 1 of 1 , Jan 17, 2010
    • 0 Attachment
      Problem
      You want to create a standalone PHP executable that can be run at the console
      without requiring the presence of the PHP interpreter.

      Solution
      Create your PHP script in the usual way:
      File: print-primes.php

      <?php
      // list all primes between 2 and some integer
      // using the Sieve of Erastothenes
      function listPrimes($end) {
      // generate an array of all possible integers
      // between the first prime and the supplied limit
      $sieve = range(2, $end);

      // retrieve the size of the array
      $size = sizeof($sieve);
      // reset internal array pointer to beginning of array
      reset($sieve);
      // iterate over the array
      while (list($key, $val) = each($sieve)) {
      // for each element
      // check if subsequent elements are divisible by it
      // remove them from the array if so
      for ($x=$key+1; $x<$size; $x++) {
      if ($sieve[$x] % $val == 0) {
      unset($sieve[$x]);
      }
      }
      }
      // at the end, elements left in array are primes
      return $sieve;
      }
      // list all the primes between 2 and 100
      // result: "2 3 5 7...83 89 97"
      echo implode(" ", listPrimes(100));
      ?>
      Then use the Bambalam PHP EXE Compiler/Embedder (Windows) to turn it into
      an executable:
      shell> bamcompile.exe print-primes.php
      Bambalam PHP EXE Compiler/Embedder 1.21
      Mainfile: print-primes.php
      Outfile: print-primes.exe
      Encoding and embedding print-primes.php
      get-php-version.exe created successfully!
      shell> print-primes.exe
      2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

      Comments
      The Bambalam PHP EXE Compiler/Embedder, freely available from http://
      www.bambalam.se/bamcompile/, is a command-line tool to convert one or
      more PHP scripts into standalone Windows executables. This makes it possible to
      create independent PHP applications that will work on any Windows system, while
      also preventing easy access to the application’s source code. If a script uses one or
      more extensions, the compiler supports adding these extensions to the standalone
      executable to eliminate external dependencies. The previous listing illustrates the
      process of turning a PHP script into an executable.
      Note that the Bambalam PHP compiler doesn’t create machine code, but instead
      simply embeds the target PHP script(s) inside a launcher executable. This launcher,
      which is similar to the PHP interpreter, creates a PHP environment for the target
      PHP script(s) to execute.

      NOTE
      The Bambalam PHP compiler is an open-source tool, released without any warranties. As such, its
      behavior may not always be consistent and the final standalone executable may not always work
      as advertised, a problem most noticeable with the embedding of external extensions. Further,
      the compiler uses a PHP 4.x launcher, and doesn’t support PHP 5.x or PHP 6.x as of this writing.
      Extensive testing is recommended if you plan to use this tool for production use.
    Your message has been successfully submitted and would be delivered to recipients shortly.