Menu
CoddyTech

Modules and first run

Lesson 1 of 48 in Coddy's PHP for Lua Developers course.

Lua runs a chunk and commonly returns a table from a module loaded with require. PHP executes a script and loads declarations with require_once. PHP files need an opening tag, and __DIR__ anchors dependencies. Keep reusable modules free of top-level output in the CLI exercises.

PHP CLI files start with an opening PHP tag. Keep reusable declarations in modules and output in the entry script. require_once loads a file only once; __DIR__ anchors its path to the current file instead of the working directory. These courses use PHP 8.3 syntax and standard CLI input, not a web framework. Strict scalar calls are requested with declare(strict_types=1) in the caller file.

Lua program

print("hello CLI")

Source output:

hello CLI

PHP program

demo.php

<?php
declare(strict_types=1);
echo "hello CLI\n";

PHP output:

hello CLI
challenge icon

Challenge

Easy

Run unchanged. The main script loads Functions.php relative to its own directory and prints PHP is ready.

The supplied main script reads one JSON value from standard input, checks the named public APIs, and prints the result. Implement the module declarations without printing; do not replace the launcher or read input from a module. Input satisfies the stated types and bounds.

Edit Functions.php. main.php is the locked entry file. Dependencies use require_once __DIR__ with a relative filename.

Try it yourself

<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}

$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(welcome()==="PHP is ready","welcome");
echo welcome(),"\n";

All lessons in PHP for Lua Developers

Practice on your own: Online PHP compiler