SysTutorials

How to Write Output to STDERR and STDOUT in PHP

· updated April 12, 2026

In PHP, STDOUT and STDERR are standard output streams that handle different types of output. STDOUT carries normal program output, while STDERR carries error messages and diagnostic information. Properly directing output to the correct stream is essential for building reliable scripts and applications.

Using fwrite() with Stream Handles

The most straightforward approach uses fwrite() with PHP’s predefined stream constants:

// Write to STDOUT
fwrite(STDOUT, "This is normal output\n");

// Write to STDERR
fwrite(STDERR, "This is an error message\n");

These constants (STDOUT, STDERR, and STDIN) are available in CLI mode and point to the actual file descriptors (1 and 2 respectively on Unix-like systems).

Using echo and error_log()

For simpler cases, echo writes to STDOUT by default:

echo "Standard output message\n";

For STDERR, use error_log():

error_log("Error message");

Note that error_log() writes to the configured error log file (typically specified in php.ini), but in CLI scripts it often goes to STDERR. For more direct control, stick with fwrite(STDERR, ...).

Practical Example: Logging Script Output

Here’s a realistic script that separates normal output from errors:

<?php

function log_error($message) {
    fwrite(STDERR, "[ERROR] " . date('Y-m-d H:i:s') . " - " . $message . "\n");
}

function log_info($message) {
    fwrite(STDOUT, "[INFO] " . date('Y-m-d H:i:s') . " - " . $message . "\n");
}

// Usage
log_info("Processing file: config.json");

if (!file_exists('config.json')) {
    log_error("Configuration file not found");
    exit(1);
}

log_info("Configuration loaded successfully");
?>

When running this script:

Handling in Web SAPI

Note that STDOUT and STDERR constants are unavailable in the Apache/PHP-FPM web SAPIs. In web contexts, use error_log() for diagnostic information instead:

error_log("Debug information", 0); // Send to configured error handler

Check your php.ini to verify where error_log output is directed (typically /var/log/php-fpm/error.log or similar).

Best Practices

<?php
// Complete CLI script example
if ($argc < 2) {
    fwrite(STDERR, "Usage: {$argv[0]} <filename>\n");
    exit(1);
}

$file = $argv[1];

if (!is_readable($file)) {
    fwrite(STDERR, "Error: Cannot read file '$file'\n");
    exit(1);
}

fwrite(STDOUT, "Processing: $file\n");
// Process file...
exit(0);
?>

This separation ensures your script integrates well with shell pipelines, log aggregation systems, and monitoring tools that expect proper stream handling.

2026 Best Practices and Advanced Techniques

For How to Write Output to STDERR and STDOUT in PHP, understanding both the fundamentals and modern practices ensures you can work efficiently and avoid common pitfalls. This guide extends the core article with practical advice for 2026 workflows.

Troubleshooting and Debugging

When issues arise, a systematic approach saves time. Start by checking logs for error messages or warnings. Test individual components in isolation before integrating them. Use verbose modes and debug flags to gather more information when standard output is not enough to diagnose the problem.

Performance Optimization

Security Considerations

Security should be built into workflows from the start. Use strong authentication methods, encrypt sensitive data in transit, and follow the principle of least privilege for access controls. Regular security audits and penetration testing help maintain system integrity.

These complementary tools expand your capabilities:

Integration with Modern Workflows

Consider automation and containerization for consistency across environments. Infrastructure as code tools enable reproducible deployments. CI/CD pipelines automate testing and deployment, reducing human error and speeding up delivery cycles.

Quick Reference

This extended guide covers the topic beyond the original article scope. For specialized needs, refer to official documentation or community resources. Practice in test environments before production deployment.

← All posts