Lua for PHP Developers
Move from PHP arrays, closures and classes to Lua 5.1 tables, functions and metatables. Practice real multi-file modules, build a stock ledger, and port two complete PHP programs.
PHP and Lua side by side
The same program in PHP and in Lua, from the lessons of this course.
Print and Comment
PHP scripts begin with <?php; Lua files have no opening tag. PHP uses . to concatenate strings, while Lua uses ... PHP echo writes exactly its arguments and adds no newline. Lua print adds a newline and separates multiple arguments with tabs. Assemble one…
PHP1<?php2echo "Hi" . ", Ada" . "\n";Lua1print("Hi" .. ", Ada")Null and Missing Fields
PHP arrays can contain a key with a null value. array_key_exists detects that key, while isset reports false for both missing and null-valued entries. Lua nil means absence: assigning it to a table field removes that field. Reading an absent Lua field returns…
PHP1<?php2$d = ["x" => null];3echo array_key_exists("x", $d) ? "present\n" : "missing\n";Lua1local d = {x = nil}2print(d.x == nil)PHP for to Numeric for
A PHP for statement spells out initialization, condition, and update. Lua numeric for accepts a start, inclusive limit, and optional step. The step defaults to one. A negative step counts downward; a positive step with a starting value above the limit runs…
PHP1<?php2$total = 0;3for ($i = 1; $i <= 3; $i++) { $total += $i; }4echo $total, "\n";Lua1local total = 02for i = 1, 3 do total = total + i end3print(total)Functions and Returns
PHP functions declare parameters with dollar-prefixed names and may declare parameter and return types. Lua function parameters are ordinary names, with no built-in type annotations. Use local function for a helper that should stay inside its module. Both…
PHP1<?php2function twice(int $x): int { return $x * 2; }3echo twice(4), "\n";Lua1local function twice(x) return x * 2 end2print(twice(4))PHP Lists to Dense Tables
PHP arrays combine list and map behavior. Removing a PHP element with unset does not renumber the remaining keys; array_splice can remove and close a list gap. Lua dense tables use consecutive integer keys from one. table.remove(values, i) removes an element…
PHP1<?php2$a = ["a", "b", "c"];3array_splice($a, 1, 1);4echo implode(",", $a), "\n";Lua1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))Sorting and Comparators
PHP sort and usort modify the array supplied by reference. Lua table.sort also sorts in place, but returns no result. PHP custom comparators return a negative integer, zero, or a positive integer. Lua comparators return a boolean stating whether the first…
PHP1<?php2$a = [3, 1, 2];3usort($a, function ($a, $b) { return $a <=> $b; });4echo $a[0], "\n";Lua1local a={3,1,2}2table.sort(a)3print(a[1])
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Lua Programs
6 lessonsValues and Decisions
5 lessonsIteration and Flow
5 lessonsFunctions and Closures
5 lessonsTables as Collections
5 lessonsOrdering, Modules and Errors
5 lessonsObjects and Owned State
5 lessonsComposition and Object Lookup
6 lessonsProject: Stock Ledger
4 lessonsFinal PHP Ports
2 lessonsFrequently asked questions
Who is Lua for PHP Developers for?
For people who already write PHP and want to learn Lua. Each lesson starts from PHP you know and shows how Lua does the same thing, so you skip the beginner basics.
Do I need to know Lua already?
No. The course starts with your first Lua program. You need PHP basics: variables, conditions, loops, functions and collections.
How many lessons does Lua for PHP Developers have?
Lua for PHP Developers contains 48 interactive lessons you can complete at your own pace.
What does Lua for PHP Developers cover?
The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.
Is Lua for PHP Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.