Benubird Benubird - 1 year ago 75
PHP Question

How to ignore first line with php include?

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
to simulate running the second script from the command line, or reading in the whole file and evaluating it with
, 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.

Answer Source

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:


// some code


include 'foo.php';

you do:


// some code


include 'lib.php';


include 'lib.php';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download