Introduction to PHP

Learn what PHP is, how it works on the server, and write your very first PHP program. 5 minute read.

Beginner 5 min read 3 examples

What is PHP?

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used, open-source server-side scripting language designed primarily for web development. Created by Rasmus Lerdorf in 1994, PHP has evolved from simple "Personal Home Page" scripts into a mature, general-purpose language powering over 77% of all websites that use a known server-side language.

Key Fact

WordPress, the platform behind 43% of all websites, is written entirely in PHP. Laravel, Symfony, and Drupal - some of the most popular web frameworks - are also PHP-based.

Unlike HTML (which is static), PHP generates HTML dynamically on the server before it reaches the browser. This means you can connect to databases, process form submissions, manage user sessions, and build full web applications - all with PHP.

How PHP Works

PHP follows a simple request-response cycle:

  1. A user visits example.com/page.php in their browser.
  2. The browser sends an HTTP request to the web server (Apache/Nginx).
  3. The server recognizes .php files and passes them to the PHP engine.
  4. PHP executes the script - reads databases, processes logic, builds HTML.
  5. The server sends the generated HTML response back to the browser.
  6. The browser renders the HTML. The user never sees PHP code.
PHP vs JavaScript

PHP runs on the server before the page is sent. JavaScript runs in the browser after the page loads. Both are essential in modern web development - and frequently work together.

What Can PHP Do?

PHP can handle almost any server-side task:

  • Dynamic websites - generate HTML based on user data or database content
  • REST APIs - serve JSON to mobile apps and single-page applications
  • Form processing - validate, sanitize, and store user input
  • Authentication - login systems, sessions, password hashing
  • File operations - upload, read, write, delete files on the server
  • Email sending - via PHPMailer or built-in mail()
  • Database CRUD - full MySQL/PostgreSQL/SQLite operations via PDO
  • CLI scripting - cron jobs, data processing, automation scripts

PHP Versions

Always learn and use the latest stable PHP version:

VersionStatusKey Additions
PHP 8.3CurrentTyped class constants, readonly improvements, json_validate()
PHP 8.2ActiveReadonly classes, null/false/true standalone types
PHP 8.1ActiveEnums, Fibers, readonly properties, intersection types
PHP 8.0Security onlyNamed args, match, nullsafe operator, union types
PHP 7.xEnd of LifeAvoid for new projects

Your First PHP Program

PHP code lives inside <?php ... ?> tags. You can mix PHP and HTML freely, or write pure PHP files. Here is the classic Hello World:

PHP hello.php
<?php
echo "Hello, World!";
Hello, World!

A slightly more practical example - showing the current date and server info:

PHP info.php
<?php
$name    = "RubanLearn";
$version = PHP_VERSION;
$today   = date("Y-m-d");

echo "Welcome to $name!\n";
echo "PHP Version: $version\n";
echo "Today is: $today\n";
Welcome to RubanLearn! PHP Version: 8.3.4 Today is: 2024-11-15
Embedding PHP in HTML

You can mix PHP inside HTML. Only the PHP parts execute on the server - the rest is sent as-is to the browser:

PHP
<!DOCTYPE html>
<html>
<body>
    <h1><?php echo "Hello " . htmlspecialchars($name); ?></h1>
    <p>Today: <?= date("D, d M Y") ?></p>
</body>
</html>

PHP vs Other Server-Side Languages

LanguagePrimary UseLearning CurvePerformance
PHPWeb, APIs, CMSEasyGood (PHP-FPM)
PythonWeb, AI/ML, DataEasyModerate
Node.jsWeb, APIs, Real-timeModerateGood (async)
JavaEnterprise, AndroidHardVery Good
GoAPIs, MicroservicesModerateExcellent

PHP's biggest advantage for beginners: zero configuration to see results. Save a .php file in your web server's folder, visit the URL - done.

Common Beginner Mistakes

Watch Out For These
  • Forgetting the semicolon ; at the end of statements
  • Using echo outside PHP tags without <?php
  • Mixing single-quoted strings (no variable interpolation) with double-quoted expectations
  • Outputting user input directly without htmlspecialchars() - XSS vulnerability
  • Not checking if a variable exists before using it (isset() and empty() are your friends)

Next Steps

Now that you understand what PHP is and how it works, move on to:

Frequently Asked Questions

PHP powers more than 77% of websites on the internet, including WordPress, Facebook (originally), and Wikipedia. In 2024 it remains the dominant server-side scripting language for building web applications, REST APIs, content management systems, and e-commerce platforms like WooCommerce and Magento.

Yes. PHP has a gentle learning curve, especially if you already know basic HTML. The syntax is readable and you can see results immediately in a browser. Most beginners can build their first dynamic webpage within a few hours of starting.

For local development, install XAMPP (Windows/Mac/Linux) or Laragon (Windows) - both are free and set up Apache, MySQL, and PHP in one click. Alternatively, use an online PHP sandbox to run code without any installation.

PHP runs on the server - the user never sees PHP code, only the HTML it generates. JavaScript runs in the browser (client-side) and can manipulate the page after it loads. Modern PHP (8.x) is also used for CLI scripting and building APIs. Both are frequently used together in full-stack development.

Learn PHP 8.x (currently 8.3). PHP 8 introduced many modern features: named arguments, match expressions, nullsafe operator, union types, readonly properties, and fibers. Avoid learning PHP 5 or 7 patterns - they are outdated.