Skip to content

PHP vs Lua

PHP is designed for general-purpose scripting and widely used to build websites. Lua is frequently embedded in a host application that exposes its own scripting APIs. A team might write a site in PHP for uploading and reviewing game levels, then use Lua inside the game to express how each level behaves.

ConcernPHPLua
Entry pointRun a script through the CLI, a web server or an application framework.Run through an interpreter or inside a host such as a game engine.
CollectionsArrays support integer and string keys. A list literal begins at index zero.Tables can represent records and sequences. A sequence literal begins at index one.
Host capabilitiesExtensions and runtime configuration determine what a PHP installation exposes.An embedding application chooses which functions and libraries scripts can call.
TypesDynamic values with optional parameter, return and property declarations.Runtime types, with no standard static type declarations in Lua 5.4.

Both examples print 1: Double points. The displayed rule number starts at one in both programs. The PHP program adds one to the list position. Lua’s ipairs already returns one-based positions when reading a contiguous sequence.

<?php
declare(strict_types=1);
$rules = [
['name' => 'Double points', 'enabled' => true],
['name' => 'Extra time', 'enabled' => false],
];
foreach ($rules as $index => $rule) {
if ($rule['enabled']) {
echo ($index + 1), ': ', $rule['name'], PHP_EOL;
}
}
local rules = {
{ name = "Double points", enabled = true },
{ name = "Extra time", enabled = false },
}
for index, rule in ipairs(rules) do
if rule.enabled then
print(index .. ": " .. rule.name)
end
end

Run with php example.php or lua example.lua. When adapting the Lua script to an embedded runtime, check the host’s version and available functions.

Exchange plain rule data and stable rule IDs between the site and game. The game can map rules to the functions it exposes to Lua, while the site validates and displays the same IDs. If you reorder the list later, the ID can stay the same even when the displayed position changes.