Miscellaneous

21 entries

cli_set_process_title / cli_get_process_title Function CLI process title read/write: cli_set_process_title rewrites the process name ps/top displays (giving each worker a recognizable identity), cli_get_process_title reads it back; the demo follows the "set, then set back" discipline — no residue left on the host process. connection_status Function Returns the connection status bitmask: CONNECTION_NORMAL(0)/CONNECTION_ABORTED(1)/CONNECTION_TIMEOUT(2), combinable bitwise (ABORTED|TIMEOUT=3). In Web scenarios it detects client disconnects; CLI has no HTTP client connection, so it is always CONNECTION_NORMAL - an honest environment-independent demo. Pairs with ignore_user_abort() as the connection-handling teaching closure. Time\Duration Function New in PHP 8.6: a nanosecond-precision duration — attached to no instant, no timezone, comparable and arithmetic-friendly; upgrades the bare millisecond numbers of sleep/usleep/time_nanosleep into a typed duration. eval Function A language construct that executes a string as PHP code: literal-expression evaluation is deterministic; feeding user input into eval is arbitrary code execution — the core risk lesson of this entry. gc_collect_cycles Function Forces collection of cyclic garbage and returns the number of objects collected. PHP memory management is refcounting plus periodic cycle detection; this function triggers one detection-and-sweep round immediately. getopt Function CLI option parsing: a short-option string (colon = takes a value) plus a long-option table (trailing '=' = takes a value), returning an option-to-value map. Its modern successors are frameworks like argparse/flag/clap. getrusage Function Per-process resource usage: user/system CPU time (ru_utime/ru_stime), peak resident memory (ru_maxrss) and 30+ fields; defaults to this process, RUSAGE_CHILDREN sums reaped children. highlight_string Function Built-in PHP syntax highlighting: highlight_string/highlight_file return <span>-colored HTML directly, no extension or third-party library needed — the only runtime-builtin highlighter among the ten languages. ignore_user_abort Function Read/set the "keep running after client abort" switch: null reads the current value, a bool sets it and returns the previous one (int 0/1); pairs with connection_status' bitmask — the status tells you whether the client left, this switch decides whether you keep going; CLI has no client, so get/set is pure switch read/write. mt_srand / srand Function Seeds the global Mt19937 engine (srand is its alias since 7.1): a fixed seed gives a fully REPRODUCIBLE sequence; the algorithm is pinned since 7.1, so the numbers are stable across versions and platforms. pack / unpack Function Binary data packing and unpacking: format letters pin both type and byte order (n/N big-endian, v/V little-endian, d/f floats, a/A strings) - the low-level tool for wire protocols and binary files. Random\Randomizer Function OOP randomness facade (PHP 8.2+): inject a seedable engine (e.g. Mt19937) at construction and every sequence from getInt/shuffleArray becomes exactly reproducible; without a seed it falls back to the cryptographically secure engine. register_shutdown_function Function Registers a callback to run after script execution finishes (or exit() is called), fired in registration order. register_tick_function Function Fires a callback every N low-level statements inside declare(ticks=N) - a PHP-only per-statement hook with no counterpart in other mainstream languages (the other entries here are hand-instrumented contrasts). serialize / unserialize Function Turns any PHP value into a portable string and back. The core security lesson: unserialize on untrusted input is object injection (triggering __wakeup/__destruct attack chains) - the allowed_classes whitelist is the only safe usage. sleep Function Pauses the script for a number of seconds (blocking the current execution flow). sys_getloadavg Function System load-average introspection: returns [1min, 5min, 15min] samples (Unix load = exponentially damped average of runnable plus uninterruptible tasks), false on failure; complements getrusage (process-level) — this one is machine-level; values float with machine state, so only the shape can be asserted. time_nanosleep / time_sleep_until Function The sleep family closure: time_nanosleep(seconds, nanoseconds) takes dual-integer args for precise delay (verified against the current php.net manual as NOT deprecated - a live page from PHP 5 through 8); a signal interrupt returns an array of remaining seconds/nanoseconds, so compare strictly with === true; time_sleep_until(timestamp) sleeps until a fixed moment, returning false with an E_WARNING for a past timestamp. Together with the sleep/usleep/hrtime family it forms a complete system. token_get_all Function Source tokenization: token_get_all splits PHP source into a token stream (T_ECHO/T_VARIABLE, etc.) using the Zend engine lexical scanner — the foundation of static analysis and code tooling. uniqid Function Generates a unique ID based on the microsecond timestamp (with optional prefix and extra entropy). usleep Function Delays script execution in microseconds (sub-second delays).