php - 将变量从 PHP 导出到 shell

标签 php shell export environment-variables

我正在尝试设置一个应该可以从 PHP 外部访问的变量。理想情况下,这应该是局部变量,但也欢迎使用环境变量。

首先,我尝试了 putenv(),但这没有给出任何结果:

$ php -r "putenv('PHP_TEST=string');" ; echo $PHP_TEST

$

当我从同一脚本调用 getenv() 时 — 它会产生正确的“字符串”值。 安全模式已关闭,但手册上说“PHP_”前缀对于 safe=on 至关重要,所以我使用它以防万一:)

然后我尝试 system()shell_exec():

$ php -r "shell_exec('PHP_TEST=string');" ; echo $PHP_TEST

$ php -r "shell_exec('export PHP_TEST=string');" ; echo $PHP_TEST

$

有解决办法吗?可能是什么原因? 我使用的是 Ubuntu Linux 9.10“Karmic”,但 FreeBSD 服务器给出了相同的结果。

最佳答案

如果你试图将一些输出传递给 shell 变量,你可以这样做:

$ testvar=$(php -r 'print "hello"')
$ echo $testvar
hello

显示导出如何影响事物:

$ php -r '$a=getenv("testvar"); print $a;'
$ export testvar
$ php -r '$a=getenv("testvar"); print $a;'
hello

在这些示例中,交互式 shell 是父进程,显示的所有其他内容都是子进程(以及彼此的 sibling )。

关于php - 将变量从 PHP 导出到 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2002970/

相关文章:

linux - 如何在 GRUNT shell 中取消命令

linux - 根据扩展名识别文件

javascript - 使用 PHP 将 png/svg 导出为单个 png 文件

php - 没有 php.ini 与 brew

php - 找出博客正在使用的 WordPress 版本(从外部)

regex - BASH 中的正则表达式?

python - Tensorflow 2.0如何从Keras模型导出预测和训练模式

php - 如何使用php根据数组生成查询

php - 使用 Sql 查询在 php 中打印输出

MySQL 数据库导出(数据库中每个表的 100 行)