100 Helloslanguages
Home / Languages / PHP

PHP

1995fraglet
webimperativeobject-orientedscripting.php
docker run --rm --platform="linux/amd64" 100hellos/php:latest

MCP + fragletc

MCPstdinargs
This language supports code execution via MCP and the fragletc CLI. Stdin piping and argument passing are both supported.
Install fragletc →

PHP is a web-focused imperative and object-oriented language first appearing in 1995.

Hello World

#!/usr/bin/env php

<?php
// BEGIN_FRAGLET
echo "Hello World!";
// END_FRAGLET
?>

Coding Guide

Language Version

PHP 8.x

Execution Model

  • Interpreted, runs directly from source
  • Code executes at the top level within <?php ... ?> tags
  • No explicit main function required

Key Characteristics

  • C-style syntax
  • Dynamic typing
  • Case-sensitive
  • Indentation is preserved based on the injection point

Fragment Authoring

Write normal PHP code between the provided tags; your fraglet becomes the script body. PHP tags are already in place, so just write the code you want to run (functions, classes, statements).

Available Packages

Standard PHP library is available. No additional packages are pre-installed.

Common Patterns

  • Print: echo "message"; or print("message");
  • String interpolation: "Total: $count" or "Total: {$count}"
  • Arrays: [1, 2, 3] or array(1, 2, 3)
  • Functions: function name() { }
  • Loops: foreach ($array as $item) { }
  • Conditionals: if ($condition) { }

Examples

// Simple output
echo "Hello, World!";

// Function definition
function greet($name) {
    return "Hello, $name!";
}

echo greet("Alice");

// Array processing
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(function($x) { return $x * $x; }, $numbers);
echo "Sum of squares: " . array_sum($squared);

// Class example (now possible with range-based injection)
class Calculator {
    public static function sum($numbers) {
        return array_sum($numbers);
    }
}

$numbers = [1, 2, 3, 4, 5];
echo "Sum: " . Calculator::sum($numbers);

Caveats

  • PHP requires semicolons at the end of statements
  • Variables must be prefixed with $
  • String concatenation uses . operator
  • PHP tags are not needed in fragments (already in the file)

Fraglet Scripts

Echo Args

#!/usr/bin/env -S fragletc --vein=php
echo "Args: " . implode(" ", array_slice($argv, 1)) . "\n";

Primes

#!/usr/bin/env -S fragletc --vein=php
function isPrime(int $n): bool {
    if ($n < 2) return false;
    if ($n % 2 === 0) return $n === 2;
    for ($i = 3; $i * $i <= $n; $i += 2) {
        if ($n % $i === 0) return false;
    }
    return true;
}

$nums = range(10, 25);
$primes = array_values(array_filter($nums, 'isPrime'));
echo implode(",", $primes) . PHP_EOL;

Stdin Upper

#!/usr/bin/env -S fragletc --vein=php
echo strtoupper(trim(file_get_contents("php://stdin"))) . "\n";

Test

#!/usr/bin/env -S fragletc --vein=php
echo "Hello World!";

Connections

Container Info

image100hellos/php:latest
build scheduleSaturday
fragletenabled