How can PHP scripts be executed in different environments such as the command line or in the browser without requiring HTML output?

PHP scripts can be executed in different environments by using conditional statements to check the environment and adjust the output accordingly. For example, to run a PHP script in the command line, you can use the PHP CLI SAPI (Server Application Programming Interface) and avoid generating HTML output. In the browser, you can include HTML output as needed.

<?php
if (php_sapi_name() === 'cli') {
    // Code to execute in command line
    echo "This is running in the command line environment.";
} else {
    // Code to execute in the browser
    echo "<h1>This is running in the browser environment.</h1>";
}