What Is PHP and What Is It Used For?
PHP is a server-side scripting language used mainly to build dynamic websites and web applications. When a visitor requests a PHP page, the web server runs the PHP code first, then sends the resulting HTML to the browser. You would use PHP when a page needs to change based on user input, stored data, or a database — for example, a login form, a product listing, or a contact form that saves messages. You don't need PHP for a purely static site.
How PHP actually runs
PHP is not executed by the browser. It runs on the web server, which means visitors never see your PHP source code — only the output it produces.
A typical request works like this:
- The browser requests a URL such as
example.com/greet.php. - The web server (commonly Apache) recognizes the
.phpextension and hands the file to the PHP interpreter. - PHP executes the code, which may read a database, check a session, or process form data.
- PHP outputs HTML (or JSON, or an image), and the server sends that output back to the browser.
This is the key difference from JavaScript: JavaScript runs in the browser after the page arrives, while PHP runs on the server before the page is sent.
Common things PHP is used for
- Generating dynamic pages — inserting different content depending on the user, the time, or the URL.
- Handling form submissions — receiving data from an HTML form, validating it, and responding.
- Connecting to databases — reading and writing records in MariaDB or MySQL, which is how most PHP applications store users, posts, and orders.
- Managing sessions and cookies — keeping a user logged in across multiple pages.
- Building APIs — returning JSON so a front-end or mobile app can fetch data.
- Content management systems — WordPress, a widely used CMS, is written in PHP.
A minimal PHP example
<?php
$name = "World";
echo "Hello, " . $name . "!";
?>
When the server runs this, the browser receives only:
Hello, World!
The <?php ... ?> tags mark where PHP code begins and ends. echo sends output to the page. Variables start with $. You can mix PHP blocks with regular HTML in the same file, which is why PHP is convenient for templating.
How PHP fits with Apache and MariaDB
PHP rarely works alone. In a typical stack:
| Component | Role |
|---|---|
| Apache | Web server that receives requests and passes .php files to PHP |
| PHP | Executes application logic and produces output |
| MariaDB / MySQL | Stores the data the PHP code reads and writes |
This combination is often called a LAMP-style stack (Linux, Apache, MySQL/MariaDB, PHP), with variations across operating systems.
Getting started locally with XAMPP
To run PHP on your own machine without configuring each piece separately, you can use an integrated package. XAMPP is one option: according to Apache Friends, it is "a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl," and it is described as a popular PHP development environment.
The general setup path:
- Download the XAMPP installer for your operating system (Windows, Linux, or OS X versions are listed on the site).
- Run the installer and start the Apache and MySQL/MariaDB modules from the control panel.
- Place your
.phpfiles in the web root directory (commonlyhtdocs). - Open a browser and visit
localhostfollowed by your file path, such aslocalhost/greet.php. - Verify it worked: if you see "Hello, World!" instead of the raw code, PHP is executing correctly.
Common snags: if the browser shows the PHP source code as plain text, the file likely isn't being processed by PHP — check the file extension and that Apache is running. If the page won't load at all, confirm the server is started and the port isn't already in use.
Where PHP is used today
PHP remains widely deployed because it is easy to host, has extensive documentation, and powers a large share of existing websites and CMS platforms. It is a practical choice when you want to build server-rendered web pages, work with an existing PHP codebase, or use a PHP-based CMS. If your goal is a purely static site or a browser-only interface, PHP may be unnecessary.
To decide whether to learn it: choose PHP if you want to build or maintain dynamic, database-driven websites and value a large hosting ecosystem. If you only need client-side interactivity, JavaScript alone may cover your needs.