assert

assert(mixed $assertion, Throwable|string|null $description = null): bool

Asserts an expectation that should always hold; failure throws AssertionError; only zend.assertions=1 executes it, while -1 (recommended for production) compiles it away at zero cost.

C C23

C23
#include <assert.h>
#include <stdio.h>

int main(void) {
    assert(1 + 1 == 2); /* compiled out entirely when NDEBUG is defined */
    printf("still running\n");
    return 0;
}
Output
still running

C++ C++26

C++26
#include <cassert>
#include <iostream>

int main() {
    assert(1 + 1 == 2); // vanishes with NDEBUG
    static_assert(2 > 1, "compile-time assertion"); // always checked, zero runtime cost
    std::cout << "still running" << std::endl;
    return 0;
}
Output
still running

C# v11.0.100

v11.0.100
// C#: Debug.Assert vanishes in Release builds; Trace.Assert is always compiled in
System.Diagnostics.Debug.Assert(1 + 1 == 2);
System.Diagnostics.Trace.Assert(1 + 1 == 2);
Console.WriteLine("still running");
Output
still running

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// Go: no assert keyword - the language position is explicit error handling
	if 1+1 != 2 {
		panic("math broke") // the closest thing, and it is always "on"
	}
	fmt.Println("still running")
}
Output
still running

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // Java: assert is disabled by default - needs the -ea flag to even run
        assert 1 + 1 == 2;
        System.out.println("still running");
    }
}
Output
still running

JavaScript v26.13.1

v26.13.1
// JS: console.assert only logs in dev tools; Node's assert.ok throws AssertionError
const assert = require("node:assert");
assert.ok(1 + 1 === 2);
console.log("still running");
Output
still running

PHP v8.6.0

v8.6.0
// assert: an expectation that can vanish in production (zend.assertions=-1 compiles it away)
var_dump(assert(1 + 1 === 2)); // true whether the check runs or is compiled away
echo "still running\n";
Output
bool(true)
still running

Python v3.15.2

v3.15.2
# Python: assert is stripped entirely when run with -O
assert 1 + 1 == 2
print("still running")
Output
still running

Rust v1.99.1

v1.99.1
fn main() {
    // Rust: assert! is always on; debug_assert! compiles out in release (-O here)
    assert_eq!(1 + 1, 2);
    debug_assert!(1 + 1 == 2);
    println!("still running");
}
Output
still running

TypeScript v26.13.1

v26.13.1
// TS: same as JS - assert.ok from node:assert
import assert from "node:assert";
assert.ok(1 + 1 === 2);
console.log("still running");
Output
still running

Language differences

C: assert(3) - define NDEBUG and the whole statement vanishes; the original semantics.

C++: same as C, plus static_assert for compile-time assertions (always on, zero runtime cost).

C#: Debug.Assert carries [Conditional("DEBUG")] and vanishes in Release builds; Trace.Assert is always compiled in; on servers failures go to the log.

Go: NO assert - the language position is explicit error handling; the closest thing, panic, is always "on".

Java: assert is off by default and needs -ea to run - the same "vanishing" semantics as PHP's zend.assertions.

JavaScript: console.assert only logs in DevTools without interrupting; Node's assert.ok() throws AssertionError (used in the demo).

PHP: use zend.assertions=-1 in production (compiled away); NEVER use assert for input validation - the check ceases to exist when it is disabled; failure throws AssertionError (an Error); string assertions were removed in PHP 8.0 (they used eval - a security hazard); all assert.* ini settings are deprecated as of 8.3.

Python: the assert statement is stripped entirely under -O - never use it for data validation.

Rust: assert!/assert_eq! are always on (release included); debug_assert! exists only in debug builds - this entry is compiled with rustc -O, where it is already gone.

TypeScript: same as JavaScript.