有谁知道如何获取 bash 文件来呈现 php 常量?例如,如果我创建一个包含的 php 文件,
define('__ROOT__', dirname(dirname(__FILE__)));
define("APPDIR", dirname(__FILE__).'/');
define("VIEW", APPDIR.'views/');
echo APPDIR;
然后,我创建了一个 shell 脚本,其中包含:
echo "Good morning, world."
APPDIR = '/var/www/site-root'
export APPDIR
`php -e test.php`
我试图查看当我运行 shell 脚本然后执行 php 脚本时,它是否承认在 PHP 中设置的常量。现在,php 中的 echo 语句打印出“APPDIR”而不是/var/www/site-root。
问题是我有 shell cron 来询问 php 代码,并且有一系列的 require 和 include 具有构造它们的文件路径的常量。我需要 shell 来遵守这些常数。我不得不使用 php $variable = "path"来获取 Web 应用程序和 crons,以便能够找到所有需要的文件以执行。
如您所见,我尝试从 shell 导出变量并使其成为 bash 和 php 都可用的环境变量,但无济于事。
想法,想法?
谢谢
最佳答案
使用 getenv() php 指令
这是一个类似问题的答案:
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
关于php - 如何获取 bash 文件以呈现 php 常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20009336/