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

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
🌐
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 ...
🌐
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....
Find elsewhere
🌐
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?

🌐
Medium
alinawrites.medium.com › how-to-run-php-file-on-localhost-e740890fcdff
How To Run PHP File On Localhost. Most articles and tutorials are written… | by Alina Writes | Medium
November 2, 2022 - And third, you need to specify the path to the php interpreter. To run a php file from the terminal or command line, you can use the following syntax:
🌐
LWS
help.lws-hosting.com › en › how-to-run-php-script-with-web-terminal
How do I run PHP scripts using the Web Terminal? | LWS
You will learn how to schedule tasks with crontab and use the php command to run and check your PHP scripts efficiently. Before reading this documentation, we invite you to access your hosting's Web Terminal. The php command on the command line is used to execute PHP scripts, check the syntax of PHP files and interact with the PHP interpreter in interactive mode.
🌐
Plesk
docs.plesk.com › en-US › obsidian › administrator-guide › web-hosting › php-management › running-php-scripts-from-the-command-line.76345
Running PHP Scripts from the Command Line | Plesk Obsidian documentation
You can run PHP scripts in the Command Line for a particular PHP version used in Plesk. In Plesk for Linux, the PHP binary (the php file) is located in the following path: ... Here <PHP version> should be replaced with a particular PHP version number. For example, if your domain uses PHP version 7.0, use the following command to run the my_script.php script:
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>.
🌐
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.
🌐
Envato Tuts+
code.tutsplus.com › home › php › php scripts
How to Run a PHP File | Envato Tuts+
April 30, 2020 - Today, we’re going to discuss how you can run PHP files. If you're new to PHP programming, this article will help you learn how to run PHP scripts. PHP is a server-side scripting language which is...
🌐
cPanel
support.cpanel.net › hc › en-us › articles › 360050224413-How-to-run-PHP-commands-via-Terminal
How to run PHP commands via Terminal
July 27, 2021 - 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.
🌐
Quora
quora.com › How-do-you-run-a-PHP-script-from-within-Visual-Studio-Code-VS-code-without-debugging-it
How to run a PHP script from within Visual Studio Code (VS code) without debugging it - Quora
Answer: There is a much easier ... Runner Extension 2. Open the PHP code file in Text Editoruse shortcut [code ]Ctrl+Alt+N[/code]or press [code ]F1[/code] and then select/type [code ]Run Code[/code],or right click the Text ...