PHP (PHP: Hypertext Preprocessor) is a server-side scripting language designed specifically for web development. Unlike HTML, CSS, and JavaScript that run in the browser, PHP code runs on the web server and generates HTML that is then sent to the client's browser. This makes PHP perfect for creating dynamic web pages that can interact with databases, process forms, manage sessions, and much more.
In this lesson, we'll learn how to create a simple PHP script that demonstrates basic syntax and functionality. By the end, you'll understand how to write, save, and run PHP code in your local development environment.
Polya's Problem Solving Approach for Creating a PHP Script
Step 1: Understand the Problem
Before writing any code, let's clarify what we're trying to achieve:
Create a simple PHP script that demonstrates basic PHP functionality
Make sure the script runs correctly in a local server environment
Display dynamic content that wouldn't be possible with just HTML
Input: PHP code we will write
Output: HTML content generated by our PHP code and displayed in a browser
Step 2: Devise a Plan
Let's break down the process into manageable steps:
Verify our local server environment is working (XAMPP, MAMP, Docker, etc.)
Implement control structures (conditionals, loops)
Display dynamic content (current date/time, server info)
Run and test our script in a browser
Step 3: Execute the Plan
Now let's follow our plan step by step:
Setting Up Your Environment
Verify Local Server Environment
Before we start coding, make sure your local server environment is up and running. You'll need one of the following:
XAMPP: Start Apache module from the XAMPP Control Panel
MAMP: Start servers from the MAMP application
Docker: Make sure your PHP container is running
Note: For this course, we recommend using Docker as mentioned in Module 1, Session 1. However, XAMPP or MAMP are also valid alternatives.
Directory Structure
Create a new file in your web server's document root. The location depends on your setup:
XAMPP: C:\xampp\htdocs\ (Windows) or /Applications/XAMPP/htdocs/ (Mac)
MAMP: /Applications/MAMP/htdocs/ (Mac) or MAMP installation directory (Windows)
Docker: The directory you mapped to your container's web root
In your project folder, create a new file named first_script.php.
Writing Your First PHP Script
Basic PHP Syntax
Open your first_script.php file in VS Code or your preferred editor. Let's start with the most basic PHP script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First PHP Script</title>
</head>
<body>
<h1>Welcome to PHP!</h1>
<?php
// This is a PHP comment
echo "<p>Hello, World! This is my first PHP script.</p>";
?>
<p>This is regular HTML after the PHP section.</p>
</body>
</html>
Key Elements:
<?php - Opening PHP tag that tells the server to start interpreting the code as PHP
echo - Command that outputs text (similar to console.log in JavaScript)
?> - Closing PHP tag that tells the server to stop interpreting as PHP
Running Your PHP Script
To run your script:
Save the file
Open your web browser
Navigate to http://localhost/my_php_project/first_script.php
The exact URL may vary depending on your setup. For example:
MAMP with custom port: http://localhost:8888/my_php_project/first_script.php
Docker with custom port: http://localhost:YOUR_PORT/my_php_project/first_script.php
Troubleshooting: If you see the PHP code instead of the executed result, your server isn't processing PHP correctly. Check if your server is running and if the file has a .php extension.
Working with Variables and Data Types
Now let's enhance our script to include variables and different data types. Replace the content of your first_script.php with:
Add this at the top of your PHP files during development (but not in production).
Real-World Applications
PHP is used extensively across the web for various applications:
WordPress and PHP
WordPress, which powers about 42% of all websites on the internet, is built using PHP. Understanding PHP is crucial for WordPress theme and plugin development, which we'll explore in later modules of this course.
Going Further
Homework Challenge Ideas
Basic: Personal Info Page - Create a PHP script that displays your name, age, favorite hobbies, and skills in a nicely formatted page.
Intermediate: Calculator - Create a simple calculator that can add, subtract, multiply, and divide two numbers input via a form.
Advanced: To-Do List - Create a simple to-do list that stores tasks in a session and allows adding, completing, and removing tasks.
You've taken your first steps into the world of PHP programming! This server-side language opens up countless possibilities for creating dynamic, interactive websites. As we continue through this course, you'll build upon these fundamentals to create more complex applications, including WordPress themes and plugins.
Remember that PHP, like any programming language, requires practice. Don't be afraid to experiment with the code examples provided and try creating your own PHP scripts to reinforce what you've learned.
Next Up in Module 2: Database integration with MySQL, more advanced PHP concepts, and an introduction to WordPress development.