Web Analytics

Introduction to PHP

Beginner ~20 min read

Welcome to PHP! PHP (Hypertext Preprocessor) is one of the most popular server-side scripting languages, powering over 75% of all websites including Facebook, WordPress, and Wikipedia. In this lesson, you'll discover what makes PHP special and why it's an excellent choice for web development.

What is PHP?

PHP is a server-side scripting language designed specifically for web development. Unlike HTML which runs in the browser, PHP code executes on the web server and generates HTML that's sent to the user's browser.

Output
Click Run to execute your code
Fun Fact: PHP originally stood for "Personal Home Page" when it was created in 1994 by Rasmus Lerdorf. It was later renamed to "PHP: Hypertext Preprocessor" - a recursive acronym!

Server-Side vs Client-Side

Understanding the difference between server-side and client-side code is crucial for web development:

Aspect Server-Side (PHP) Client-Side (JavaScript)
Execution Runs on the web server Runs in the user's browser
Visibility Code is hidden from users Code is visible to users
Use Cases Database access, file operations, authentication Interactive UI, animations, form validation
Security More secure for sensitive operations Can be manipulated by users
PHP Client-Server Architecture
Output
Click Run to execute your code
Pro Tip: PHP generates the current time on the server, so every user sees the server's time, not their local time. This is perfect for ensuring consistency across all users!

Why Learn PHP?

PHP remains incredibly relevant in modern web development for several compelling reasons:

  • Widespread Adoption: Powers 75%+ of all websites (WordPress, Facebook, Wikipedia)
  • Easy to Learn: Simple syntax perfect for beginners
  • Powerful Features: Built-in functions for databases, files, sessions, and more
  • Great Community: Massive ecosystem of frameworks (Laravel, Symfony) and libraries
  • Job Market: High demand for PHP developers worldwide
  • Free & Open Source: No licensing costs

What Can PHP Do?

PHP is incredibly versatile for web development. Here are some common use cases:

PHP Powers:
  • Dynamic Web Pages: Generate HTML content based on user input or database data
  • Form Processing: Handle user submissions, validate data, send emails
  • Database Operations: Create, read, update, delete (CRUD) operations with MySQL, PostgreSQL, etc.
  • User Authentication: Login systems, sessions, cookies, password hashing
  • File Management: Upload, download, read, write files on the server
  • APIs: Build RESTful APIs for mobile apps and SPAs
  • E-commerce: Shopping carts, payment processing, inventory management
  • Content Management: WordPress, Drupal, Joomla are all built with PHP

PHP Versions

PHP has evolved significantly over the years. Here's what you need to know:

Version Release Year Key Features Status
PHP 5.x 2004-2018 OOP improvements, PDO ❌ End of Life
PHP 7.x 2015-2022 2x faster, type declarations ⚠️ Security fixes only
PHP 8.0 2020 JIT compiler, named arguments, union types ✅ Active support
PHP 8.1 2021 Enums, readonly properties, fibers ✅ Active support
PHP 8.2 2022 Readonly classes, disjunctive normal form types ✅ Active support
PHP 8.3 2023 Typed class constants, readonly amendments ✅ Active support
Important: This tutorial focuses on PHP 8.0+ features while maintaining compatibility with PHP 7.4. Always use the latest stable version for new projects!

Your First PHP Code

Let's write your very first PHP program! PHP code is enclosed in <?php ?> tags:

Output
Click Run to execute your code
Syntax Breakdown:
  • <?php - Opening PHP tag (required)
  • echo - Outputs text to the screen
  • "Hello, World!" - String (text) in quotes
  • ; - Semicolon ends each statement
  • ?> - Closing PHP tag (optional at end of file)

Common Mistakes

1. Forgetting the PHP tags

// Wrong - no PHP tags
echo "Hello";  // This won't work!

// Correct - wrapped in PHP tags
<?php
echo "Hello";
?>

2. Missing semicolons

// Wrong - missing semicolon
<?php
echo "Hello"  // Parse error!
?>

// Correct - semicolon at end
<?php
echo "Hello";
?>

3. Using wrong quotes

// Wrong - using backticks or smart quotes
<?php
echo `Hello`;  // Backticks are for shell commands!
echo "Hello";  // Smart quotes don't work
?>

// Correct - straight quotes
<?php
echo "Hello";  // Double quotes
echo 'Hello';  // Single quotes
?>

Summary

  • PHP: Server-side scripting language for web development
  • Popularity: Powers 75%+ of all websites including WordPress and Facebook
  • Server-Side: Code runs on the server, not in the browser
  • Use Cases: Dynamic pages, databases, forms, authentication, APIs
  • Modern PHP: PHP 8.0+ brings major performance and feature improvements
  • Syntax: Code goes inside <?php ?> tags
  • echo: Outputs text to the screen
  • Semicolons: Required at the end of each statement

What's Next?

Now that you understand what PHP is and why it's powerful, it's time to set up your development environment! In the next lesson, we'll walk through installing PHP on your computer so you can start writing and testing PHP code locally.