Intro to PHP
The language behind 77% of the web. WordPress, Wikipedia, and this very platform run on PHP.
The language behind 77% of the web. WordPress, Wikipedia, and this very platform run on 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.
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.
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?>
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.
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.
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.
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?>
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?>
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 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.
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.
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.
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.
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.
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.
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.
PHP does not need a separate server process. Apache or Nginx handles it automatically. Write code, save the file, refresh the browser. Instant feedback.
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.
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?>
PHP is already installed on your terminal. Open it up and start writing code right now.
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?>
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.
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.
77% of websites with a known server-side language use PHP. WordPress alone accounts for 43% of all websites.
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.
Every PHP variable starts with a dollar sign: $name, $age, $score. No type declarations needed. Strings use the dot operator for concatenation.
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.
Put your new knowledge into practice!