Skip to main content

Online PHP Compiler

index.php
Ready

About PHP

PHP is a server-side scripting language behind a large share of websites, including WordPress and many Laravel apps.

How to take input in PHP

fgets(STDIN) reads one line, including its line break, so wrap it in trim().

$n = (int) trim(fgets(STDIN));
$nums = array_map('intval', explode(' ', trim(fgets(STDIN))));

PHP programs to try

Copy one into the editor above, put any input in the Input tab and press Run.

Associative arrays

<?php
$marks = ["Asha" => 91, "Ravi" => 86];
arsort($marks);
foreach ($marks as $name => $m) {
    echo "$name: $m\n";
}

String functions

<?php
$s = "online php compiler";
echo ucwords($s) . "\n";
echo strrev($s) . "\n";
echo str_word_count($s) . "\n";

A function

<?php
function factorial(int $n): int {
    return $n <= 1 ? 1 : $n * factorial($n - 1);
}
echo factorial(6) . "\n";

Common PHP errors and how to fix them

PHP Parse error: syntax error, unexpected ...
A semicolon, bracket or quote is missing near the line in the message.
PHP Warning: Undefined variable
The variable is misspelled or used before it's set. Remember the $ in front of every variable name.
Code is printed instead of run
Start the file with <?php.

Frequently asked questions

Is this online PHP compiler free?

Yes. Write and run PHP code in your browser for free, with nothing to install and no setup.

How do I give input to my PHP script?

Type it in the Input tab before pressing Run and read each line with trim(fgets(STDIN)).

Can I run Laravel or a website here?

This page runs single PHP scripts from the command line. Frameworks and web pages need a full project.

Does it work on my phone?

Yes. The editor, input and output work in a mobile browser, so you can practise PHP without a laptop.