30

Often times when I see PHP that is meant to be ran from the command line, it will have this line #!/usr/bin/env php at the top of the file like this...

#!/usr/bin/env php
<?php
    // code
?>

I was wanting to know if this is meant just for when the file is ran on a Linux/Unix system or is needed for running on Windows as well?

JasonDavis
  • 48,204
  • 100
  • 318
  • 537

3 Answers3

36

The shebang line is required for auto-detection of the type of script. It enables this sort of usage:

[pfisher ~]$ chmod +x run-me.php
[pfisher ~]$ ./run-me.php

That line is not needed if you pass the filename as an argument to the php interpreter, like so:

[pfisher ~]$ php run-me.php

Edit: replace "hashbang" with shebang.

lsl
  • 4,371
  • 3
  • 39
  • 54
Patrick Fisher
  • 7,926
  • 5
  • 35
  • 28
  • 3
    Of course, this only works on *nix. On windows, you have to use `> php run-me.php` from the command line. – ThatOtherPerson Jan 05 '12 at 05:09
  • 5
    Also worth mentioning is, that thanks to this line you can rename script file so it does not contain an extension. This way you will be able to execute it without even knowing what language it is: `./run-me`. – Tadeck Jan 05 '12 at 05:24
  • 1
    @ThatOtherPerson, Windows has it's own version: http://stackoverflow.com/a/6818266/632951 – Pacerier Aug 11 '15 at 12:12
  • shebang | hashbang ? hashbang i've never heard befor. but i'm not a uk | us native – f b May 28 '21 at 23:03
  • @f b: Agreed. Hashbang is a regression. –  May 28 '21 at 23:32
2

No it's not, you can directly use

#!/path/to/php

Running php (or anything else) through the env utility is a weak security measure. Dpending on the platform, will "fix" PATH, LIB, and other environment variables according to various config files and potentially remove some of the dangerous values in there (e.g. env on HPUX).

It is also to limit the scope of shell-expansions on certain environments. (See man 1 env on Linux).

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • 6
    `#!/usr/bin/env php` is used also because `#!/path/to/php` is different on different systems, doesn't it? – Tadeck Jan 05 '12 at 05:25
  • No, `/usr/bin/env` doesn't do anything of the sort -- all it does in a shebang is search for php (or whatever) on your `$PATH`, to cope with systems where the interpreter is somewhere strange like `/usr/local/bin`. –  Jan 05 '12 at 07:07
  • actually it DOES do exactly what i said in my answer. go read the man pages on those systems. – Ahmed Masud Jan 05 '12 at 07:41
  • I have an alias to php7.4 but usr/bin/env php -v gives 5.4.16. How do I change *that* php version to 7.4? – it's a hire car baby Oct 04 '20 at 12:41
  • 2
    @samerivertwice which os? on debian systems (debian,unbuntu, kubuntu...): `update-alternatives --config php` but this only belongs to cli! – f b May 28 '21 at 23:05
0
  1. the magic belongs to the executable flag (chmod +x FILE)
  2. the shebang: and if it exists with that version you may expect
    • /usr/bin/env cliVersion -> /usr/bin/env php
    • php -v # tells you which version you have by default as: 'cli' in a shell

alternativly use e.g:

php8.0 /to/php/script.php

to run it without a shebang at the first line of the script. (it will work even if it stays there but check the real php version on execution if important for you)

"standard"? over the last 10 years is /usr/bin/env depending on what version you set to be the default on your system:

debian systems (debian,unbuntu,kubuntu...): #root: update-alternatives --config php will guide you

f b
  • 115
  • 10