← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 3 of 13

PHP Environment Setup

To run PHP on your own computer you need a small helper program that understands PHP files, like needing a projector before you can show a movie. Once it is set up, your computer acts like a mini website server.

Web Server Requirements

PHP code needs a web server that knows how to hand .php requests to the PHP interpreter — Apache and Nginx are the two most common.

Rather than installing and configuring each piece separately, most beginners install an all-in-one bundle like XAMPP or MAMP, which packages a web server, PHP, and MySQL together.

Example: Web Server Requirements

php
<?php
// Declare `$server`, set to `$_SERVER['SERVER_SOFTWARE'] ?? 'PHP CLI'`
$server = $_SERVER['SERVER_SOFTWARE'] ?? 'PHP CLI';
// Print `"This request was handled by: " . $server` to the output
echo "This request was handled by: " . $server;
?>

Installing PHP

You can install PHP directly on your machine via your OS package manager (or an installer bundle), or skip local setup entirely and write PHP in a browser-based playground while you're still learning the syntax — useful when you just want to test a snippet without standing up a full server.

Example: Installing PHP

php
<?php
// Run this file with: php filename.php  (or via a browser-based playground)
echo "PHP is installed and running.";
?>

Writing Your Code

Save your code in a plain text file with a .php extension so the web server routes it to the PHP interpreter instead of serving it as a static file.

Inside that file, PHP code lives between <?php and ?> tags; everything outside those tags is passed through as-is, which is what lets you mix PHP and HTML freely.

Example: Writing Your Code

php
<?php
// Print "This runs inside the tags." to the output
echo "This runs inside the tags.";
?>
<p>This HTML passes through untouched.</p>

Running PHP Locally

You can run a .php file two ways: through a configured web server (visiting it in a browser), or directly from a terminal with php filename.php, which executes the script and prints its output to the terminal — handy for quick tests without touching a browser at all.

Example: Running PHP Locally

php
<?php
// Run with: php filename.php
echo "Printed directly to the terminal, no browser needed.";
?>

Verifying the Version

Run php -v in a terminal to see which PHP version is installed. Aim for PHP 7.4 or newer: modern syntax like arrow functions, typed properties, and the null coalescing assignment operator (??=) simply won't parse on older interpreters.

Example: Verifying the Version

php
<?php
// Print `"PHP version: " . PHP_VERSION` to the output
echo "PHP version: " . PHP_VERSION;
// Print `PHP_VERSION_ID >= 70400 ? "\nModern syntax supported." : "\nUpgrade recommended."` to the output
echo PHP_VERSION_ID >= 70400 ? "\nModern syntax supported." : "\nUpgrade recommended.";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Double-clicking a .php file or opening it in the browser from disk, which never invokes PHP; the file has to be served through a web server or run with php file.php.
  2. Saving files outside the web server's document root (such as htdocs or /var/www/html), so the browser URL returns 404 even though the code is correct.
  3. Forgetting to restart Apache or Nginx after editing php.ini or enabling an extension, so the old settings stay in effect.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.