Menu
CoddyTech

Modules and first run

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

C# compiles an assembly with an entry point; PHP executes a script beginning with <?php and loads modules using require_once. C# using directives import namespace names, not PHP-style source files. This course uses ordinary PHP CLI rather than ASP.NET or a PHP web framework.

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.

C# program

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;

class Program {

 static void Main() {
  Console.WriteLine("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 C# Developers

Practice on your own: Online PHP compiler