To run a PHP file, open a terminal in its folder and type php index.php. PHP runs the file and prints its output in the terminal. To see a PHP page in a browser, start the built-in server with php -S localhost:8000 and open http://localhost:8000.
php index.php
A first index.php can be one line. Press Run: this is exactly what php index.php prints.
PHP code goes after the <?php tag. Anything before that tag is printed as plain text, which is how PHP mixes code with HTML.
Run a PHP file from the command line
The PHP CLI (command line interface) runs a script once and exits. You need PHP installed; if php -v prints an error, see install PHP first.
cd ~/projects/hello
php index.php # run the file
php -l index.php # only check the syntax ("lint")
php index.php > out.txt # save the output to a file
php -l is useful before uploading a file: it reports a parse error with its line number, or No syntax errors detected in index.php.
Pass arguments with $argv
Words after the file name arrive in $argv. $argv[0] is always the script's own name and $argc is the count, including that name. Here the block runs without extra arguments, so only the name is there:
On your own machine, php index.php Ada 3 prints the greeting three times. The ?? fallbacks keep the script working when an argument is missing. For named options like --name=Ada, use getopt:
<?php
// php index.php --name=Ada --verbose
$opts = getopt('', ['name:', 'verbose']);
echo "Hello, " . ($opts['name'] ?? 'world') . "\n";
if (isset($opts['verbose'])) {
echo "Verbose mode\n";
}
Run PHP in the browser with php -S
PHP has a development web server built in. Start it in the folder with your files:
php -S localhost:8000
[Sat Oct 3 07:21:34 2026] PHP 8.3.19 Development Server (http://localhost:8000) started
Now http://localhost:8000/ runs index.php, and http://localhost:8000/about.php runs about.php. Each page load runs the file again, with the query string in $_GET. The block below behaves the same way: run it, then click a link, and the script runs again with $_GET['page'] set.
Useful options:
php -S localhost:8000 -t public # serve the public/ folder as the web root
php -S 0.0.0.0:8000 # reachable from other devices on your network
php -S localhost:8000 router.php # send every request through router.php
The built-in server handles one request at a time and is meant for development only. In production PHP runs behind Nginx or Apache, usually through PHP-FPM.
Check how the script was started
PHP_SAPI tells you which "server API" runs the script: cli in a terminal, cli-server under php -S, and fpm-fcgi or apache2handler behind a real web server. Scripts that should only run from a terminal check it first:
Run a single line with php -r
For a quick test you do not need a file. php -r runs the code you pass, without the <?php tag:
php -r 'echo date("Y-m-d"), PHP_EOL;'
php -r 'var_dump(0.1 + 0.2 == 0.3);'
On Windows cmd, wrap the code in double quotes and use single quotes inside it. For several lines, php -a opens an interactive shell where each statement runs when you press Enter.
Make a script executable on macOS and Linux
Add a shebang line before <?php and mark the file executable. The shebang line is not printed:
#!/usr/bin/env php
<?php
echo "Backup started\n";
chmod +x backup.php
./backup.php
This is how PHP scripts run from cron: 0 3 * * * /usr/bin/php /home/me/backup.php.
Exit codes for scripts
A command-line script reports success or failure through its exit code, which shell scripts and cron can check. exit(0) means success, any other number means failure:
<?php
$file = $argv[1] ?? '';
if (!is_file($file)) {
fwrite(STDERR, "File not found: $file\n");
exit(1);
}
echo "Processing $file\n";
exit(0);
Common mistake: running the wrong file or folder
Could not open input file: index.php means PHP cannot find the file from where the terminal is. Check the current folder with pwd (or cd on Windows) and either cd into the project or give the full path: php ~/projects/hello/index.php. With php -S, a 404 for a page you know exists usually means the server was started in a different folder; stop it with Ctrl+C and start it again in the right place, or pass the folder with -t. When the file runs but prints a blank page, turn on error reporting to see what went wrong.
Frequently Asked Questions
How do I run a PHP file in the browser?
Start the built-in server in the folder that contains the file with php -S localhost:8000, then open http://localhost:8000/yourfile.php. Opening the file directly from disk does not work, because nothing runs the PHP code.
How do I pass arguments to a PHP script?
Write them after the file name: php report.php 2026 sales. Inside the script, $argv is the array ['report.php', '2026', 'sales'] and $argc is 3. The first element is always the script name.
How do I run one line of PHP without a file?
Use php -r and leave out the <?php tag: php -r 'echo strtoupper("hi"), PHP_EOL;'. For several lines, php -a opens an interactive shell.
How do I know if PHP is running from the command line or a web server?
Check PHP_SAPI (or php_sapi_name()). It is "cli" in a terminal, "cli-server" under php -S, and something like "fpm-fcgi" or "apache2handler" behind a web server.