register_tick_function

register_tick_function(callable $callback, mixed ...$args): bool

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).

C C23

C23
#include <stdio.h>

/* PHP fires a callback per statement (declare(ticks)); C's closest honest
   equivalent is compile-time instrumentation via a counter macro */
static int tick_count = 0;
#define STEP (tick_count++)

int main(void) {
    int a = STEP;
    int b = STEP;
    int c = STEP;
    printf("statements instrumented: %d\n", 3);
    printf("macro fired per statement: %s\n", tick_count == 3 ? "true" : "false");
    printf("C has no tick hook - the macro is manual instrumentation\n");
    return 0;
}
Output
statements instrumented: 3
macro fired per statement: true
C has no tick hook - the macro is manual instrumentation

C++ C++26

C++26
#include <iostream>

// PHP fires a callback per statement; C++ has no such hook - a counting
// wrapper is the honest approximation
static int tick_count = 0;

static void step(void (*f)()) {
    tick_count++;
    f();
}

int main() {
    step([] {});
    step([] {});
    step([] {});
    std::cout << "callback fired per call: " << (tick_count == 3 ? "true" : "false") << "\n";
    std::cout << "C++ has no tick hook - this is manual instrumentation\n";
    return 0;
}
Output
callback fired per call: true
C++ has no tick hook - this is manual instrumentation

C# v11.0.100

v11.0.100
// PHP has a per-statement tick hook; C# has none (Profiler APIs are
// tooling-grade) - manual instrumentation is the honest approximation
int count = 0;
void Step(Action f) { count++; f(); }
Step(() => { });
Step(() => { });
Step(() => { });
Console.WriteLine("callback fired per call: " + (count == 3));
Console.WriteLine("C# has no tick hook - this is manual instrumentation");
Output
callback fired per call: True
C# has no tick hook - this is manual instrumentation

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// PHP has a per-statement tick hook; Go has none - manual
	// instrumentation with a counting wrapper is the honest approximation
	tickCount := 0
	step := func(fn func()) {
		tickCount++
		fn()
	}
	step(func() {})
	step(func() {})
	step(func() {})
	fmt.Println("callback fired per call:", tickCount == 3)
	fmt.Println("Go has no tick hook - this is manual instrumentation")
}
Output
callback fired per call: true
Go has no tick hook - this is manual instrumentation

Java v27.0.1

v27.0.1
public class Main {
    static int count = 0;

    static void step(Runnable f) {
        count++;
        f.run();
    }

    public static void main(String[] args) {
        // PHP has a per-statement tick hook; Java has none (java.lang.instrument
        // is agent-grade tooling) - manual instrumentation is the honest approximation
        step(() -> {});
        step(() -> {});
        step(() -> {});
        System.out.println("callback fired per call: " + (count == 3));
        System.out.println("Java has no per-statement hook - this is manual instrumentation");
    }
}
Output
callback fired per call: true
Java has no per-statement hook - this is manual instrumentation

JavaScript v26.13.1

v26.13.1
// PHP has a per-statement tick hook; JS has none - manual
// instrumentation with a counting wrapper is the honest approximation
let count = 0;
const step = (fn) => {
  count++;
  fn();
};
step(() => {});
step(() => {});
step(() => {});
console.log("callback fired per call:", count === 3);
console.log("JS has no tick hook - this is manual instrumentation");
Output
callback fired per call: true
JS has no tick hook - this is manual instrumentation

PHP v8.6.0

v8.6.0
// register_tick_function: a callback fired every N statements inside declare(ticks=N)
// PHP-ONLY hook: no mainstream language fires a callback per statement
declare(ticks=1);
$count = 0;
register_tick_function(function () use (&$count) { $count++; });
// Each simple statement below advances the tick counter (ticks=1)
$a = 1;
$b = 2;
$c = 3;
// The exact count depends on opcode structure, not statement count alone
printf("3 statements; callback fired %d times\n", $count);
var_dump($count >= 1);
Output
3 statements; callback fired 1 times
bool(true)

Python v3.15.2

v3.15.2
import sys

# PHP fires a callback per statement inside declare(ticks); Python's closest
# hook is sys.settrace - a debugging-only line event tracer (new frames only)
count = 0


def tracer(frame, event, arg):
    global count
    if event == "line":
        count += 1
    return tracer


def work():
    a = 1
    b = 2
    c = 3


sys.settrace(tracer)
work()  # a fresh frame - settrace hooks fire per line inside it
sys.settrace(None)
print("line events fired (>= 3 statements):", count >= 3)
print("Python has no tick hook - settrace is debugging-only tooling")
Output
line events fired (>= 3 statements): True
Python has no tick hook - settrace is debugging-only tooling

Rust v1.99.1

v1.99.1
fn main() {
    // PHP has a per-statement tick hook; Rust has none - manual
    // instrumentation with a counting closure is the honest approximation
    let mut count = 0;
    let mut step = |f: fn()| {
        count += 1;
        f();
    };
    step(|| {});
    step(|| {});
    step(|| {});
    println!("callback fired per call: {}", count == 3);
    println!("Rust has no tick hook - this is manual instrumentation");
}
Output
callback fired per call: true
Rust has no tick hook - this is manual instrumentation

TypeScript v26.13.1

v26.13.1
// PHP has a per-statement tick hook; TS has none - manual
// instrumentation with a counting wrapper is the honest approximation
let count = 0;
const step = (fn: () => void): void => {
  count++;
  fn();
};
step(() => {});
step(() => {});
step(() => {});
console.log("callback fired per call:", count === 3);
console.log("TS has no tick hook - this is manual instrumentation");
Output
callback fired per call: true
TS has no tick hook - this is manual instrumentation

Language differences

C: no per-statement hook at all - the most honest approximation is compile-time macro instrumentation (#define STEP (count++), example); the only runtime hook is a debugger.

C++: same as C, no hook - a counting wrapper function (example) or gcc/clang's -finstrument-functions (function boundaries, not statement level).

C#: no per-statement hook - Profiler/IL instrumentation is tooling-grade (CLR Profiler API); a manual wrapper approximates it (example).

Go: no per-statement hook - the runtime tracer is a performance tool, not a callback hook; a manual wrapper approximates it (example).

Java: no per-statement hook - java.lang.instrument is agent-grade bytecode tooling; a manual wrapper approximates it (example).

JavaScript: no per-statement hook - Node --cpu-prof/debuggers are tooling-grade; a manual wrapper approximates it (example).

PHP: declare(ticks=N) + register_tick_function is the unique mechanism - a callback fires every N low-level tickable statements (the firing granularity counts zend opcode structure, not source statements, and may change across versions; the example records the local count honestly); unregister_tick_function deregisters; since 8.5 tick handlers deactivate later during request shutdown (after output cleanup); the modern replacement idea: use Xdebug-class tools for profiling, never ticks in production.

Python: sys.settrace with line events is the closest hook - but its official role is debugger/coverage tooling (PEP 669 monitoring is the new backend); it only fires for newly entered frames (the module top level never triggers, so the example puts statements in work()); high overhead, not a production hook.

Rust: no per-statement hook - debug_assert!/macros are compile-time; a manual wrapper approximates it (example).

TypeScript: same as JavaScript - no per-statement hook.