Swift for PHP Developers
Move from PHP to Swift through static types, value semantics, optionals, Unicode, protocols and ARC. Complete 48 multi-file exercises, build a parcel desk and port two complete PHP programs.
PHP and Swift side by side
The same program in PHP and in Swift, from the lessons of this course.
Bindings and Mutation
Ordinary PHP variables can hold values of different types over time. Reassigning label from text to an integer does not change saved. PHP parameter and property declarations can impose separate type constraints, and strict_types affects scalar call coercions…
PHP1<?php2declare(strict_types=1);3$label = "draft";4$saved = $label;5$label = 17;6echo "$saved $label\n";Swift1let saved = "draft"2var label = saved3label = "ready"4print(saved, label)Strings and Interpolation
PHP double-quoted strings interpolate variables, and concatenation uses a dot. Complex expressions are often formatted or combined explicitly, as the arithmetic output here shows. PHP strings are byte sequences; str_starts_with checks the supplied prefix…
PHP1<?php2declare(strict_types=1);3$done = 3;4echo "progress=", $done * 25, "%\n";5echo str_starts_with("report.csv", "report") ? "true\n" : "false\n";Swift1let done = 32print("progress=\(done * 25)%")3print("report.csv".hasPrefix("report"))Switch and Patterns
PHP match is an expression that uses strict comparisons and returns a selected value. Multiple conditions can share an arm, and an unhandled value raises UnhandledMatchError if there is no default. It differs from PHP switch, whose comparison and fallthrough…
PHP1<?php2declare(strict_types=1);3$command = "pause";4echo match ($command) {5 "pause", "stop" => "idle",6 default => "running"7}, "\n";Swift1let command = "pause"2switch command {3case "pause", "stop": print("idle")4default: print("running")5}Functions and Labels
PHP supports named arguments using the declared parameter names, so changing a public parameter name can affect callers. Positional arguments may precede named ones. A Swift external label has a similar readability role but can differ from the local parameter…
PHP1<?php2declare(strict_types=1);3function echo_sound(string $sound, int $times): string {4 return str_repeat($sound, $times);5}6echo echo_sound("ha", times: 3), "\n";Swift1func echo(_ sound: String, times count: Int) -> String {2 return String(repeating: sound, count: count)3}Dictionaries and Missing Keys
PHP arrays are ordered maps. unset removes a key, while assigning null keeps an entry that array_key_exists can still find. The ?? fallback treats both a missing key and a null value as absent for that expression. Count includes present null entries, so…
PHP1<?php2declare(strict_types=1);3$settings = ["font" => 14, "gap" => 2];4unset($settings["gap"]);5echo $settings["gap"] ?? 8, "\n";6echo count($settings), "\n";Swift1var settings = ["font": 14, "gap": 2]2settings["gap"] = nil3print(settings["gap"] ?? 8)4print(settings.count)Structs and Independent Copies
PHP object assignment copies the object identifier, so first and second refer to the same Heading instance. This differs from ordinary array assignment. clone creates a separate object but is shallow unless the class customizes its cloning behavior for…
PHP1<?php2declare(strict_types=1);3class Heading {4 public function __construct(public string $direction) {}5}6$first = new Heading("north");7$second = $first;8$second->direction = "west";9echo "$first->direction $second->direction\n";Swift1struct Heading {2 var direction: String3 mutating func turnWest() { direction = "west" }4}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsPHP Ports
2 lessonsFrequently asked questions
Who is Swift for PHP Developers for?
For people who already write PHP and want to learn Swift. Each lesson starts from PHP you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need PHP basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for PHP Developers have?
Swift for PHP Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for PHP Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for PHP Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.