← All docs

Conditional Compilation

ifdef blocks for compile-time feature flags and platform-specific code.

ifdef selectively includes or excludes code based on compile-time symbols.

Syntax

<?php
ifdef DEBUG {
    echo "debug mode\n";
} else {
    echo "release mode\n";
}

How it works

  • Symbols set via --define CLI flags
  • Resolved before include resolution and type checking
  • Inactive branch completely removed from AST
  • Inactive branches can reference files that don’t exist

Use cases

<?php
ifdef USE_SDL {
    extern "SDL2" {
        function SDL_Init(int $flags): int;
    }
    SDL_Init(0x20);
}

ifdef DEBUG {
    if ($hp < 0) {
        echo "BUG: negative HP!\n";
        exit(1);
    }
}

CLI usage

elephc --define DEBUG app.php
elephc --define DEBUG --define USE_SDL app.php

Nesting

<?php
ifdef PLATFORM_MAC {
    ifdef USE_METAL {
        echo "Metal renderer\n";
    } else {
        echo "OpenGL renderer\n";
    }
}

Constraints

  • Symbols are simple names (no expressions, no ifndef, no #if)
  • Symbols come only from --define flags
  • ifdef is not PHP syntax

CLI flags reference

FlagDescription
--target TARGETCompile for macos-aarch64, linux-aarch64, or linux-x86_64 instead of auto-detecting the host target
--heap-size=BYTESSet heap buffer size (default 8MB, min 64KB)
--gc-statsPrint GC allocation/free statistics at exit
--heap-debugEnable runtime heap verification (slow)
--emit-asmWrite the generated assembly and skip assemble/link
--checkRun the front-end pipeline without producing assembly or a binary
--timingsPrint per-phase compiler timings to stderr
--source-mapWrite a sidecar .map file next to generated assembly
--define SYMBOLDefine compile-time symbol for ifdef
--link LIB / -lLIBLink an extra native library
--link-path DIR / -LDIRAdd an extra native library search path
--framework NAMELink a macOS framework