2

Does anyone know how to get bash files to render php constants? For example if i create a php file containing,

define('__ROOT__', dirname(dirname(__FILE__))); 
define("APPDIR", dirname(__FILE__).'/');
define("VIEW", APPDIR.'views/');
echo APPDIR;

and, I then create a shell script containing:

echo "Good morning, world."
APPDIR = '/var/www/site-root'
export APPDIR
`php -e test.php`

I am trying to see if when I run the shell script that then executes the php script that it acknowledges the constants that are being set in PHP. Right now, the echo statement in php prints out "APPDIR" instead of /var/www/site-root.

The issue is i have shell crons that interrogate the php code and there are a series of require and includes that have constants that construct their file paths. I need the shell to adhere to those constants. I have had to use php $variable = "path" to get both the web app and the crons to be able to locate all the needed files for execution.

As you can see, i have tried exporting the variable from shell and making it an environment variable available to both bash and php to no avail.

Thoughts, ideas? Thanks

1

2 Answers 2

3

Use the getenv() php directive

Here is an answer to a similar question: Export a variable from PHP to shell

If you're trying to pass some output to a shell variable, you can do it like this:
$ testvar=$(php -r 'print "hello"')
$ echo $testvar
hello
Showing how export affects things:
$ php -r '$a=getenv("testvar"); print $a;'
$ export testvar
$ php -r '$a=getenv("testvar"); print $a;'
`hello

Sign up to request clarification or add additional context in comments.

Comments

1

You could either pass your variables as command line arguments to php, or use getenv() in php to read the environment variables.

<?php
define("APPDIR",getenv("APPDIR"));
?>

1 Comment

putenv() might be more along the lines of what i am looking for. Thanks so much for the clue though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.