PHPPHP

PHP exercises with solutions

Solve in editor

Write your solution right in the browser.

Open exercises with editor

1. Even Numbers Beginner

Print all even numbers from 1 to 10 – each on its own line.

Expected output: 2 ⏎ 4 ⏎ 6 ⏎ 8 ⏎ 10

Note

Count in steps of two: for ($i = 2; $i <= 10; $i += 2). Or check with % 2 === 0.

Show sample solution
<?php
for ($i = 2; $i <= 10; $i += 2) {
    echo $i, "\n";
}

2. FizzBuzz (1–15) Beginner

Print the numbers 1 to 15, each on one line. For multiples of 3 print Fizz instead, for multiples of 5 Buzz, for both FizzBuzz.

Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz

Note

Check the "divisible by 15" case first. Or use match (true).

Show sample solution
<?php
for ($i = 1; $i <= 15; $i++) {
    echo match (true) {
        $i % 15 === 0 => "FizzBuzz",
        $i % 3 === 0 => "Fizz",
        $i % 5 === 0 => "Buzz",
        default => $i,
    }, "\n";
}

3. Reverse a Word Beginner

Reverse the string "Hello" and print it.

Expected output: olleH

Note

The function strrev() reverses a string.

Show sample solution
<?php
$wort = "Hello";
echo strrev($wort);

4. Sum of Squares Beginner

Calculate the sum of the squares of the numbers 1 to 10 and print it.

Expected output: 385

Note

range(1, 10) creates the array; array_map squares, array_sum adds up.

Show sample solution
<?php
echo array_sum(array_map(fn($x) => $x ** 2, range(1, 10)));

5. Palindrome Function Intermediate

Write a function ist_palindrom(string $text): bool that returns true if the text reads the same forwards and backwards (ignore upper/lower case). Print the result for "Anna" and "PHP8" with var_dump.

Expected output: bool(true) ⏎ bool(false)

Note

Convert with strtolower and compare with strrev of the result (===).

Show sample solution
<?php
function ist_palindrom(string $text): bool {
    $t = strtolower($text);
    return $t === strrev($t);
}

var_dump(ist_palindrom("Anna"));
var_dump(ist_palindrom("PHP8"));

6. Most Frequent Word Intermediate

Find the most frequent word in "a b a c b a" and print it.

Expected output: a

Note

array_count_values(explode(" ", $text)) counts; arsort sorts descending by value, array_key_first returns the first key.

Show sample solution
<?php
$text = "a b a c b a";
$zaehler = array_count_values(explode(" ", $text));
arsort($zaehler);
echo array_key_first($zaehler);

7. Rectangle Class Intermediate

Write a class Rechteck with constructor promotion (width and height as readonly float) and the method flaeche(): float. Print the area of a 3x4 rectangle.

Expected output: 12

Note

public function __construct(public readonly float $breite, public readonly float $hoehe) {}; the area is width times height.

Show sample solution
<?php
class Rechteck {
    public function __construct(
        public readonly float $breite,
        public readonly float $hoehe,
    ) {}

    public function flaeche(): float {
        return $this->breite * $this->hoehe;
    }
}

echo (new Rechteck(3, 4))->flaeche();

8. Fibonacci with a Generator Advanced

Write a generator fib(int $n) that yields the first $n Fibonacci numbers (0, 1, 1, 2, ...). Print the first 10 on one line, separated by spaces.

Expected output: 0 1 1 2 3 5 8 13 21 34

Note

Keep two variables $a = 0, $b = 1, yield $a; and swap with [$a, $b] = [$b, $a + $b]; in a loop.

Show sample solution
<?php
function fib(int $n): Generator {
    [$a, $b] = [0, 1];
    for ($i = 0; $i < $n; $i++) {
        yield $a;
        [$a, $b] = [$b, $a + $b];
    }
}

echo implode(" ", iterator_to_array(fib(10), false));