---
title: "assert() vs throw"
canonical_url: "https://handbook.propay.co.za/php/assert-vs-throw"
---
# assert() vs throw

Assertions should only be used to verify conditions that should be logically impossible to be false. These conditions should only be based on inputs generated by your own code. Any checks based on external inputs should use exceptions.

The best use case for `assert()` is to treat it as a docblock that specifies types (great for nullables and polymorphic types). `assert()` may be disabled in production, so please do not rely on it at runtime; prefer exceptions if you need to check an expression at runtime.

Make sure you add a description to `assert` so that it's easy to reason about the code when a failure occurs.

From [official documentation](https://www.php.net/manual/en/function.assert.php):

> Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be true and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.

- [assert on php.net](https://www.php.net/manual/en/function.assert.php)
- [Should I be using assert in my PHP code?](https://stackoverflow.com/questions/4516419/should-i-be-using-assert-in-my-php-code)
- [Assertions and assertion libraries](https://matthiasnoback.nl/2018/09/assertions-and-assertion-libraries/)


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
