Skip to content

PHP Tutorial

PHP Tutorial: Learn Modern PHP Step by Step

369 lessons 5 min read By Deepak Paswan Updated Oct 5, 2026

HTML and CSS make a page. JavaScript makes it react in the browser. PHP runs on the server and builds the page, reads the form, checks the password, talks to the database and sends the result back.

This course teaches modern PHP from the first line of code to a small working CMS. Each lesson explains one idea in plain language, shows short code with its expected output, points out the mistakes people usually make and ends with a small practice task.

What is PHP, and where does it run?

PHP is a server-side programming language. When someone opens a page, the web server runs your PHP code, and the browser receives only the result, usually HTML. Start with What Is PHP? and How PHP Works.

PHP code does not run in the browser, so the examples in this course do not have a Try it button. Each PHP example shows the code and the output you should see, and Running Your First PHP File shows how to run it on your own computer in a couple of minutes.

PHP, HTML, CSS and JavaScript

The four work together. PHP builds the HTML, CSS styles it and JavaScript adds behaviour in the browser. If you want a refresher on the other three, see the HTML course, the CSS course and the JavaScript course.

Modern PHP, not PHP 5

Many tutorials still teach PHP the way it was written fifteen years ago. This course teaches how PHP is written today: typed parameters and return values, match, named arguments, constructor property promotion, enums, readonly properties, namespaces and Composer. PHP Versions explains what changed from PHP 5 to PHP 8.

What you will be able to do

By the end of the course you will be able to:

  • Write PHP with variables, conditions, loops, functions and arrays.
  • Handle forms, validate input and keep users logged in with sessions.
  • Read and write files and handle errors with exceptions.
  • Write object-oriented PHP with classes, interfaces and traits.
  • Use PHP 8 features such as enums, match, readonly properties and attributes.
  • Organise code with namespaces and install packages with Composer.
  • Connect to MySQL with PDO and build create, read, update and delete pages.
  • Build registration and login with safe password hashing.
  • Protect an application against SQL injection, XSS, CSRF and unsafe uploads.
  • Work with JSON and APIs and structure a small PHP project.
  • Build 11 projects, from a calculator to a mini CMS.

Before you start

You need a computer, a code editor and PHP. No programming experience is needed. A little HTML helps. Installing PHP and PHP Development Environment walk you through the setup.

How to use this course

  • Go in order. Lessons build on earlier ones. Use the chapter list on the left (or the course button on a phone).
  • Run the code yourself. Type the examples instead of pasting them, then change them and see what happens.
  • Compare with the output. The output block under each example is what the code prints.
  • Do the exercises. Try each one before you open the solution.
  • Read the warnings. Security notes are marked clearly, and the security chapter explains the attacks in detail.
lightbulb
Start here
New to programming? Begin with What Is PHP?. Already know another language? Skim the first chapters and start at Variables.

Frequently Asked Questions

PHP Frequently Asked Questions

PHP is one of the friendlier languages to start with. The basics are variables, conditions, loops and functions, and you can see results quickly. The harder parts come later: security, databases and structuring bigger projects. This course goes in small steps and explains why each habit matters.
A little HTML helps, because PHP usually produces HTML. You do not need to be an expert. If you are new to web pages, take the HTML course on this site first or alongside this one.
PHP runs on a server or on the command line, not inside the browser. Install PHP or a bundle such as XAMPP, Laragon or MAMP, save a file as index.php and run it. The lesson on running your first PHP file shows each step. Every example in this course shows the code and the output you should see.
Because PHP cannot run in your browser, and running visitors’ code on a public server would be unsafe. HTML and JavaScript lessons on this site have Try it editors. PHP lessons show the code with its expected output, and you run the code on your own machine.
The course teaches current PHP 8 habits: type declarations, match, enums, readonly properties and named arguments. Each lesson says which version a feature needs, so you know what an older server will reject.
Yes. WordPress is written in PHP, and Laravel is a PHP framework built on object-oriented code, namespaces and Composer. The course teaches plain PHP and adds short notes on why each topic matters in WordPress and Laravel.
No. The database chapters explain the small amount of SQL you need to save, read, update and delete records with PDO. A separate, deeper database course is not part of this one.

Course contents

Getting Started with PHP

What PHP is, where it runs, how a request becomes a web page, and how to run your first PHP file.

PHP Syntax and Output

The basic rules of writing PHP: statements, printing with echo and print, comments, case rules and mixing PHP with HTML.

Variables and Constants

Storing and reusing values: variables, naming rules, references, constants, scope, static variables and a first look at superglobals.

Data Types

The kinds of values PHP works with: strings, numbers, booleans, arrays, objects, null and resources, plus type checking, casting and strict typing.

Operators

The symbols that calculate, assign, compare and combine values: arithmetic, comparison, logical, string and array operators, plus ?? and ?->.

Numbers and Math

Math functions, rounding, min and max, random numbers, number formatting, validating numeric input and the pitfalls of floating-point maths.

Conditional Statements

Making decisions in code: if, else, elseif, nested conditions, the ternary operator, match and switch.

Loops

Repeating work: while, do...while, for and foreach loops, nested loops, break and continue, and practical loop examples.

Forms and User Input

Receiving data from visitors: GET and POST, reading form values, required fields, validation, sanitizing, error messages and a complete contact form.

Superglobals

The built-in arrays PHP fills for every request: $_GET, $_POST, $_REQUEST, $_SERVER, $_SESSION, $_COOKIE, $_FILES, $_ENV and $GLOBALS.

Include and Require

Splitting code into files: include, require, the _once versions, reusable header and footer parts, configuration files and safe file paths.

Sessions and Cookies

Remembering visitors between requests: how sessions and cookies work, reading and writing them, destroying them and keeping them secure.

Error Handling

Understanding PHP errors and warnings, using exceptions with try, catch, finally and throw, writing custom exceptions, logging, and handling errors safely in production.

Object-Oriented PHP

Classes and objects from the ground up: properties, methods, constructors, visibility, static members, $this, class constants and magic methods.

Advanced OOP

Building on classes: inheritance, overriding, abstract classes, interfaces, traits, final, encapsulation, polymorphism, dependency injection and composition.

Modern PHP Features

The features that define PHP 8: advanced types, constructor promotion, readonly, enums, attributes, first-class callables, iterables and generators, plus what arrived in PHP 8.3, 8.4 and 8.5.

Namespaces and Autoloading

Organising code in larger projects: namespaces, the use statement, name conflicts, project layout, autoloading and the PSR-4 standard.

Composer

Managing libraries the modern way: installing Composer, composer.json, packages, the lock file, autoloading, PSR-4, version constraints and a worked example.

PHP and MySQL

Working with a database: what MySQL is, creating tables, connecting with PDO, prepared statements, select, insert, update, delete, fetching results and handling errors.

Database CRUD

Create, read, update and delete pages with PDO: forms, validation, search, pagination, filtering, sorting and a complete mini admin panel.

Authentication

Letting people sign up and log in safely: registration, password hashing, login sessions, protecting pages, remember me, password reset and common security rules.

PHP Security

Writing code that resists attack: SQL injection, XSS, CSRF, session fixation, upload security, validation, escaping, cookies, headers, secrets and a production checklist.

Working with JSON and APIs

Exchanging data with other programs: JSON encoding and decoding, building JSON responses, HTTP status codes and methods, calling APIs with cURL, handling API errors and authentication basics.

Practical PHP Development

Structuring a real application: configuration, environment variables, reusable components, routing, controllers and views, services, logging, validation and a clean project layout.

PHP for WordPress Developers

How the PHP you have learned is used in WordPress: theme templates, the Loop, hooks, actions and filters, common functions, a first plugin, shortcodes, custom post types and WordPress security basics.

PHP Performance and Best Practices

Writing PHP that is readable, fast enough and easy to maintain: measuring, avoiding repeated work, query optimisation, caching, memory, efficient loops, production settings, standards and maintainable design.

Practical PHP Projects

Eleven projects of growing difficulty: calculator, contact form, to-do list, notes app, file upload manager, login system, blog, product catalogue, search and filter app, JSON API and a mini CMS.

PHP Interview and Practice

Questions with clear answers for beginner to advanced PHP interviews, coding and debugging challenges, and a final assessment to check what you have learned.