PHP vs Swift
Swift is a natural choice when the project is a native Apple-platform app. PHP is a natural choice when the project is a web application or API backed by a database. A Swift client can call a PHP backend over HTTP and use the same API as a browser client.
| Concern | PHP | Swift |
|---|---|---|
| Common application | A website, API, CMS, or server-side script. | A native Apple-platform app, a command-line tool, or a server-side Swift service. |
| Missing value | A nullable type can hold a value or null. | An optional is an explicit type that can hold a value or nil. |
| Main package tool | Composer. | Swift Package Manager, also integrated into Xcode. |
| Interface layer | HTML rendered by PHP or JSON for a separate client. | SwiftUI or UIKit on supported Apple platforms. |
Show a fallback display name
Section titled “Show a fallback display name”<?phpdeclare(strict_types=1);
final class Profile{ public function __construct(public ?string $name) {}}
function displayName(?Profile $profile): string{ return $profile?->name ?? 'Guest';}
echo displayName(new Profile('Anika')), PHP_EOL;echo displayName(null), PHP_EOL;struct Profile { let name: String?}
func displayName(_ profile: Profile?) -> String { profile?.name ?? "Guest"}
print(displayName(Profile(name: "Anika")))print(displayName(nil))Both print Anika and then Guest. Use php example.php and swift example.swift. The examples use optional access and a fallback value. An empty string is still a value in both versions. If a profile name full of spaces should also become Guest, give that rule a separate test.
Connect an app and an API
Section titled “Connect an app and an API”When building both sides, start with a written JSON contract for a profile response. Define which fields may be null, which may be omitted, and what a client should display for each. Keep permissions and database rules on the server. Use Swift to decode the response and present the result in the app.