<?php echo "Hello!"; ?>

Intro to PHP

The language behind 77% of the web. WordPress, Wikipedia, and this very platform run on PHP.

1

The Story of PHP

In 1995, a developer named Rasmus Lerdorf wanted to track visits to his online resume. He wrote a set of C scripts and called them "Personal Home Page" tools. That modest beginning grew into one of the most widely used programming languages in history.

1995
PHP/FI released by Rasmus Lerdorf. "Personal Home Page / Forms Interpreter." A simple tool for tracking page views and processing forms.
1997
PHP 3 rewritten by Andi Gutmans and Zeev Suraski. The name becomes a recursive acronym: "PHP: Hypertext Preprocessor." Now a real programming language.
2004
PHP 5 brings proper object-oriented programming. Classes, interfaces, exceptions. WordPress launches the same year, built entirely on PHP.
2015
PHP 7 doubles performance. Twice as fast as PHP 5, with dramatically lower memory usage. PHP skipped version 6 entirely.
2020
PHP 8 introduces JIT compilation, named arguments, attributes, and match expressions. Modern, fast, and still evolving.
77%
of websites use PHP
43%
of all sites are WordPress
30
years of development
W
WordPress
43% of all websites
W
Wikipedia
MediaWiki framework
f
Facebook
Originally PHP
S
Slack
Backend services
E
Etsy
E-commerce platform
M
This Platform
Built with PHP
Fun Fact

PHP skipped version 6 entirely. A planned Unicode rewrite ran into so many problems that the team abandoned it and jumped straight from PHP 5 to PHP 7.

2

Hello, World!

PHP code lives inside special tags: <?php and ?>. Everything between those tags is executed on the server. The browser never sees your PHP code. It only sees the output.

1<?php
2
3// Your first PHP program
4echo "Hello, World!";
5
6?>
echo sends text to the browser. The semicolon ends the statement. That is all it takes to produce output in PHP.

Variables in PHP always start with a dollar sign. You do not need to declare their type. PHP figures it out.

1<?php
2
3$name = "Alice";
4$age = 14;
5$score = 98.5;
6
7// String concatenation uses the dot operator
8echo $name . " is " . $age . " years old";
9// Output: Alice is 14 years old
10
11?>

You can try PHP interactively in a REPL (Read-Eval-Print Loop). Type a command, see the result immediately.

php> echo "Hello!";
Hello!
php> $x = 10; echo $x * 3;
30
php> $greeting = "Hi, " . "World"; echo $greeting;
Hi, World
Fun Fact

The dot operator for string concatenation is unique to PHP. Most languages use + to join strings. Rasmus Lerdorf chose the dot so that + always means addition, never string joining. No ambiguity.

3

Arrays: PHP's Workhorse

PHP arrays are incredibly versatile. They work as indexed lists, key-value maps (like Python dictionaries), stacks, queues, and more. One data structure that does it all.

Indexed Arrays

1<?php
2
3// Create an indexed array
4$colors = ["red", "green", "blue"];
5
6// Access by index (starts at 0)
7echo $colors[0];  // "red"
8
9// Add to the end
10$colors[] = "yellow";
11
12// Count elements
13echo count($colors);  // 4
14
15?>

Associative Arrays

1<?php
2
3// Key-value pairs (like a dictionary)
4$student = [
5    "name"  => "Alice",
6    "age"   => 14,
7    "grade" => "A"
8];
9
10echo $student["name"];  // "Alice"
11
12?>

Looping Through Arrays

1<?php
2
3$scores = [85, 92, 78, 96, 88];
4
5foreach ($scores as $score) {
6    echo $score . " ";
7}
8// Output: 85 92 78 96 88
9
10// With key and value
11foreach ($student as $key => $value) {
12    echo $key . ": " . $value . "\n";
13}
14// Output:
15// name: Alice
16// age: 14
17// grade: A
18
19?>
PHP arrays are ordered maps under the hood. They maintain insertion order and support both integer and string keys in the same array. This makes them one of the most flexible array implementations in any language.
4

Functions and String Power

PHP functions use the function keyword. Parameters go in parentheses, and return sends a value back. Simple and familiar.

1<?php
2
3function greet($name, $time) {
4    return "Good " . $time . ", " . $name . "!";
5}
6
7echo greet("Alice", "morning");
8// Output: Good morning, Alice!
9
10?>

PHP has a massive built-in function library. Thousands of functions are available without installing anything. Here are some of the most useful string functions.

strlen()
Get string length
strpos()
Find text position
substr()
Extract a portion
str_replace()
Find and replace
strtolower()
Convert to lowercase
strtoupper()
Convert to uppercase
explode()
Split string to array
implode()
Join array to string
trim()
Remove whitespace
php> echo strlen("Hello");
5
php> echo str_replace("World", "PHP", "Hello World");
Hello PHP
php> $parts = explode(",", "red,green,blue"); print_r($parts);
Array ( [0] => red [1] => green [2] => blue )
php> echo implode(" + ", $parts);
red + green + blue
Fun Fact

PHP has over 1,000 built-in functions. From sending email (mail()) to generating PDF files (pdf_new()), from connecting to databases (mysqli_connect()) to processing images (imagecreate()). If you need it, PHP probably has a function for it.

5

What Makes PHP Special

PHP was built specifically for the web. While other languages were adapted for web development later, PHP was designed for it from day one. That focus gives it real advantages.

Built for the Web

PHP was created to generate HTML. It embeds directly in web pages, handles form data, manages cookies and sessions, and talks to databases natively. Web development is not an afterthought. It is the entire point.

Runs Everywhere

Virtually every web host supports PHP. Shared hosting, cloud platforms, dedicated servers. Upload a .php file and it works. No complex build step, no compilation, no deployment pipeline required.

Massive Ecosystem

Composer (package manager) gives you access to 300,000+ packages. Laravel is one of the most popular web frameworks in any language. WordPress, Drupal, Magento, and MediaWiki are all PHP.

Easy to Learn

PHP has a gentle learning curve. Variables are obvious ($name), syntax is C-like, error messages are helpful. You can build something useful on your first day.

The "Just Works" Language

PHP does not need a separate server process. Apache or Nginx handles it automatically. Write code, save the file, refresh the browser. Instant feedback.

Jobs and Community

PHP developers are in high demand. WordPress alone powers 43% of the web, and every WordPress site needs PHP developers. The community is huge, welcoming, and well-documented.

PHP 8 is modern and fast. With JIT compilation, PHP 8 approaches the speed of compiled languages for many workloads. Named arguments, union types, attributes, and match expressions make the code clean and expressive. PHP in 2024 is a very different language from the PHP of 2005.
1<?php
2
3// Modern PHP 8 features
4
5// Named arguments
6str_contains(haystack: "Hello World", needle: "World");  // true
7
8// Match expression (like a cleaner switch)
9$status = match($code) {
10    200 => "OK",
11    404 => "Not Found",
12    500 => "Server Error",
13};
14
15?>
6

Try It Yourself

PHP is already installed on your terminal. Open it up and start writing code right now.

# Install PHP (if not already installed)
$ apt install php
# Create a PHP file
$ nano hello.php
# Run it
$ php hello.php
Hello, World!
# Or use the interactive REPL
$ php -a
Interactive shell
php> echo 2 + 2;
4
This is the same language that built the platform you are on right now. Every page you have loaded, every project you have saved, every discovery you have read was served by PHP running on this server.
1<?php
2
3// A complete mini-program to try
4
5$languages = [
6    "PHP"        => 1995,
7    "JavaScript" => 1995,
8    "Python"     => 1991,
9    "Ruby"       => 1995,
10];
11
12foreach ($languages as $name => $year) {
13    $age = date("Y") - $year;
14    echo $name . " is " . $age . " years old\n";
15}
16
17?>
Fun Fact

1995 was a remarkable year for programming languages. PHP, JavaScript, Ruby, and Java were all released within months of each other. Three decades later, all four are still widely used.

PHP Developer!

You have explored PHP, the language that quietly powers most of the web. From personal homepages to Wikipedia, WordPress, and even this platform, PHP keeps the internet running.

0
Sections Explored
0
Time Exploring

Powers Most of the Web

77% of websites with a known server-side language use PHP. WordPress alone accounts for 43% of all websites.

Built for Web Development

PHP was designed specifically for the web. It runs on virtually every web host, and its ecosystem includes frameworks like Laravel, CMS tools like WordPress, and package managers like Composer.

Dollar Sign Variables

Every PHP variable starts with a dollar sign: $name, $age, $score. No type declarations needed. Strings use the dot operator for concatenation.

This Platform Uses It

The platform you are using right now is built with PHP. Every page you load, every project you save, every discovery you read is served by PHP code.

Ready to Create?

Put your new knowledge into practice!

Suggest a Correction