# PHP vs Swift

[Original page](https://codewithphp.com/compare/php-vs-swift/)

> Compare PHP and Swift for web backends and Apple-platform apps. See how both handle a missing profile value and how they work together.

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

```php
<?php
declare(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;
```

```swift
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

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.

- [Swift language basics](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/)
- [PHP type declarations](https://www.php.net/manual/en/language.types.declarations.php)
- [Build a CRM API with PHP and Laravel](/series/build-crm-laravel-12/chapters/20-building-restful-api-crm/)
- [PHP vs Dart](/compare/php-vs-dart/) · [All PHP comparisons](/compare/)
