# PHP vs Scala

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

> Compare PHP and Scala for web applications, JVM data work and typed workflows. See a PHP enum and Scala 3 pattern match side by side.

Use PHP when the primary job is serving a web product that fits its application ecosystem. Scala is worth considering for a JVM codebase that already uses its libraries or for teams that want to model transformations with immutable collections and patterns. A PHP site can display the results of a Scala data pipeline over an API or from shared storage.

| Concern | PHP | Scala |
| --- | --- | --- |
| Typical deployment | PHP runtime with the extensions and Composer packages the application needs. | Compiled code targeting a JVM in a typical server deployment. |
| Data modeling | Classes, typed properties and enums can define domain values. | Case classes and enums can represent alternatives and their associated data. |
| Branching | `match` evaluates a value using strict comparisons. | Pattern matching can inspect the structure and values inside a case. |
| Processing model | Often web requests and separately managed background jobs. | Depends on the framework and may include long-lived services or batch processing. |

## Give each order state a label

This example needs PHP 8.1 or later for enums and Scala 3 for its enum syntax. Save the Scala version as `Example.scala` and run with `scala run Example.scala`. Both programs print `Ready to ship`.

```php
<?php
declare(strict_types=1);

enum OrderStatus
{
    case Received;
    case Packed;
}

function label(OrderStatus $status): string
{
    return match ($status) {
        OrderStatus::Received => 'Order received',
        OrderStatus::Packed => 'Ready to ship',
    };
}

echo label(OrderStatus::Packed), PHP_EOL;
```

```scala
enum OrderStatus:
  case Received, Packed

def label(status: OrderStatus): String =
  status match
    case OrderStatus.Received => "Order received"
    case OrderStatus.Packed => "Ready to ship"

@main def example(): Unit =
  println(label(OrderStatus.Packed))
```

When you add a case, Scala can flag an incomplete match during compilation. An unhandled PHP `match` throws `UnhandledMatchError` if that value reaches it. Testing every enum case helps catch that gap in a PHP project.

## Define the data handoff

If Scala prepares summaries for a PHP dashboard, agree on the result schema, when it refreshes and what the dashboard should display when a batch fails. A timestamp next to the summary can tell a reader how current the data is while background processing is retried.

- [Scala 3 algebraic data types](https://docs.scala-lang.org/scala3/book/types-adts-gadts.html)
- [PHP enumerations](https://www.php.net/manual/en/language.enumerations.php)
- [PHP vs Java](/compare/php-vs-java/) · [PHP vs Elixir](/compare/php-vs-elixir/) · [All PHP comparisons](/compare/)
