[Setup] PHP Development Environment
This page explains how to set up a PHP development environment. PHP is a widely used language for web application development. With the built-in server, you can verify your code in a browser right away.
Installing PHP
| OS | Installation Method |
|---|---|
| Windows | Download the ZIP file from php.net and extract it. Add the folder path to the PATH environment variable. If you want an all-in-one solution, installing XAMPP gives you PHP, Apache, and MySQL in one package. |
| macOS | Run brew install php with Homebrew. After installation, run php --version in a terminal to verify. |
php --version PHP 8.3.8 (cli)
Running Code with the Built-in Server
PHP includes a built-in web server. You can test PHP in a browser without installing a separate server like Apache.
First, save the following content as index.php using a text editor.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Test</title>
</head>
<body>
<h1>Hello, PHP!</h1>
<p>Current date/time: <?php echo date('Y-m-d H:i:s'); ?></p>
<?php
$fruits = ['Apple', 'Banana', 'Orange'];
echo '<ul>';
foreach ($fruits as $fruit) {
echo '<li>' . $fruit . '</li>';
}
echo '</ul>';
?>
</body>
</html>
Navigate to the folder where you saved the file in a terminal and start the built-in server with the following command.
php -S localhost:8000
Open http://localhost:8000 in a browser to see the output. Press Ctrl + C to stop the server.
Running PHP from the CLI
PHP files can also be run directly from the terminal without a browser.
Save the following content as hello.php using a text editor.
<?php
// hello.php
echo "Hello, PHP!" . PHP_EOL;
$name = "Alice";
echo "Hello, " . $name . "!" . PHP_EOL;
for ($i = 1; $i <= 5; $i++) {
echo "Loop iteration " . $i . PHP_EOL;
}
Run the following command in a terminal.
php hello.php Hello, PHP! Hello, Alice! Loop iteration 1 Loop iteration 2 Loop iteration 3 Loop iteration 4 Loop iteration 5
Checking php.ini
You can find the location of PHP's configuration file php.ini with the following command. Refer to it when you need to change settings such as error display.
php --ini Loaded Configuration File: /usr/local/etc/php/8.3/php.ini
During development, adding the following settings to php.ini makes it easier to find the cause of errors.
display_errors = On error_reporting = E_ALL
Command Not Found
If the terminal shows php: command not found, the PATH may not be configured correctly. Follow the steps below to check and set it up.
1. Locate the command
Check where the php command is installed.
which php
If it is not found, check the common installation paths.
ls /usr/local/bin/php ls /opt/homebrew/bin/php ls /Applications/XAMPP/bin/php
2. Check your shell
echo $SHELL
If the output is /bin/zsh, edit ~/.zshrc. If it is /bin/bash, edit ~/.bashrc.
3. Add the path to PATH
Once you know the installation path, add it to your shell configuration file.
For macOS (zsh):
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
For Linux (bash):
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc source ~/.bashrc
If you are using XAMPP, adjust the path to match your XAMPP installation (e.g., /Applications/XAMPP/bin).
On Windows, go to "System Properties" → "Environment Variables" → "Path" and add the folder where PHP is installed (e.g., C:\php or C:\xampp\php).
If you find any errors or copyright issues, please contact us.