PHP Environment Setup
In this page:
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
$server = $_SERVER['SERVER_SOFTWARE'] ?? 'PHP CLI';
echo "This request was handled by: " . $server;
?>
Login to try C/C++/Java/PHP code in the editor
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
// Run this file with: php filename.php (or via a browser-based playground)
echo "PHP is installed and running.";
?>
Login to try C/C++/Java/PHP code in the editor
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
echo "This runs inside the tags.";
?>
<p>This HTML passes through untouched.</p>
Login to try C/C++/Java/PHP code in the editor
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
// Run with: php filename.php
echo "Printed directly to the terminal, no browser needed.";
?>
Login to try C/C++/Java/PHP code in the editor
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
echo "PHP version: " . PHP_VERSION;
echo PHP_VERSION_ID >= 70400 ? "\nModern syntax supported." : "\nUpgrade recommended.";
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: