dtmsaqtly798322992
2015-11-04 10:48
浏览 108
已采纳

使用参数从另一个调用PHP脚本并获取输出

I have a php script, let's call it one.php. This calls a second php script two.php with some arguments. Right now my code in one.php looks like this:

shell_exec("php ./two.php" . ' ' . escapeshellarg($var1) . ' '.      
escapeshellarg($var2) . ' '. escapeshellarg($var3));

This works fine (although I don't like it), but now I want to get some values that are computed in two.php.

I colud use include() or require() to get access to all the variables, but I want to use two.php in more than just one script - without having to use the same variable names in every script that calls two.php (which I had to because of the scope of the variables in include()).

My php skills are quite poor, so: Is there a way to send variables of a called script to the calling script or something like this? Or maybe another way of calling two.php.

Update: Using a function did it. Thanks!

图片转代码服务由CSDN问答提供 功能建议

我有一个php脚本,我们称之为 one.php 。 这会调用带有一些参数的第二个PHP脚本 two.php 。 现在我在 one.php 中的代码如下所示:

  shell_exec(“php ./two.php”。''。escapeshellarg($  var1)。''。
escapeshellarg($ var2)。''。escapeshellarg($ var3)); 
   
 
 

这很好用(虽然我不喜欢 它),但现在我想得到一些在 two.php 中计算的值。

我使用 include() require()来访问所有变量,但我想使用 two.php 不仅仅是一个脚本 - 无需在每个调用 two.php 的脚本中使用相同的变量名称(由于变量的范围,我不得不使用 在 include())。

我的PHP技能非常差,所以:有没有办法将调用脚本的变量发送到调用脚本或其他东西 像这样? 或者也许是另一种调用 two.php 的方式。

更新:使用函数做到了。 谢谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutuo6689 2015-11-04 10:53
    已采纳

    "Calling another script" via shell_exec is a pretty bad idea. Instead, two.php should contain a bunch of functions or classes which you can include and call anytime:

    include 'two.php';
    
    $data = func_from_two($var1, $var2, $var3);
    

    Learn to program using functions and classes, that's the only sane way to go.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题