Tsallaka zuwa abun ciki
Justice Tech School

PHP Exercises

Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.

PHP Introduction

Exercise

Print text in PHP.

 "Hello";

Syntax and Output

Exercise

Which symbol ends a statement?

echo "Hi"

PHP Install and Run

Exercise

Start a local server.

php  localhost:8000

PHP Comments

Exercise

Start a one line comment.

 note

Variables

Exercise

Which symbol starts a variable name?

name = "Ada";

Data Types

Exercise

Show a value and its type.

(42);

Strings

Exercise

Join two strings.

echo "a"  "b";

Numbers and Math

Exercise

Round to 2 decimals.

(3.14159, 2)

Operators

Exercise

Use a default if the value is null.

$city = $input  "unknown";

echo, print and printf

Exercise

Format with placeholders.

("%d items", 3)

PHP Constants and Magic Constants

Exercise

Create a constant.

 MAX = 5;

PHP Type Juggling and Casting

Exercise

Cast to integer.

$n = () "5";

null, isset and empty

Exercise

Is the variable set?

($x)

More String Functions

Exercise

Count Unicode characters.

("héllo")

PHP Math and Random Numbers

Exercise

Secure random integer.

(1, 6)

PHP Tags and Statements

Exercise

Print a value.

 "hi";

Variables in Depth

Exercise

Variable prefix.

name = "Ada";

String Interpolation and Heredoc

Exercise

Which quotes read variables?

 quotes

Integers and Floats

Exercise

Integer division.

(7, 2)

Casting and Conversion

Exercise

Cast to int.

()"5"

== and ===

Exercise

Strict equal.

1  "1"

Null Coalescing

Exercise

Default value.

$x  "none"

Constants and Magic Constants

Exercise

Define a constant.

("A", 1);

Operators Summary

Exercise

Join strings.

"a"  "b"

If, Else and Match

Exercise

Add a second condition.

if ($a) {}  ($b) {}

Loops

Exercise

Walk through an array.

 ($items as $item) {}

Switch and Match In Depth

Exercise

End a switch case.

case 1: echo "a"; ;

break, continue and Alternative Syntax

Exercise

Skip to the next round.

if ($x) ;

Ternary, Spaceship and Null Safe

Exercise

Compare for sorting.

$a  $b

if, elseif and else

Exercise

Else branch.

if ($a) {}  {}

match Expression

Exercise

Fallback arm.

 => "x"

for and while Loops

Exercise

Loop keyword.

 ($i = 0; $i < 3; $i++) {}

foreach

Exercise

Key and value.

foreach ($a as $k  $v)

break and continue

Exercise

Skip a round.

;

Arrays

Exercise

Count the items.

echo ($fruits);

Array Functions

Exercise

Transform every item.

$r = (fn($n) => $n * 2, $nums);

Functions

Exercise

Send a value back.

function add($a, $b) {  $a + $b; }

Variable Scope

Exercise

Keep a value between calls.

function f() {  $n = 0; }

Sorting Arrays

Exercise

Sort keeping keys by value.

($arr);

Multidimensional Arrays

Exercise

Extract one column.

($rows, "n")

More Array Functions

Exercise

Remove duplicates.

($a)

Spread and Destructuring

Exercise

Spread an array.

sum($a)

Closures and Callables

Exercise

Capture an outer variable.

function ()  ($x) {}

Recursion and Generators

Exercise

Produce a value lazily.

 $i;

Arrays

Exercise

Add to the end.

$a = 5;

Adding and Removing Items

Exercise

Remove last.

($a)

Searching Arrays

Exercise

Check a value.

(2, $a)

array_map, array_filter, array_reduce

Exercise

Select items.

array_($a, $fn)

array_slice, merge and combine

Exercise

Join arrays.

array_([1], [2])

Unique, Count and Statistics

Exercise

Remove duplicates.

array_($a)

Sorting

Exercise

Sort by key.

($p)

implode, explode and str_split

Exercise

Join array to text.

("-", $a)

Functions

Exercise

Return a value.

function f() {  1; }

Named and Variadic Arguments

Exercise

Collect args.

function f($n) {}

Arrow Functions and Closures

Exercise

Short function.

$f = ($x) => $x * 2;

Variable Scope and static

Exercise

Keep between calls.

 $n = 0;

Type Declarations

Exercise

Return type.

function f():  { return 1; }

More String Functions

Exercise

Part of a string.

("abc", 1, 2)

Checking Strings

Exercise

Starts with.

str_with("hello", "he")

Formatting Numbers

Exercise

Thousands separator.

(1234.5, 2)

Dates

Exercise

Format a date.

("Y-m-d")

Math Functions

Exercise

Square root.

(16)

Classes and Objects

Exercise

Refer to the current object.

return ->name;

Inheritance and Interfaces

Exercise

Inherit from Animal.

class Dog  Animal {}

Static Members, Constants and Traits

Exercise

Reach a static member.

echo Counter$total;

Constructors, Readonly and Enums

Exercise

Set once.

public  int $id

Abstract Classes and Interfaces

Exercise

Class that cannot be instantiated.

 class Base {}

Magic Methods

Exercise

Convert an object to text.

public function () { }

Namespaces and Autoloading

Exercise

Import a class.

 App\Models\User;

Type Declarations and Union Types

Exercise

Nullable int.

function f(int $n) {}

Classes and Objects

Exercise

Create an object.

$d =  Dog("Rex");

public, protected, private

Exercise

Class only.

 $secret;

Inheritance

Exercise

Inherit from a class.

class B  A {}

Interfaces

Exercise

Use an interface.

class A  I {}

Static Members and Class Constants

Exercise

Class access operator.

Minc()

Enums

Exercise

Declare an enum.

 Status {}

readonly Properties

Exercise

Set once.

public  int $x

Exceptions

Exercise

Throw.

 new Exception("x");

Forms and Superglobals

Exercise

Make text safe to show in HTML.

echo ($name);

Sessions and Cookies

Exercise

Start a session.

();

JSON

Exercise

Turn an array into JSON.

echo ($data);

Files

Exercise

Read a whole file.

$text = ($path);

Databases with PDO

Exercise

Use a prepared statement.

$stmt = $pdo->("SELECT * FROM t WHERE id = ?");

Errors and Exceptions

Exercise

Raise an exception.

 new Exception("oops");

Dates and Regular Expressions

Exercise

Find all regex matches.

("/\d+/", $text, $m);

Form Handling and Validation

Exercise

Validate an email.

filter_var($e, FILTER_VALIDATE)

File Uploads

Exercise

Move an upload.

($tmp, $dest)

Passwords and Security

Exercise

Check a password.

($pw, $hash)

HTTP Requests with cURL

Exercise

Start a request.

("https://x.com")

Headers, Redirects and Status Codes

Exercise

Send a header.

("Location: /")

Regular Expressions In Depth

Exercise

Split with a regex.

("/,/", $s)

DateTime In Depth

Exercise

Immutable date class.

new ("now")

JSON

Exercise

Array from JSON.

json_decode($j, )

Superglobals

Exercise

Query data.

$["q"]

Escaping Output

Exercise

Escape HTML.

($s)

Validating Input with filter_var

Exercise

Validate an email.

FILTER_VALIDATE_

Reading and Writing Files

Exercise

Write a file.

($f, "x")

Regular Expressions

Exercise

Test a pattern.

("/a/", "cat")

Password Hashing

Exercise

Check a password.

("secret", $h)

Practice: Word Counter

Exercise

Split on space.

(" ", $text)

Practice: FizzBuzz

Exercise

Remainder.

$i  3 == 0

Practice: Simple Cart

Exercise

Pick a column.

array__($rows, "n")

Composer and Packages

Premium exercise. See plans

PDO Transactions and Fetch Modes

Premium exercise. See plans

Preventing SQL Injection

Premium exercise. See plans

Authentication Flow

Premium exercise. See plans

Testing with PHPUnit

Premium exercise. See plans

Performance and OPcache

Premium exercise. See plans

Building a REST API

Premium exercise. See plans

Laravel Overview

Premium exercise. See plans

WordPress and PHP

Premium exercise. See plans

Templates and Separation of Concerns

Premium exercise. See plans

Logging and Error Reporting

Premium exercise. See plans

PHP Best Practices

Premium exercise. See plans

Practice Projects

Exercise

Popular PHP guide site.

phptheright