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" SCRIPT_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.

Answer from Sampo Sarrala on Stack Overflow
Top answer
1 of 3
192

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" SCRIPT_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.

2 of 3
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.

🌐
PHP
php.net › manual › en › features.commandline.usage.php
PHP: Usage - Manual
The program first checks that there is the required one argument (in addition to the script name, which is also counted). If not, or if the argument was --help, -help, -h or -?, the help message is printed out, using $argv[0] to dynamically print the script name as typed on the command line. Otherwise, the argument is echoed out exactly as received. To run the above script on Unix, it must be made executable, and called simply as script.php echothis or script.php -h. On Windows, a batch file similar to the following can be created for this task:
Discussions

How to execute php block from terminal without saving to a file - Stack Overflow
Say I have a block of code I would like to test like this: How I quickly run this code from terminal without saving it to a file? I tried things like... ... More on stackoverflow.com
🌐 stackoverflow.com
How to run my php file from mac terminal - Stack Overflow
Sorry if this is a very novice question - I am a novice - but, I have no clue how to run my php file from the terminal. I type in the mac terminal something like this: php test.php (where test.php... More on stackoverflow.com
🌐 stackoverflow.com
Opening /running PHP files in Mac
php -S localhost:8000 will start a test server using the index.php in your current working directory. https://www.php.net/manual/en/features.commandline.webserver.php More on reddit.com
🌐 r/webdev
6
0
August 9, 2022
How to install php and run it?
I take it you're on a Windows system? Might try one of these all in one installers: https://www.wampserver.com/en/ (Apache, MySQL and PHP) https://www.apachefriends.org/index.html (Apache, MariaDB, PHP, & Perl) or just install things separately https://www.php.net/manual/en/install.windows.php More on reddit.com
🌐 r/PHPhelp
8
0
September 10, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-execute-php-code-using-command-line
How to Execute PHP Code using Command Line? - GeeksforGeeks
... To run PHP code interactively, use the -a option. ... If you need to run a PHP script in the background (e.g., for long-running processes), append & to the command: ... This will run the script in the background and free up the terminal.
Published   July 12, 2025
🌐
IONOS
ionos.com › help › hosting › using-php-for-web-projects › executing-php-files-via-the-command-line-php-cli
Executing PHP files via the command line (PHP CLI) - IONOS Help
Execute the command cd in the terminal ... execute the command. ... To execute a PHP file, enter the path to the PHP CLI program followed by the name of the PHP file in the terminal....
🌐
Educative
educative.io › answers › how-to-execute-php-scripts-on-the-command-line
How to execute PHP scripts on the command line
To check if it is installed and ... on your terminal or cmd: ... If you don’t have PHP installed, you can install it as shown below. Download it from the official site. Extract the file and store it in a folder, preferably named php, in your C just like so: c:/php · Next, place this folder in your environment variable path so that it can be recognized by the command line. ... You can change the php5 to your version of choice. You can run the command ...
Find elsewhere
Top answer
1 of 2
83

To run PHP commands immediately on Terminal you can pass -a option to your installed PHP:

php -a

Details:

php -a opens an interactive shell which let you type directly PHP commands and view the result on your terminal, As an example, after typing php -a in terminal you can type echo 'Hello World'; and after Press Enter Hello World! will be printed on the screen.

Windows Solution

On windows there is no interactive mode same as Linux but still you can use interactive like mode!, So, open PHP on place you installed it for example if you use XAMPP then your PHP should be is on C:\xampp\php (or add the binary directory to environment variables) and then type php -a, At the end of each line you can view the results by pressing Ctrl+Z and then Enter.

php -a
echo 'hello world!';
^Z
2 of 2
52

Escape the inside double quotes (") that you are using to delimit your string.

php -r "Print \"Hello, World!\";"

Alternatively, use single quotes (') for the PHP string or for the quoting of the PHP code.

If you run php --help you can see a list of commands that the php program accepts.

  -a               Run as interactive shell
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --rz <name>      Show information about Zend extension <name>.
  --ri <name>      Show configuration for extension <name>.
🌐
YouTube
youtube.com › indus technologies
How to Run php file in cmd | Running PHP from Command Line | How to Run PHP script in command prompt - YouTube
#runphpincmd #phpisnotrecognized #internalorexternalcommand #readinputincommandpromptHow to Run php file in cmd | Running PHP from Command Line | How to Run ...
Published   September 27, 2022
Views   10K
🌐
TecMint
tecmint.com › home › php › how to use and execute php codes in linux command line – part 1
How to Use and Execute PHP Codes in Linux Command Line - Part 1
July 13, 2015 - Next thing, we do is to test a php (if installed correctly or not) commonly as by creating a file infophp.php at location ‘/var/www/html‘ (Apache2 working directory in most of the distros), with the content <?php phpinfo(); ?>, simply by running the below command. # echo '<?php phpinfo(); ?>' > /var/www/html/infophp.php · and then point your browser to http://127.0.0.1/infophp.php which opens this file in web browser. ... Same results can be obtained from the Linux terminal without the need of any browser.
🌐
Quora
quora.com › How-can-we-execute-PHP-script-using-command-line
How can we execute PHP script using command line? - Quora
Save your file with .php extension. Make sure you've php installed and environment variables are set. Assuming you're ready to go simply rum following command. ... It will execute your script. If you want to run script on browser then you need web server installed on your machine.
🌐
Ecenica Hosting
ecenica.com › support › answer › how-to-run-a-php-script-in-terminal-in-cpanel
How To Run A PHP Script In Terminal In CPanel - Ecenica
October 4, 2011 - For example, if your script is in the public_html directory, you would type: ... Now that you’re in the correct directory, you can run your PHP script. Use the php command followed by the name of your script.
🌐
bodHOST
bodhost.com › tutorial › how to use the terminal to run php commands
How to Use the Terminal to Run PHP Commands | bodHOST
February 1, 2023 - Replace $user with the username of the script’s owner and $path with the actual path to the file’s location. ... Next, make certain that you are running as the user. ... Run the script, replacing the path to the PHP binary with the one found in the previous section and “script.php” with the script’s name. /opt/$vendor/xx-phpXX/root/usr/bin/php ./script.php · We hope that you now have a good understanding of How To Use The Terminal To Run PHP Commands.
🌐
Reddit
reddit.com › r/webdev › opening /running php files in mac
r/webdev on Reddit: Opening /running PHP files in Mac
August 9, 2022 -

Hi all,

I'm learning PHP and I'm having trouble understanding how to open PHP files on my Mac through my browsers. I have downloaded PHP via home-brew, and I can run php files by using the following command on my terminal: php /File/Path/hello.php.

However, I don't know how to open the file through safari or chrome. I have apache installed and running on my Mac. I'm confused where should I save my php files on my computer and how to open them on my browsers?

🌐
W3Docs
w3docs.com › php
How to run my php file from mac terminal?
April 4, 2018 - To run a PHP file from the Mac terminal, you will need to have a web server installed, such as Apache, and PHP should be configured correctly with the server.
🌐
cPanel
support.cpanel.net › hc › en-us › articles › 360050224413-How-to-run-PHP-commands-via-Terminal
How to run PHP commands as a cPanel user via Terminal – cPanel
July 13, 2015 - Change to the directory that the script is in by running the following command: ... Note: "/home/$username/path/to/script/folder" must be replaced with the full path to the script's folder. Execute the script using the full path to the php executable: ... Note: "XX" and "script.php" must be replaced with the PHP version (without the decimal point) and the script's file name, respectively.