Martin Smith
Martin Smith

Reputation: 4077

How to hide shebang when generating HTML page from PHP script

I have a PHP script that is designed to be run from the command line (including as a CRON job) or a web browser.

To run it from the command line, I understand that I need to include the shebang #!/usr/bin/php as the first line of the file.

But this line then appears before the <html> element when the script renders as an HTML web page.

How can I hide the shebang when rendering the script as HTML?

Upvotes: 1

Views: 1342

Answers (2)

axiac
axiac

Reputation: 72226

To run it from the command line, I understand that I need to include the shebang #!/usr/bin/php as the first line of the file.

No, you don't need to include a shebang line.
A shebang line tells the shell what program to use to run the script and, in order to work, needs the file to be marked as executable. The advantage is that it can be run in a standalone manner:

$ ./script.php

You can always run a PHP script by passing it as the argument to the PHP interpreter:

$ php script.php

The script file does not need to be executable and you can add more arguments to the PHP interpreter, if you need (f.e. e custom php.ini file).

Upvotes: 4

Daniel W.
Daniel W.

Reputation: 32310

You shouldn't use the exact same script for use in Apache and use in console.

If you really need or want to, you can remove the shebang and run the script with the interpreter.

Currently you must have something like

* * * * * * userxy /folder/script.php

You can run it without shebang:

* * * * * * userxy php /folder/script.php

Upvotes: 8

Related Questions