Online PHP Compiler

Write, run, and debug PHP code online in a full-featured browser IDE. Work with multiple files and packages, save your workspace, and share or collaborate in real time.

🚀 323,464 total executions (208 this month)
Embed
Output

Practice Python with coding challenges

Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.

All Python challenges
Easy

Rectangle Perimeter

Compute the perimeter of a rectangle from its sides.

You'll learn ArithmeticReturn values
BeginnerLearning-pathVariables
Easy

Days in a Month

Return how many days a month has, including leap years.

You'll learn Calendar rulesHelper functions
BeginnerConditionsFunctionsLearning-path
Easy

Dot Product of Two Vectors

Multiply two vectors element by element and add the results: the core operation of every neuron.

You'll learn VectorsDot product
Neural networksaimachine learning
Easy

Distance Between Points

Compute the straight-line distance between two points.

You'll learn Pythagorean theoremSquare roots
BeginnerFunctionsLearning-pathmath
Easy

Average of Three

Average three whole numbers.

You'll learn ArithmeticDivision
BeginnerLearning-pathVariables
Easy

To-Do List

Keep a list of tasks: add them, mark them done, remove them and report what is still pending.

You'll learn Typed collectionsRecords
BeginnerSimulationStrings
Easy

Prime Number Check

Decide whether a number is prime.

You'll learn DivisibilityLoop optimization
BeginnerLoopsNumbersStudent-beginner
Hard

Word Break

Decide whether text can be split into dictionary words.

You'll learn Prefix DPString segmentation
AdvancedDynamic-programmingHackathonStrings
Easy

Count Pairs With a Sum

Count pairs of positions whose values add up to a target.

You'll learn Pairs of indexesAvoiding double counts
ArraysBeginnerLearning-pathNested-loops
Easy

Missing Number

Find the one number missing from 0..n.

You'll learn Arithmetic seriesSum difference trick
ArraysBeginnerStudent-beginnermath
Medium

K-th Smallest Element

Find the k-th smallest value without fully sorting.

You'll learn QuickselectPartitioning
AdvancedCompetitive-programmingSorting
Easy

Min and Max Together

Find the smallest and largest value in one pass.

You'll learn Single passMultiple return values
ArraysBeginnerFunctionsLearning-path

🐘 About This PHP Online Executor

The CodeUtility PHP Executor lets you write and run PHP code directly in your browser - no setup, server, or installation required. It runs real PHP environments in isolated sandboxes, supporting versions from 5.6 up to the latest 8.2.

Whether you're testing snippets, learning PHP basics, or exploring new syntax and library features, this tool provides a lightweight and interactive environment to execute your PHP code instantly.

You can quickly check logic, debug functions, or practice for interviews - all within your browser, with real-time output displayed in the built-in console.

It’s ideal for students, developers, and web engineers who need a quick way to experiment with PHP or validate snippets before deploying them to a real web server.

More than a code runner

Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.

How to use this executor?

Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.

  1. Select a version and edit the current file or open another workspace file.
  2. Run the code, provide input in the terminal when requested, and review saved run history.
  3. Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.

💡 PHP Basics & Examples You Can Try Above

1. Declaring Variables and Constants

PHP variables start with $. Use define() or const for constants.

$x = 10;
$pi = 3.14;
$name = "Alice";
$isActive = true;

define("MAX_USERS", 100);
const APP_NAME = "CodeUtility";

2. Conditionals (if / switch)

Use if, elseif, and else for branching, or switch for multiple cases.

$x = 2;
if ($x == 1) {
    echo "One\n";
} elseif ($x == 2) {
    echo "Two\n";
} else {
    echo "Other\n";
}

switch ($x) {
    case 1:
        echo "One\n";
        break;
    case 2:
        echo "Two\n";
        break;
    default:
        echo "Other\n";
}

3. Loops

PHP supports for, while, and foreach loops.

for ($i = 0; $i < 3; $i++) {
    echo $i . "\n";
}

$n = 3;
while ($n > 0) {
    echo $n . "\n";
    $n--;
}

4. Arrays

Arrays hold multiple values. PHP supports indexed and associative arrays.

$nums = array(10, 20, 30);
echo $nums[1];

5. Array Manipulation

Use functions like array_push(), array_pop(), count(), and slicing with array_slice().

$fruits = ["apple", "banana"];
array_push($fruits, "cherry");
array_pop($fruits);
print_r($fruits);

$sliced = array_slice($fruits, 0, 1);
print_r($sliced);

6. Console Input/Output

Use readline() for CLI input and echo/print for output.

$name = readline("Enter your name: ");
echo "Hello, $name\n";

7. Functions

Functions encapsulate reusable logic. Parameters can have default values.

function greet($name = "Guest") {
    return "Hello, $name";
}

echo greet("Alice");

8. Associative Arrays

PHP arrays can act like dictionaries with string keys.

$person = ["name" => "Bob", "age" => 25];
echo $person["name"];

9. Exception Handling

Use try, catch, and throw to handle errors.

try {
    throw new Exception("Something went wrong");
} catch (Exception $e) {
    echo $e->getMessage();
}

10. File I/O

Use functions like fopen(), fwrite(), and fread() for file operations.

$file = fopen("test.txt", "w");
fwrite($file, "Hello File");
fclose($file);

$file = fopen("test.txt", "r");
echo fread($file, filesize("test.txt"));
fclose($file);

11. String Manipulation

Use functions like strlen(), str_replace(), and explode().

$text = "Hello World";
echo strlen($text);
echo str_replace("Hello", "Hi", $text);
print_r(explode(" ", $text));

12. Classes & Objects

Use classes to model real-world objects and reuse logic.

class Person {
  public $name;
  function __construct($name) {
    $this->name = $name;
  }
  function greet() {
    return "Hi, I'm " . $this->name;
  }
}

$p = new Person("Alice");
echo $p->greet();

13. References

In PHP, you can pass variables by reference using &.

function addOne(&$num) {
    $num++;
}

$x = 5;
addOne($x);
echo $x; // 6