PHP Programming

PHP from the first program to arrays, functions, closures, classes, interfaces, traits and exceptions, with every program run in your browser.

Level
Beginner
Time
About 20 hours
Modules
5
Lessons
15
Exercises
45

PHP is the language behind WordPress, Laravel and a very large share of the web. It is easy to start with and powerful enough for large applications, and it is one of the most useful languages for a future web developer. This course teaches PHP 8.4 from the first line of code to classes, interfaces, traits and exceptions.

Every example runs on PHP in your browser, so there is nothing to install, and every exercise is checked against hidden tests. The programs read their input from the Input box in the same way as the php command on a terminal, and they run unchanged on a real PHP 8 installation.

What you will learn

  • Write and run PHP programs that read input and print results
  • Use types, strings, conditions, loops and functions correctly
  • Store data in indexed and associative arrays
  • Use closures, arrow functions and the built-in array and string functions
  • Design classes with encapsulation, inheritance, interfaces and traits
  • Handle errors with exceptions and build a small complete program

Skills you will gain

  • PHP syntax and types
  • Arrays and built-in functions
  • Functions and closures
  • Object-oriented design
  • Exceptions and validation

Before you start

  • No previous programming is required
  • Comfort with basic arithmetic and reading careful instructions
  • Nothing to install: PHP runs in your browser

Where it is used

  • Web development and content systems such as WordPress
  • Back-end work with Laravel and Symfony
  • Command-line scripts and automation
  • University courses and interviews that include PHP

Modules

Module 1: PHP basics

Your first program, variables and types, and working with text.

  1. 1

    Your first PHP program

    How a PHP file is laid out, printing with echo, print and printf, and the difference between single and double quotes.

  2. 2

    Variables and types

    Variables, the types int, float, string, bool and null, type conversion with casts, division and constants.

  3. 3

    Strings

    Joining text with the dot, trimming, searching, cutting and changing strings with the most useful functions.

Module practice and quiz →

Module 2: Input, decisions and loops

Reading input, making decisions with conditions and repeating work with loops.

  1. 1

    Input from STDIN

    Reading lines, numbers and lists from the standard input with fgets, fscanf and explode, and reading until the input ends.

  2. 2

    Operators and conditions

    Arithmetic and comparison operators, if, elseif and else, the ternary and null coalescing operators, switch and match.

  3. 3

    Loops

    Repeating code with for, while, do-while and foreach, and controlling loops with break and continue.

Module practice and quiz →

Module 3: Arrays and functions

Lists, associative arrays and your own functions.

  1. 1

    Arrays

    Lists of values: indexes, adding and removing items, sorting, searching and tables made of arrays.

  2. 2

    Associative arrays

    Arrays with names as keys: reading and changing pairs, safe reads, sorting by key or value, counting and records.

  3. 3

    Functions

    Writing your own functions: parameters, return values, types, defaults, named arguments, references and recursion.

Module practice and quiz →

Module 4: Closures, built-in functions and objects

Scope and closures, the string and array function toolbox, and the first classes.

  1. 1

    Scope and closures Intermediate

    Local and global scope, static variables, closures with use, arrow functions and callbacks.

  2. 2

    String and array functions Intermediate

    Map, filter, reduce and sort with callbacks, and the most useful string and array functions combined.

  3. 3

    Classes and objects Intermediate

    Classes, objects, properties and methods, constructors, visibility, static members, constants and copying objects.

Module practice and quiz →

Module 5: Object design and complete programs

Inheritance, interfaces and traits, exceptions, and a complete program.

  1. 1

    Inheritance, interfaces and traits Intermediate

    Parent and child classes, abstract classes, interfaces, polymorphism and traits.

  2. 2

    Exceptions Intermediate

    Reporting and handling errors with throw, try, catch and finally, and writing your own exception classes.

  3. 3

    Putting it together Intermediate

    Planning and writing a complete command-line program that reads commands, keeps data in a class and reports errors with exceptions.

Module practice and quiz →

Projects

Final assessment →

What to learn next