Use of undefined constant STDOUT

Viewed 304

I've got a very simple line of code that results in the error message "Warning: Use of undefined constant stdout - assumed 'stdout' (this will throw an Error in a future version of PHP) in /xxxxx/terminal.php on line 49"

That line is

 fwrite(STDOUT, 'Process started: '.date('H:i:s').'...'.PHP_EOL);

I tried preceding that with

if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb'));

but that results in a Server 500 error.

Any ideas as to what I can do next?

MTIA UPDATE 15/10/21 Currently the entire module looks like this

    $params = [];
    $envs = [
        'local' => 'local.******.com',
        'staging' => 'chimes.testing.pm',
        'live' => '******.com'
    ];
    $disableRouter = true;
    $_SERVER['HTTP_HOST'] = $envs['live'];
        $stdout = fopen('php://stdout', 'w');

    if (is_array($argv)) {
        foreach ($argv as $arg) {
                    echo $arg.''.PHP_EOL;
            if (preg_match('#^--([^=]+)=(.+)$#is', $arg, $match)) {
                $params[$match[1]] = $match[2];
            }
        }
    } else {
            if (!empty($_GET['env'])){
                $params['env'] = $_GET['env'];
                $params['update'] = $_GET['update'];
            }
        }

    if (!empty($params['env']) && !empty($envs[$params['env']])) {
        $_SERVER['HTTP_HOST'] = $envs[$params['env']];
    }

    if (!empty($params['homestead'])) {
        putenv('APP_ENV=homestead');
    }

    require_once('index.php');

    /*-----------------------------------------------------------------------------------
        To update from ******
    -------------------------------------------------------------------------------------

    * /opt/plesk/php/7.0/bin/terminal.php --update=brands --env=staging
    * /opt/plesk/php/7.0/bin/terminal.php --update=categories --env=staging
    * /opt/plesk/php/7.0/bin/terminal.php --update=all-products --env=staging
    * /opt/plesk/php/7.0/bin/terminal.php --update=products-in-last-day --env=staging
    * /opt/plesk/php/7.0/bin/terminal.php --update=create-orders --env=staging
    * /opt/plesk/php/7.0/bin/terminal.php --update=get-customers --env=staging

    -----------------------------------------------------------------------------------*/
    define('STDOUT', 'php://stdout');
    fwrite(STDOUT, 'Process started: '.date('H:i:s').'...'.PHP_EOL);
    echo 'Process started: '.date('H:i:s').'...'.PHP_EOL;
    echo $params['update'].''.PHP_EOL;

    // Session::set("auth", "7");   
        // setcookie("little_terra_auth", "7");

    if (!empty($params['update'])) {
        switch ($params['update']) {
            case 'brands':
                fwrite(STDOUT, 'Updating brands...'.PHP_EOL);
                PMCOM\Core\Router::route('/admin/spark-stone/get-brands/');
            break;
            case 'categories':
                fwrite(STDOUT, 'Updating categories...'.PHP_EOL);
                PMCOM\Core\Router::route('/admin/spark-stone/get-categories/');
            break;
            case 'all-products':
                fwrite(STDOUT, 'Updating all products...'.PHP_EOL);
                PMCOM\Core\Router::route('/admin/spark-stone/get-products/');
            break;
            case 'products-in-last-day':
                echo 'Updating products from the previous day...'.PHP_EOL;
                fwrite(STDOUT, 'Updating products from the previous day...'.PHP_EOL);
                PMCOM\Core\Router::route('/admin/spark-stone/get-products-by-date/');
            break;
            case 'create-orders':
                fwrite(STDOUT, 'Creating orders...'.PHP_EOL);
                PMCOM\Core\Router::route('/admin/spark-stone/create-order/');
            break;
            case 'get-customers':
                fwrite(STDOUT, 'Updating customers...'.PHP_EOL);
                PMCOM\Core\Router::route('/admin/spark-stone/get-customers/');
            break;
        }
    }

    echo 'Process completed: '.date('H:i:s').'...'.PHP_EOL;
    ob_end_flush();

1 Answers

but that results in a Server 500 error.

STDOUT is only defined in CLI SAPI, it doesn't exist in CGI/FastCGI SAPI.

If you want to show something on the browser, use echo.

If you want to write to a file, consider error_log or file_put_contents.

If you want to use STDOUT, try run the php script with command line.

Related