Menu
CoddyTech
Course

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.

  1. 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…

    PHP
    1<?php2echo "Hi" . ", Ada" . "\n";
    Lua
    1print("Hi" .. ", Ada")
  2. 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…

    PHP
    1<?php2$d = ["x" => null];3echo array_key_exists("x", $d) ? "present\n" : "missing\n";
    Lua
    1local d = {x = nil}2print(d.x == nil)
  3. 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…

    PHP
    1<?php2$total = 0;3for ($i = 1; $i <= 3; $i++) { $total += $i; }4echo $total, "\n";
    Lua
    1local total = 02for i = 1, 3 do total = total + i end3print(total)
  4. 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…

    PHP
    1<?php2function twice(int $x): int { return $x * 2; }3echo twice(4), "\n";
    Lua
    1local function twice(x) return x * 2 end2print(twice(4))
  5. 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…

    PHP
    1<?php2$a = ["a", "b", "c"];3array_splice($a, 1, 1);4echo implode(",", $a), "\n";
    Lua
    1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))
  6. 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…

    PHP
    1<?php2$a = [3, 1, 2];3usort($a, function ($a, $b) { return $a <=> $b; });4echo $a[0], "\n";
    Lua
    1local a={3,1,2}2table.sort(a)3print(a[1])

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

Final PHP Ports

2 lessons

More courses for PHP developers

Learn Lua from another language

Frequently 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.

Coddy programming languages illustration

Start Lua for PHP Developers for free

Get started