Assigning PHP variables in shell

For a project I am currently working on it was necessary to read PHP define()’d-values from a configuration file using a shell script that did some further processing.

In order to be able to read define()’d-values you need to first include() (or better require_once()) the source configuration file(s) and then assign its values to shell variables.

  1. Determine path to PHP:
    PHP_PATH="/opt/lampp/bin/php"
    
  2. Include any required configuration files:
    PHP_REQUIRE="require_once('$(pwd)/../src/config/base.php');"
    
  3. Finally assign PHP variables to shell:
    RELEASE_NUMBER=`$PHP_PATH -r "$PHP_REQUIRE echo RELEASE_VERSION;"`
    

Hint: In case your PHP installation is included in PATH you can simply write PHP_PATH=”php”.

In this example the release version number is read from a base-configuration file, which will be used by the shell script for further processing.

2 thoughts on “Assigning PHP variables in shell”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top