1

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

Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
  • Related: https://stackoverflow.com/questions/50142633/how-to-read-variables-from-a-php-config-file-using-a-bash-shell-script – Jesse Nickles Jul 23 '22 at 18:19

2 Answers2

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

Community
  • 1
  • 1
Taz
  • 31
  • 3
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"));
?>
sivann
  • 2,083
  • 4
  • 29
  • 44