# PHP vs Python

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

> Compare PHP and Python for web apps, automation and data work. Run the same calculation in both languages and see when each fits.

PHP fits a website or API where handling web requests is the bulk of the work. Python fits a project where data analysis, automation or machine learning drives what you build. Both can serve complete web applications, so the right choice often comes down to the libraries and deployment you already use.

| Decision | PHP | Python |
| --- | --- | --- |
| Web applications | Use plain PHP, Laravel or Symfony. A common production setup runs PHP behind a web server with PHP-FPM. | Use Django for an integrated framework or a smaller framework such as Flask. Deploy through a Python application server. |
| Collections | An array can act as an ordered list or an ordered map. | A list stores a sequence and a dictionary maps keys to values. |
| Types | Parameter and return declarations are checked when values cross those boundaries. | Type hints support editors and static checkers. Python itself generally assigns no runtime enforcement to annotations. |
| Dependencies | Composer resolves dependencies and supplies an autoloader. | Python projects commonly use virtual environments with a packaging tool such as pip. |

## Sum the paid entries

Both programs receive the same two paid invoice amounts and one unpaid amount. They total the paid amounts in integer cents so rounding plays no part in the comparison.

```php
<?php
$invoices = [
    ['cents' => 1250, 'paid' => true],
    ['cents' => 900, 'paid' => false],
    ['cents' => 1200, 'paid' => true],
];

$total = 0;
foreach ($invoices as $invoice) {
    if ($invoice['paid']) {
        $total += $invoice['cents'];
    }
}
echo $total, PHP_EOL;
```

```python
invoices = [
    {"cents": 1250, "paid": True},
    {"cents": 900, "paid": False},
    {"cents": 1200, "paid": True},
]

print(sum(invoice["cents"] for invoice in invoices if invoice["paid"]))
```

Run the files with `php example.php` and `python3 example.py`. Both print `2450`. The Python generator feeds one matching value at a time to `sum`. PHP’s loop gives you a convenient place to inspect each row when you need to debug an import.

## Choosing for a real project

For a small business website with forms, accounts and admin screens, compare the full Laravel and Django workflows before choosing. For an analytics job that already depends on pandas, keep the calculation in Python and publish its results to the web app through a file or API. A project can use PHP for the website and Python for a scheduled job without asking the team to maintain two web stacks.

- [PHP and Laravel for Python developers](/series/python-developers-love-php-laravel/)
- [Python data structures](https://docs.python.org/3/tutorial/datastructures.html)
- [PHP arrays](https://www.php.net/manual/en/language.types.array.php)
- [PHP vs Ruby](/compare/php-vs-ruby/) · [All PHP comparisons](/compare/)
