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.
| Concern | PHP | Lua |
|---|---|---|
| Entry point | Run 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. |
| Collections | Arrays 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 capabilities | Extensions and runtime configuration determine what a PHP installation exposes. | An embedding application chooses which functions and libraries scripts can call. |
| Types | Dynamic values with optional parameter, return and property declarations. | Runtime types, with no standard static type declarations in Lua 5.4. |
Print the enabled rules for a level
Section titled “Print the enabled rules for a level”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.
<?phpdeclare(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) endendRun 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.
Keep the level format portable
Section titled “Keep the level format portable”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.