define
define(string $constant_name, mixed $value): bool
Defines a named constant at runtime (complementing the const keyword).
C C23
#include <stdio.h>
#define APP_VERSION "1.0"
int main(void) {
printf("%s\n", APP_VERSION);
return 0;
}
1.0
C++ C++26
#include <iostream>
int main() {
constexpr const char *APP_VERSION = "1.0"; // constexpr: a compile-time constant
std::cout << APP_VERSION << "\n";
return 0;
}
1.0
C# v11.0.100
const string AppVersion = "1.0"; // const: compile-time, inlined at use sites
Console.WriteLine(AppVersion);
1.0
Go v1.27.4
package main
import "fmt"
const AppVersion = "1.0"
func main() {
fmt.Println(AppVersion)
}
1.0
Java v27.0.1
public class Main {
static final String APP_VERSION = "1.0";
public static void main(String[] args) {
System.out.println(APP_VERSION);
}
}
1.0
JavaScript v26.13.1
const APP_VERSION = "1.0"; // const forbids reassignment of the binding; object contents still mutate
console.log(APP_VERSION);
1.0
PHP v8.6.0
define("APP_VERSION", "1.0");
echo APP_VERSION, "\n", defined("APP_VERSION") ? "yes" : "no";
1.0 yes
Python v3.15.2
APP_VERSION = "1.0" # ALL_CAPS is a naming convention only; Python has no true constants
print(APP_VERSION)
1.0
Rust v1.99.1
const APP_VERSION: &str = "1.0";
fn main() {
println!("{}", APP_VERSION);
}
1.0
TypeScript v26.13.1
const APP_VERSION = "1.0"; // const binding cannot be reassigned; literal type is inferred
console.log(APP_VERSION);
1.0
Language differences
C: #define is a preprocessor macro (textual substitution, no type, no scope); a const variable buys type safety (the example uses the macro form to mirror define's global visibility).
C++: the modern choice is constexpr (typed, scoped, compile-time evaluated, as in the example); #define still works but is considered legacy.
C#: const declares a compile-time constant inlined at use sites (example) — beware across assemblies on redeployment; runtime constants use static readonly.
Go: const declares compile-time constants (example); untyped constants with implicit conversion are a Go-specific trait; only basic types qualify.
Java: no global constants — the convention is static final fields (example); the compiler inlines primitive/string constants at use sites.
JavaScript: const is a lexical binding constraint (no reassignment), not an immutable value — object contents can still mutate (see the example comment); unlike PHP's define there is block scope instead of global visibility.
PHP: define creates constants at runtime — allowed inside conditionals, names can be composed from variables; the const keyword requires compile time (top level or inside classes); constants are global and cannot be redefined.
Python: no true constants — the ALL_CAPS convention is only a convention, reassignment still works (see the example comment); genuine read-only needs types.MappingProxyType or module-level __getattr__ interception.
Rust: const is a compile-time constant (type annotation mandatory, example); static is the fixed-address counterpart; both are typed and scoped.
TypeScript: same const semantics as JavaScript, plus literal-type inference at the type level (see the example comment); deep immutability via as const.