Is #!/usr/bin/env required to run PHP from command line? - php

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?

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.

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).

the magic belongs to the executable flag (chmod +x FILE)
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

Related

PHP exec on local script

Hello i have a PHP script, and its added to cron, it is possible to execute from this script shell command (with exec() or something) without enabling it on php.ini? I don't want to enable exec on my site
It's called PHP CLI, check here
Usually when you install php, there's option to install php_cli too.
So long you can run php on shell prompt, then it can work.
Open bash (or other shell), try this:
php -v
If the version printed, then it's working.
Then you can
php -f phpfile
or put
#!/usr/bin/php
At the beginning of your php file as a line, and chmod +x file.php, and then
./file.php
#or
/path/to/file.php
to run it.
(Note /usr/bin/php is the usual place of php executable, it might change, eg in unix is ually /bin/php. Use whereis php to check its place.)

Change Default version of PHP that is called on the command line

is there a way to change the default version of PHP that is called on the command line?
I am trying to install Laravel using composer the problem is composer is calling php (which is version 4.4) when it needs to call php5.4, i have changed the binary in the header from #!/usr/bin/env php to #!/usr/bin/env php5.4 but this throws an error!
here is my php set-up
php: /usr/bin/php /usr/bin/php5.4 /usr/bin/php5.5 /usr/lib/php /usr/lib/php.ini-nourl /usr/lib/php.ini /usr/lib/php5.4 /usr/lib/php5.5 /usr/local/bin/php /usr/local/bin/php5.4 /usr/local/bin/php5.5 /usr/local/lib/php.ini-nourl /usr/include/php /usr/include/php5.4 /usr/include/php5.5 /usr/local/php /usr/share/php
There are a couple options here, but really this is not php specific, this is about basic execution path rules.
The #!/usr/bin/env php header means that when you run composer alone it runs it with php, so if you manually run it like php5.4 /path/to/composer you can bypass that header.
The other option is to define a symlink in /usr/local/bin/php or something that:
points to /usr/bin/php5.4
is in a directory that has precedence over /usr/bin inside your PATH environment variable.

Windows 8: .phar files, how do you want to open

I'm trying to run composer on windows with wamp. I installed composer using the cmd prompt, and now I'm trying to run "composer update" for an SDK. However, when I type in "composer.phar update," windows asks what app I want to use to run this program. I want the command prompt to deal with it! How do I just run it through cmd, without this "what app" window coming up?
You have to set php.exe as your default application for phar files.
.phar stands for PHP Archive
Usually .phars take some arguments, so they are intended to be run from command prompt. Linux/BSD/OS X shell or Windows command prompt.
Linux .phar use case scenarios assume .phars are copied to some /bin and renamed to be without .phar extension, so you can use a php archive as if you would use any other linux command. So I recommend following way of doing the same thing with Windows:
Put all your .phar files to one directory like C:\php\phars
Add C:\php\phars to system environment variables (right-click my Computer -> Properties -> Advanced System Settings -> Environment variables)
Start the elevated command prompt (find command prompt in start menu then right-click and select Run as Administrator)
Type the following commands, replacing the path C:\phpdev\php\php542\php.exe with full path to your PHP executable:
ftype PHARFile=C:\phpdev\php\php542\php.exe "%1" %*
assoc .phar=PHARFile
set PATHEXT=%PATHEXT%;.PHAR
Next time your should be able just to run Windows console (keyboard Win+R and type cmd.exe) and type any of your .phar's like apigen.phar followed by any command and it will work
C:\Users\acosonic>apigen.phar help
Usage:
...
Arguments:
command The command to execute
command_name The command name (default: "help")
Options:
--xml To output help as XML
--format To output help in other formats (default: "txt")
--raw To output raw command help
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--version (-V) Display this application version.
Help:
The help command displays help for a given command:
php C:\phpdev\phars\apigen.phar help list
You can also output the help in other formats by using the --format option:
php C:\phpdev\phars\apigen.phar help --format=xml list
To display the list of available commands, please use the list command.
C:\Users\acosonic>
So this way lets you run .phar archives in a directory where you need to work, for example generating documentation in C:\myproject\controller without specifying full path to .phar as if you would if it's run without adding it to Windows path.
To explain what commands in step 4 did:
Created mapping HKCR.phar → HKCR\PHARFile
Created HKCR\PHARFile\shell\open\command = 'php.exe "%1" %*' [REG_EXPAND_SZ]
Extended HKCU\Environment\PATHEXT = '%PATHEXT%;.PHAR' [REG_EXPAND_SZ]
*.phar gets treated like binary/script, and *.phar execution works as long as a *.phar file is located anywhere in %PATH%.
One can wrap php *.phar with *.bat, then the filename will be the name of the CLI command:
#ECHO OFF
php "C:/Program Files/PHAR/phpDocumentor.phar" %*
One can also use such a wrap to pass along default arguments; eg. wp.bat:
#ECHO OFF
php "C:/Program Files/PHAR/wp-cli.phar" --path="D:/SDK/wordpress" %*
Where pattern %* will capture and forward CLI arguments, as it is supposed to.
Alike this one can run phar alike any other CLI command, in a terminal window.
Keep it simple. Instead of changing .phar's default program, which is not always easy in Windows, try just typing "php" in front of your command.
php composer.phar update

PHP: How can I tweak PHPUnit to use a different PHP interpreter?

My system has two PHP interpreters. One came bundled with the OS and the other I installed via the XAMPP package. All of my PHP extensions are applied to the XAMPP installation but PHPUnit seems to only run the version of PHP that came with my machine.
Does anybody know of a way I can configure or rebuild PHPUnit so that it uses my XAMPP PHP interpreter exclusively?
For Mac/Linux, the first line of the phpunit script with starts with
#!/usr/bin/php
change that to
#!/Applications/XAMPP/xamppfiles/bin/php
or whatever other php interpret you want to use.
Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like
set PHPBIN="C:\php\php.exe"
%PHPBIN% "C:\php\phpunit" %*
Change it to read
set PHPBIN="C:\xampp\php\php.exe"
%PHPBIN% "C:\xampp\php\phpunit" %*
Or whatever the path to your PHP executable is
Since modifying phpunit file did not work for me because of phar signature errors, I was running phpunit with different php version by calling interpreter explicitly (on Linux):
php7.1 /usr/local/bin/phpunit
php5.6 /usr/local/bin/phpunit
Following the example with XAMPP, full path to php interpreter could be provided:
/Applications/XAMPP/xamppfiles/bin/php /usr/local/bin/phpunit
In agreement with Thomas' statement, additionally there's a line further below
if (strpos('/Applications/MAMP/bin/php5.3/bin/php', '#php_bin') === 0) {
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}
That I've been told you're also supposed to change to reflect the PHP you're interested in using
(I've set mine to MAMP obviously)
I've switched back and forth from 5.2 and 5.3 a lot recently :)
This applies to phpunit installed using Homebrew on Mac OS 10.9. I’ve editing the file located at /usr/local/Cellar/phpunit/4.2.6/bin as seen below. CAVEAT: I don’t know how Homebrew will handle this on a PhpUnit update, but for now it’s working to be able to select the php version that PhpUnit is using for it's testing.
#!/usr/bin/env bash
php=/Applications/MAMP/bin/php/php5.3.14/bin/php
#php=/Applications/MAMP/bin/php/php5.4.4/bin/php
/usr/bin/env $php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/4.2.6/libexec/phpunit-4.2.6.phar $*
On Windows, this may be achieved using a similar approach to the ones mentioned in other replies.
In your /path/to/composer/phpunit directory, open the phpunit file in an editor. The first line should look like this:
#!/usr/bin/env php
Simply download your desired version of PHP for Windows, place the contents of the ZIP file somewhere to your liking, and reference the fully quantified path to the php.exe file, instead of just php. Like so:
#!/usr/bin/env /c/misc/php-5.5.9-nts-Win32-VC11-x86/php.exe
In my case, I put it in /c/misc/php-5.5.9-nts-Win32-VC11-x86/, which corresponds to C:\misc\php-5.5.9-nts-Win32-VC11-x86\ using Windows path syntax.
Remember to verify that the correct php.ini file is being used (php --ini or in a script file php_ini_loaded_file()).

linux - running php script from command line when php is installed as apache module

Normally when I want to run a php script from the command line I just create a php page, add a shebang pointing to the php binary, then ./file.php to run it. Since I have php installed as an apache module, I'm not even sure what my shebang should look like. Any ideas?
The CLI version of PHP had been part of the default installation since 4.3 and has to be explicitly turned off when PHP is being built. If you have access to the command line try
$ php -v
If you don't get a command not found error then you should be ready to go.
To actually run a php file from the command line do this:
$ php -f file.php
If it's just an Apache module, I don't think you can do it… At least, not without using a script like this:
$ cat run_php_with_apache
#!/bin/sh
cp "$1" /var/www/
curl "http://localhost/`basename "$1"`"
rm "/var/www/`basename "$1"`"

Categories