dongmi5015 2014-07-04 09:31
浏览 123
已采纳

如何忽略php包含的第一行?

I have a php script, which can be run independently from the command line, so it's first line is #!/usr/bin/env php. However, I also have another script, which can automatically run the first one. It does this by calling include 'example.php', which works, except that it outputs the first line as text.

How do I make include skip the first line? So far the only options I could think of were to wrap the whole thing in an output buffer and then trim the first line, but that has a whole host of problems with it. Alternatives such as using exec() to simulate running the second script from the command line, or reading in the whole file and evaluating it with eval(), both seem like pretty horrible solutions.

Is there any way to tell php, "include this file but don't output the first line"?

I am aware I can just remove the shebang and run the script with php script.php whenever I need to use it, which is ok in this specific case, but I would still like to know if there is an alternative, general way to make include ignore the first (or first X) lines.

  • 写回答

4条回答 默认 最新

  • doudi1978 2014-07-04 09:38
    关注

    IMO a script with a shebang is very explicitly a "front end" to the command line and should not be included in other scripts in the first place, because it contains code specific to dealing with CLI interaction (parsing passed arguments and such). It would probably make more sense to separate the reusable code in that script out into a library file, then include that file from both the shebang script and the other script.

    I.e., instead of:

    foo.php:

    #!/usr/bin/php
    // some code
    

    bar.php:

    include 'foo.php';
    

    you do:

    lib.php:

    // some code
    

    foo.php:

    #!/usr/bin/php
    include 'lib.php';
    

    bar.php:

    include 'lib.php';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口