99

How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?

I have a phpinfo.php file which is accessed from the web shows that German is installed. However, if I run the phpinfo.php from the command line using - php phpinfo.php and grep for German, I don't find it. So both PHP files are different. I need to run a script which the php on which German is installed.

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Siddharth
  • 5,009
  • 11
  • 49
  • 71

3 Answers3

183

You should check your server configuration files. Look for lines that start with LoadModule php... There probably are configuration files/directories named mods or something like that. Start from there.

You could also check output from php -r 'phpinfo();' | grep php and compare lines to phpinfo(); from web server.

To run php interactively:

(So you can paste/write code in the console.)

php -a

To make it parse a file and output to the console:

php -f file.php

Parse a file and output to another file:

php -f file.php > results.html

Do you need something else?

To run only a small part, one line or like, you can use:

php -r '$x = "Hello World"; echo "$x\n";'

If you are running Linux then do man php at the console.

If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:

SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"

Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 4
    No. The problem is that there are multiple phps on the system. I need to find out which php is used to parse web scripts. If I run php from the console then it can't find a class - but if I call that script from the web then that problem isn't there. So it means that the PHP which is used are different, isn't it? – Siddharth Apr 21 '12 at 20:20
  • 2
    @Siddharth You should check that from your server s/w configuration. Add your server details to question. – Sampo Sarrala - codidact.org Apr 21 '12 at 20:23
  • 2
    Yes. How can I check that? In short, how can I use the correct php from the command line? – Siddharth Apr 21 '12 at 20:24
  • 2
    In linux, you can use `which php` to tell you which executable you are running by default. To use another executable, you must specify the full path to the executable you wish to use: `/path/to/specific/php -f file.php > results.html` – Emilio P. Sep 20 '15 at 22:03
  • 1
    nice detail, +1 for explanation. Just here to comment, be watch with the environment vars, maybe you need to use the complete directory path even when the script is in the same directory that php.exe – Leandro Bardelli Feb 20 '19 at 19:18
3

On SUSE Linux, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. Each has a "php.ini" file. The files are for the same purpose (PHP configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.

If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the German module, while the the CLI php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.

You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.

Or, you could find the line that loads the German module in the Apache file and copy/paste just it to the CLI file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

I was looking for a resolution to this issue in Windows, and it seems to be that if you don't have the environment variables ok, you need to put the complete directory. For example, with a file in the same directory as PHP:

F:\myfolder\php\php.exe -f F:\myfolder\php\script.php
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • If you run this verbatim from the command prompt, the `>` at the start will probably redirect the output of `-f ...` (likely an error) into your php.exe file, and wipe the content. – mwfearnley Mar 25 '19 at 17:26