Skip to content
    PHP Sandboxphp-wasm 0.1.0 (WebAssembly)
    Version:
    Output (PHP 8.4)

    Running PHP...

    Loading PHP 8.4...
    © WebFiddle.xyz

    PHP Online Compiler and Sandbox for PHP 8.0 to 8.5

    This PHP online compiler runs genuine PHP builds in your browser, so you can run PHP online without installing anything. WebFiddle loads php-wasm 0.1.0 (WebAssembly), the PHP interpreter compiled to WebAssembly, and executes your script on your own device, so nothing is sent to a server to be run. Choose PHP 8.5, 8.4, 8.3, 8.2, 8.1 and 8.0 from the version menu (8.4 is the default), press Run PHP, and read the result as rendered HTML or as raw text. It works as an online PHP editor for quick experiments, a PHP sandbox for checking how the same function behaves on different releases, and a scratchpad for teaching or answering questions with a runnable link.

    How to use the PHP online compiler

    1. Write or paste PHP in the editor. Open the file with <?php; anything outside the PHP tags is printed as-is, exactly as a web server would send it.
    2. Pick a release from the Version menu. The first time you choose a version its WebAssembly build is downloaded, and the toolbar shows the exact PHP_VERSION once it is ready.
    3. Press Run PHP. The runtime is reset before each run, so nothing from the previous run leaks into the next one.
    4. Switch the output panel between HTML Preview, which renders the markup your script echoes, and Raw Output, which shows the exact text it printed.
    5. Press Share to copy a link that stores your code and the selected PHP version in the URL. Opening that link loads both and runs the script automatically.

    Features

    • Six PHP releases: 8.5, 8.4, 8.3, 8.2, 8.1 and 8.0, each loaded on demand
    • The toolbar reports the PHP_VERSION of the build that is actually running
    • HTML Preview and Raw Output views of the same script output
    • Messages PHP writes to standard error are shown with the output
    • Code is autosaved to local storage and restored on your next visit
    • Share links that carry both the code and the PHP version
    • Syntax-highlighted editor with light and dark themes
    • No account, no install and no server-side execution

    What works and what doesn’t

    • There is no database server. mysqli and PDO have nothing to connect to, so keep test data in arrays or JSON strings while you work on the logic.
    • Network access is not available to PHP. cURL requests, sockets and fetching URLs with file_get_contents() should not be expected to work, because the interpreter runs inside the browser sandbox.
    • Composer, PECL and custom extensions cannot be installed. Only the extensions compiled into the php-wasm build are available; call get_loaded_extensions() to list them.
    • Scripts run through PHP's embed SAPI, not behind a web server, so $_GET, $_POST, cookies and sessions do not describe a real HTTP request, and header() has no visible effect.
    • Files your script writes go to a virtual filesystem inside the browser. Nothing is written to your computer, and there is no way to upload your own files into it.
    • Each PHP version is a download of several megabytes the first time you select it, and a long-running loop keeps the output panel busy until it finishes.

    Example programs

    Named arguments and match expressions (PHP 8.0 and later)

    Named arguments let you skip optional parameters, and match compares strictly and returns a value. This runs on every version in the menu. Switch the output panel to Raw Output so the PHP_EOL line breaks are kept. Expected output: USD 1,234.50, then 1.234,50 €, USD 99 and ¥1,235.

    <?php
    function price(float $amount, string $currency = 'USD', int $decimals = 2): string
    {
        return match ($currency) {
            'EUR' => number_format($amount, $decimals, ',', '.') . ' €',
            'JPY' => '¥' . number_format($amount),
            default => $currency . ' ' . number_format($amount, $decimals),
        };
    }
    
    echo price(1234.5), PHP_EOL;
    echo price(1234.5, currency: 'EUR'), PHP_EOL;
    echo price(amount: 99, decimals: 0), PHP_EOL;
    echo price(1234.5, 'JPY'), PHP_EOL;

    Enums and readonly properties (PHP 8.1 and later)

    A good way to see version switching at work. On PHP 8.1 or newer this prints "Hello PHP: Published" and then "Error: Cannot modify readonly property Post::$title". Select PHP 8.0 and run it again: the enum keyword is not recognised there, so you get a parse error instead.

    <?php
    enum Status: string
    {
        case Draft = 'draft';
        case Published = 'published';
    
        public function label(): string
        {
            return ucfirst($this->value);
        }
    }
    
    final class Post
    {
        public function __construct(
            public readonly string $title,
            public readonly Status $status = Status::Draft,
        ) {}
    }
    
    $post = new Post('Hello PHP', Status::from('published'));
    echo $post->title, ': ', $post->status->label(), PHP_EOL;
    
    try {
        $post->title = 'Changed';
    } catch (Error $e) {
        echo get_class($e), ': ', $e->getMessage(), PHP_EOL;
    }

    Filtering and summing arrays with arrow functions

    Typical data-shaping code: array_filter with an arrow function, array_column to pull one field, and json_encode for the result. The first line prints the exact PHP_VERSION of the build you picked, which is handy when you share a link and want the reader to know what produced the output.

    <?php
    echo 'Running PHP ', PHP_VERSION, PHP_EOL;
    
    $orders = [
        ['id' => 1, 'total' => 42.50, 'paid' => true],
        ['id' => 2, 'total' => 19.99, 'paid' => false],
        ['id' => 3, 'total' => 120.00, 'paid' => true],
    ];
    
    $paid = array_filter($orders, fn (array $order): bool => $order['paid']);
    $revenue = array_sum(array_column($paid, 'total'));
    
    printf('%d paid orders, revenue %.2f%s', count($paid), $revenue, PHP_EOL);
    echo json_encode(array_column($paid, 'id')), PHP_EOL;

    Frequently asked questions

    Is this a real PHP online compiler or a simulation?

    It is real PHP. The sandbox loads php-wasm 0.1.0 (WebAssembly), which builds the official PHP interpreter for WebAssembly, and runs your script inside your browser tab. Every line of output comes from that interpreter, and the toolbar shows the PHP_VERSION constant reported by the build you selected.

    Which PHP versions can I run online?

    You can run PHP 8.5, 8.4, 8.3, 8.2, 8.1 and 8.0; PHP 8.4 is selected by default. Each version is a separate WebAssembly build that is downloaded only when you choose it, so you can keep the same script in the editor, switch versions and compare the results between releases.

    Is my PHP code sent to a server?

    No. Execution happens in your browser, so your code is not uploaded to WebFiddle to be run. The PHP runtime files are fetched from the jsDelivr CDN, your draft is kept in local storage on your device, and the code only leaves it when you copy a share link and send it to someone.

    Can I connect to MySQL or install Composer packages?

    No. There is no database server behind the sandbox and the interpreter has no network sockets, so mysqli or PDO connections to a host fail and Composer cannot download packages. The sandbox suits language features, string and array logic, algorithms, and generating HTML.

    Why does my output look different in HTML Preview?

    HTML Preview renders whatever your script prints as markup, so tags become elements and plain line breaks collapse into spaces, as they would in a browser. Raw Output shows the exact text instead, which is the better view for print_r, var_dump, printf and anything that relies on PHP_EOL.

    Do variables and functions carry over between runs?

    No. The sandbox resets the PHP runtime before every run, so functions, classes, constants and globals from the previous run are gone. That matches a normal web request, which also starts from a clean state. Anything your script needs has to be defined in the code you are running.

    Embed this sandbox

    Paste this iframe into any web page and readers can run code without leaving it. The snippet travels in the URL, so there is nothing to host and no account to create. Open the sandbox, write your example, press Share, then append that link’s query string to the embed URL to preload it.

    <iframe src="https://webfiddle.xyz/embed/php" width="100%" height="480" loading="lazy"
            style="border:1px solid #ddd;border-radius:8px"
            title="WebFiddle php sandbox"></iframe>
    Preview the embed

    Related articles

    Related sandboxes

    Official documentation