Chapter 1: Introduction to PHP
PHP is a popular general-purpose scripting language that runs primarily on the server side (you can also use it from the command line). It powers many websites and APIs. PHP integrates well with Apache and Nginx through SAPIs such as mod_php and FPM. You can install it on Windows, macOS, and Linux, and you can start quickly with a bundled stack that includes a web server and a database.
In this chapter you will install a local environment, create your first script, run PHP from the command line, learn about tags and files, and tune a few core configuration settings.
What PHP Is and Where It Runs
PHP executes on the server to build responses that are sent to browsers or other clients. You can embed PHP into HTML or keep it separate and send JSON. You can execute PHP scripts via a web server (Apache or Nginx using FPM), or directly using the php command in a terminal. The same code typically runs on Windows, macOS, and Linux with only minor environment differences.
Understanding the request lifecycle in simple steps
A browser sends a request to your server; the web server forwards that request to PHP; your PHP script reads input, performs work, and returns output; the web server sends the response to the client. For APIs you usually return JSON. For classic sites you return HTML that may include embedded PHP blocks.
Common PHP stack choices for local work
For Windows you can use WampServer (bundled stack). For macOS you can use MAMP. For any platform you can use XAMPP. For minimal setups you can install PHP alone and use the built-in server during development.
Installing PHP on Your System
This section focuses on a fast Windows setup with WampServer. You will also see brief notes about other choices so you can pick the best path for your platform.
Installing WampServer on Windows for a complete stack
WampServer bundles Apache, MySQL, and PHP so you can run examples immediately. Download the correct installer for your CPU from sourceforge.net/projects/wampserver, run it, then start WampServer. When the tray icon turns green you can open http://localhost/. Create a folder inside the www directory for your projects and place .php files there.
C:\wamp64\www. Create a folder such as C:\wamp64\www\playground and put your test files inside it.
Verifying that PHP works after installation
Open a terminal and run php -v to see the version. In your project folder create a file named info.php with <?php phpinfo(); ?>, then visit http://localhost/playground/info.php. You should see details about your PHP build and configuration paths.
Alternative local options in short form
- For macOS you can install MAMP from www.mamp.info/en/mac and use a similar process (create a project inside the
htdocsfolder). If you develop on both macOS and Windows you can also install MAMP for Windows (www.mamp.info/en/windows/). - For any platform you can use XAMPP (www.apachefriends.org) with Apache and MariaDB.
- For Windows you can also try Laragon (laragon.org).
- On Linux you can install native packages; for example
sudo apt install phporsudo dnf install phpthen enable FPM and your web server. If you only need a quick test you can use the built-in server that ships with PHP (covered later).
localhost. If Apache fails to start, check whether another service uses port 80 or 443 and either stop that service or change the configured ports.
Your First PHP Script
Here is a small script that prints a greeting and shows basic information about your environment. It works by outputing the file contents directly to the browser, but interpreting any PHP commands that are encountered within <?php … ?> sections. Save this as index.php inside your project folder, then open it in the browser through your local server.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello PHP</title>
</head>
<body>
<h1>Hello from PHP</h1>
<p>The time is: <?php echo date('Y-m-d H:i:s'); ?></p>
<p>PHP version: <?php echo PHP_VERSION; ?></p>
</body>
</html>
http://localhost/… rather than opening it directly with a file:// URL.
Placing files in the served project folder
Your PHP files must live inside the server’s document root or a configured virtual host. For WampServer that root is the www folder. Use subfolders to keep projects separate and tidy.
Running PHP from the CLI and Built-in Server
PHP includes a command line interface that is useful for quick experiments and automation. You can also use a lightweight development server that ships with PHP, which is convenient for small demos or local APIs.
Running simple commands with the php CLI
Use php -v to print the version. Use php -i to print configuration. Use php -r "echo 2 + 2;" for quick one-liners. Run a script with php path/to/script.php from the terminal.
Starting the built-in development server
From a project folder that contains an index.php file you can run this command to serve files at http://localhost:8000:
php -S localhost:8000
To serve a specific folder use the -t option. For example:
php -S localhost:8000 -t public
You can also supply a router script for clean URLs that maps requests to a single entry point:
php -S localhost:8000 router.php
PHP Tags, Files, and Project Layout
PHP code lives between tags. The standard form is <?php … ?>. When you only need to echo a value, you can use the short echo form <?= … ?>. Use the .php file extension for executable files. Keep a clear folder structure to make navigation easier.
Using standard tags and avoiding legacy short tags
Use <?php and ?> for all code blocks. Avoid legacy short tags like <? … ?> since availability depends on configuration and may not be enabled everywhere. The short echo form <?= is always available and safe to use.
A simple project layout that scales
Start with a small structure, then grow it. Keep public assets in a dedicated folder. Add a src folder for application code and a vendor folder when you introduce Composer. Use an index.php front controller inside public.
my-app/
public/
index.php
styles.css
src/
App.php
tests/
…
vendor/
…
composer.json
README.md
public folder reduces the risk of exposing private files such as configuration or templates.
php.ini Overview and Configuration Basics
The php.ini file controls how PHP behaves. Common installations include separate configurations for CLI and web SAPI. You can view active settings using php --ini and with phpinfo(). You can override some settings at runtime using ini_set().
Finding and selecting the active php.ini
Run php --ini in a terminal to see the loaded configuration files. In a browser open a page that calls phpinfo() and look for “Loaded Configuration File”. Some stacks provide “development” and “production” templates; copy the one that fits your needs and adjust values carefully.
Tuning common directives with safe defaults
Set a timezone to avoid warnings; for example date.timezone = Europe/London. Use sensible memory limits such as memory_limit = 256M for typical local work. Enable useful error reporting during development with display_errors = On and error_reporting = E_ALL. For uploads adjust upload_max_filesize and post_max_size to values that match your needs.
; Development-friendly snippet
display_errors = On
error_reporting = E_ALL
date.timezone = Europe/London
memory_limit = 256M
upload_max_filesize = 10M
post_max_size = 12M
Overriding settings safely when needed
You can change some options per request using ini_set(). Some SAPIs support per-directory overrides using .user.ini. Apache module users can set a few options inside .htaccess files with specific directives. Only override what you need; keep configuration simple and obvious.
display_errors on a public server. Show friendly messages to users and send errors to logs instead.