Menu
CoddyTech

Modules and first run

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

JavaScript Node scripts run without a PHP opening tag. PHP uses <?php and explicit require_once paths. Node CommonJS require returns exported values; PHP require_once loads declarations without creating a module namespace. This course uses CLI scripts, not browser DOM or HTTP handlers.

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.

JavaScript program

console.log("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 JavaScript Developers

Practice on your own: Online PHP compiler