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

PHP Environment Setup

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
$server = $_SERVER['SERVER_SOFTWARE'] ?? 'PHP CLI';
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
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
echo "PHP version: " . PHP_VERSION;
echo PHP_VERSION_ID >= 70400 ? "\nModern syntax supported." : "\nUpgrade recommended.";
?>

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.