|
| 1 | +# Output |
| 2 | + |
| 3 | +`ext-php-rs` provides several macros and functions for writing output to PHP's |
| 4 | +stdout and stderr streams. These are essential when your extension needs to |
| 5 | +produce output that integrates with PHP's output buffering system. |
| 6 | + |
| 7 | +## Text Output |
| 8 | + |
| 9 | +For regular text output (strings without NUL bytes), use the `php_print!` and |
| 10 | +`php_println!` macros. These work similarly to Rust's `print!` and `println!` |
| 11 | +macros. |
| 12 | + |
| 13 | +### `php_print!` |
| 14 | + |
| 15 | +Prints to PHP's standard output without a trailing newline. |
| 16 | + |
| 17 | +```rust,ignore |
| 18 | +use ext_php_rs::prelude::*; |
| 19 | +
|
| 20 | +#[php_function] |
| 21 | +pub fn greet(name: &str) { |
| 22 | + php_print!("Hello, {}!", name); |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### `php_println!` |
| 27 | + |
| 28 | +Prints to PHP's standard output with a trailing newline. |
| 29 | + |
| 30 | +```rust,ignore |
| 31 | +use ext_php_rs::prelude::*; |
| 32 | +
|
| 33 | +#[php_function] |
| 34 | +pub fn greet(name: &str) { |
| 35 | + php_println!("Hello, {}!", name); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +> **Note:** `php_print!` and `php_println!` will panic if the string contains |
| 40 | +> NUL bytes (`\0`). For binary-safe output, use `php_write!` instead. |
| 41 | +
|
| 42 | +## Binary-Safe Output |
| 43 | + |
| 44 | +When working with binary data that may contain NUL bytes, use the binary-safe |
| 45 | +output functions. These are essential for outputting raw bytes, binary file |
| 46 | +contents, or any data that might contain `\0` characters. |
| 47 | + |
| 48 | +### `php_write!` |
| 49 | + |
| 50 | +Writes binary data to PHP's standard output. This macro is binary-safe and can |
| 51 | +handle data containing NUL bytes. It uses the SAPI module's `ub_write` function. |
| 52 | + |
| 53 | +```rust,ignore |
| 54 | +use ext_php_rs::prelude::*; |
| 55 | +
|
| 56 | +#[php_function] |
| 57 | +pub fn output_binary() -> i64 { |
| 58 | + // Write a byte literal |
| 59 | + php_write!(b"Hello World"); |
| 60 | +
|
| 61 | + // Write binary data with NUL bytes (would panic with php_print!) |
| 62 | + let bytes_written = php_write!(b"Hello\x00World"); |
| 63 | +
|
| 64 | + // Write a byte slice |
| 65 | + let data: &[u8] = &[0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello" |
| 66 | + php_write!(data); |
| 67 | +
|
| 68 | + bytes_written as i64 |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +The macro returns the number of bytes written, which can be useful for verifying |
| 73 | +that all data was output successfully. |
| 74 | + |
| 75 | +## Function API |
| 76 | + |
| 77 | +In addition to macros, you can use the underlying functions directly: |
| 78 | + |
| 79 | +| Function | Target | Binary-Safe | Description | |
| 80 | +|----------|--------|-------------|-------------| |
| 81 | +| `zend::printf()` | stdout | No | Printf-style output (used by `php_print!`) | |
| 82 | +| `zend::write()` | stdout | Yes | Binary-safe stdout output | |
| 83 | + |
| 84 | +### Example using functions directly |
| 85 | + |
| 86 | +```rust,ignore |
| 87 | +use ext_php_rs::zend::write; |
| 88 | +
|
| 89 | +fn output_data(data: &[u8]) { |
| 90 | + let bytes_written = write(data); |
| 91 | + if bytes_written != data.len() { |
| 92 | + eprintln!("Warning: incomplete write"); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Comparison |
| 98 | + |
| 99 | +| Macro/Function | Binary-Safe | Supports Formatting | Target | |
| 100 | +|----------------|-------------|---------------------|--------| |
| 101 | +| `php_print!` | No | Yes | stdout | |
| 102 | +| `php_println!` | No | Yes | stdout | |
| 103 | +| `php_write!` | Yes | No | stdout | |
| 104 | + |
| 105 | +## When to Use Each |
| 106 | + |
| 107 | +- **`php_print!` / `php_println!`**: Use for text output with format strings, |
| 108 | + similar to Rust's `print!` and `println!`. Best for human-readable messages. |
| 109 | + |
| 110 | +- **`php_write!`**: Use when outputting binary data, file contents, or any data |
| 111 | + that might contain NUL bytes. Also useful when you need to know exactly how |
| 112 | + many bytes were written. |
0 commit comments