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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题