PHP Introduction

What Is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language for dynamic websites and web apps. The server runs PHP and sends the finished HTML to the browser—visitors never see your source.
Pipeline:
browser request → web server → PHP engine → HTML response
Tags: <?php ... ?>
Output: echo "Hello";
Files: .php (often mixed with HTML) PHP handles forms, logins, databases, sessions, and APIs. It powers WordPress, Laravel, Symfony, and millions of custom sites. This page is the gateway to the PHP tutorials on CodeToFun.
Why Use PHP?
Rasmus Lerdorf created PHP in 1994 as tools for his personal home page. It grew into a full language maintained by The PHP Group and a large open-source community.
| Need | How PHP helps |
|---|---|
| Dynamic pages | Generate HTML from logic, forms, and databases |
| Easy hosting | Most shared hosts ship PHP ready to use |
| CMS & frameworks | WordPress, Laravel, Symfony, and a huge package ecosystem |
| Fast to start | Embed PHP in HTML and print with echo |
Key milestones
| Milestone | Year | Notes |
|---|---|---|
| PHP created | 1994 | Rasmus Lerdorf personal tools |
| PHP 3 | 1998 | Name: Hypertext Preprocessor |
| PHP 7 | 2015 | Major speed improvements |
| PHP 8+ | 2020+ | JIT, attributes, named arguments |
How a PHP Page Runs
PHP executes on the server before the browser gets a response. Keep this flow in mind.
| Step | What happens | Who does it |
|---|---|---|
| Request | Browser asks for a .php page | User / browser |
| Hand off | Server passes the file to the PHP engine | Apache / Nginx |
| Execute | PHP runs your code and builds output | PHP runtime |
| Respond | HTML/CSS/JS is sent to the browser | Web server |
Cheat sheet — building blocks
| Concept | Role |
|---|---|
| Variables | $name, strings, numbers, arrays |
| Output | echo / print |
| Control flow | if, loops, switch |
| Functions | Reusable logic |
| Forms & input | $_GET, $_POST |
| Sessions | Remember users across pages |
What Does a PHP Program Look Like?
Save this as hello.php. PHP tags wrap the code the server should run:
<?php
echo "Hello, World!";
?> | Part | Meaning |
|---|---|
<?php … ?> | PHP code block processed on the server |
echo | Sends text (often HTML) to the response |
| Semicolons | End most statements |
| HTML outside tags | Sent to the browser unchanged |
You can mix HTML and PHP in one file:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Today is <?php echo date('Y-m-d'); ?></h1>
</body>
</html> PHP vs JavaScript
Both are popular for the web, but they usually run in different places:
| JavaScript (typical) | PHP |
|---|---|
| Runs in the browser (or Node.js) | Runs on the web server |
| Updates the page without a full reload | Builds HTML before the response is sent |
console.log("Hi") | echo "Hi"; |
| Client-side interactivity | Forms, databases, sessions, CMS |
| Source often visible in DevTools | Source stays on the server |
Same idea, two styles
Print a greeting:
console.log("Hello, World!"); <?php
echo "Hello, World!";
?> When to choose which
| Prefer PHP when… | Prefer JavaScript when… |
|---|---|
| You need server logic, WordPress, or classic web hosting | You need rich UI behavior in the browser |
| Your stack is Laravel / Symfony / PHP CMS | Your stack is Node.js or a JS frontend framework |
| Secrets and DB access must stay on the server | You already ship most logic as client JS + APIs |
Many sites use both: PHP (or another backend) for the server, JavaScript for the interface.
Run PHP Locally
Install PHP (or a stack like XAMPP / Laragon), save hello.php, then start a quick built-in server in that folder.
php -v
cd my-project
php -S localhost:8000 How It Works
1. Check install. php -v confirms the PHP CLI is available.
2. Start a server. php -S localhost:8000 serves files from the current folder.
3. Open the page. Visit http://localhost:8000/hello.php. PHP runs echo and the browser shows the text.
Next, learn the most common output statement in PHP echo.
Where Is PHP Used?
Common workloads where server-side PHP fits well:
| Use case | Why PHP fits |
|---|---|
| Content sites & blogs | WordPress and other CMS platforms |
| E-commerce | Carts, checkout, and catalogs with server logic |
| Business web apps | Forms, auth, CRUD dashboards |
| APIs & backends | Return JSON from Laravel / Symfony apps |
| Shared hosting projects | Cheap, widely available PHP hosting |
| Legacy modernization | Huge existing PHP codebase worldwide |
Key Takeaways
- PHP is a server-side language that builds HTML before the browser sees it.
- Pipeline: request → PHP engine → HTML response.
- Start with:
<?phptags andecho. - Local test:
php -S localhost:8000or XAMPP / Laragon.
One line: PHP runs on the server and outputs HTML—next, master echo.
Frequently Asked Questions
Did you know?
PHP runs on the server before HTML reaches the browser. Wrap code in <?php ... ?>, output with echo, and test locally with XAMPP or php -S localhost:8000.
Next: PHP echo
Learn the main way to send output from PHP to the browser.
11 people found this page helpful
