Practical PHP for Aspiring Framework Developers
PHP Lifecycle and Environment
The Server-Side Story
When you visit a website, your browser (the client) sends a request over the internet to another computer (the server). The server then finds the requested files and sends them back to your browser, which displays the page.
For simple websites with just HTML and CSS, the server just sends the files as they are. But for dynamic websites, like blogs or online stores, the server has to do some work first. This is where server-side languages like PHP come in. PHP code runs on the server, not in your browser. It can do things like fetch the latest blog posts from a database, check if a user is logged in, or process a contact form. After the PHP code runs, it generates a plain HTML file that is sent back to your browser.
Think of it like ordering at a restaurant. You (the client) give your order (the request) to the waiter. The waiter takes it to the kitchen (the server), where the chefs (PHP) cook your meal from various ingredients (data). You don't see the cooking process. You just get the finished plate of food (the HTML page) delivered to your table.
The PHP Lifecycle
One of the most important things to understand about PHP is its request-response lifecycle. Every time someone visits a PHP page, the process starts from scratch and ends completely. It's a short, focused burst of activity.
Here’s how it works:
- Your browser requests a page, like
contact.php. - The web server sees the
.phpextension and wakes up the PHP interpreter. - The PHP interpreter reads the file, executes all the code inside, and generates an HTML output.
- The server sends this finished HTML back to your browser.
- The PHP interpreter shuts down completely. All memory and resources it used are released.
This is very different from some other server technologies, like Node.js, where an application is started once and keeps running, waiting for requests. With PHP, each page load is a brand new, independent event. This is why a PHP script can't "remember" anything from a previous visit on its own; it needs tools like sessions or databases to store information between requests.
Each request to a PHP page starts a new process that lives only long enough to generate a response, and then it's gone.
Setting Up Your Workshop
To write and test PHP code, you can't just open a .php file in your browser like you would an HTML file. Your browser doesn't know how to run PHP. You need to simulate a web server environment on your own computer. This is called a local development environment.
Luckily, you don't have to install and configure all the pieces separately. Tools like XAMPP, MAMP, or LocalWP bundle everything you need into a single, easy-to-install package. These packages typically include:
- A web server: Usually Apache, which listens for requests from your browser.
- PHP: The interpreter that actually runs your code.
- A database server: Usually MySQL or MariaDB, for storing data.
Setting up one of these tools gives you a personal web server right on your machine, allowing you to build and test your projects before you ever put them online.
For beginners, tools like XAMPP bundle the PHP interpreter, Apache, and MariaDB (a database server that’s compatible with MySQL) together for easy setup.
Your First PHP File
PHP files are just plain text files, but they must be saved with a .php extension. This extension is the signal to the web server that the file contains code that needs to be processed by the PHP interpreter.
PHP code is written inside special tags. The most common opening tag is <?php and the closing tag is ?>. Everything inside these tags is treated as PHP code. Anything outside the tags is treated as regular HTML and is passed through to the browser untouched.
This means you can easily mix PHP logic right into your HTML structure.
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1>This is a standard HTML heading.</h1>
<p>
<?php
// This is PHP code
echo "And this text was generated by PHP!";
?>
</p>
</body>
</html>
If you saved this file as hello.php on your local server and visited it in your browser, you wouldn't see the PHP code. The server processes it first. The PHP interpreter sees the echo command and outputs the string of text. The browser only receives the final HTML:
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1>This is a standard HTML heading.</h1>
<p>
And this text was generated by PHP! </p>
</body>
</html>
This separation is key. The server does the dynamic work, and the browser just has to worry about displaying the final result.
Now that you understand the basic environment and lifecycle, you're ready to start exploring what the language can do.