Running PHP Files Locally: Beyond the Server Environment
You have PHP and Apache installed on your machine, yet you cannot see the output of a simple script like the one provided in your query. The core issue isn't missing software; it is how your operating system handles file execution requests. When you double-click an .php file or run it via a shell without the correct context, your computer treats it as a text file rather than executable code. To get output, PHP requires a web server to interpret the <?php tags and send the resulting HTML back to a browser or print stream.
The Role of Apache in Local Execution
Since you already have Apache installed, the most direct path is to utilize its built-in capabilities. However, simply having the service running isn't always enough; you must ensure your files are served through an active document root.
If you are on Linux or macOS, check if Apache is listening on port 80 and serving files from a directory like /var/www/html. Place your test file in that directory:
cd /var/www/html
echo '<?php $a=5; $b=10; $c=$a+$b; echo $c; ?>' > test.php
Then, visit http://localhost/test.php in your browser. If the output 15 appears, Apache is functioning correctly as a local server. The critical distinction here is that PHP code only runs when requested via HTTP. If you try to execute php test.php from the command line directly, it will fail unless you invoke the PHP interpreter explicitly.
On Windows, this often involves configuring the httpd.conf file to allow scripts in specific folders or ensuring the "DocumentRoot" is set to a directory you can easily access. Without these configurations, Apache might reject requests for files outside its designated root for security reasons.
Using the CLI Interpreter
If you prefer not to rely on a web server interface or need to run PHP scripts without HTML output (like generating JSON or processing data), the Command Line Interface (CLI) version of PHP is the tool of choice. This approach decouples your script from the Apache web server entirely, which is useful for automation tasks or background jobs.
First, verify that the CLI binary exists and is accessible in your system path:
php -v
If this command returns a version number, you can run your script directly from the terminal:
php test.php
This method bypasses Apache completely. It sends echo output directly to stdout (the terminal) and ignores HTML headers. This is often cleaner for debugging because it avoids potential conflicts with Apache's configuration, such as mod_rewrite rules or .htaccess restrictions that might block local file execution in a web context.
Troubleshooting Common Pitfalls
A frequent stumbling block when moving from a production server to a local environment is the difference in error reporting. On a live server, you might see the correct output because errors are suppressed or logged elsewhere. Locally, if your script crashes, PHP might exit silently unless error reporting is enabled.
Add this to the top of your file to ensure you see fatal errors during development:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$a=5;
$b=10;
$c=$a+$b;
print $c;
?>
Another common issue is running the script from a directory containing sensitive files or expecting it to behave differently based on the file extension. Some web servers are configured to only execute PHP files in specific subdirectories (e.g., /public or /htdocs) for security. If you place your file outside these directories, Apache may serve it as plain text or return a 403 Forbidden error.
When to Use Which Method
Choosing between the web server method and the CLI depends on your workflow. Use the browser-based approach (Apache/Nginx) when developing web applications where you need to inspect HTTP headers, cookies, or session handling. Use the CLI method for command-line tools, cron jobs, or when you simply want to verify PHP syntax without setting up a virtual host.
If you are building a Laravel application specifically, running it locally almost always involves starting a development server rather than relying on system-wide Apache configurations. In that scenario, the laravel company resource provides detailed guides on configuring your local environment using tools like Vagrant or Homestead to mimic production setups seamlessly. However, for raw PHP scripts, sticking to the standard CLI or configured Apache document root is the most robust solution.