Weird session issue in drupal

Was seeing some session issue as follows:

 1[me@example default]$ cd /var/www/example.com/docroot
 2[me@example docroot]$ drush st
 3
 4session_set_save_handler(): Cannot change save handler when headers already sent session.inc:242                                                                         [warning]
 5session_id(): Cannot change session id when headers already sent session.inc:266                                                                                         [warning]
 6session_name(): Cannot change session name when headers already sent in drupal_settings_initialize() (line 831 of                                                        [warning]
 7/var/www/example.com/docroot/includes/bootstrap.inc).
 8 Drupal version                  :  7.69                                             
 9 Site URI                        :  http://default                                   
10 Database driver                 :  mysql                                            
11 Database hostname               :  localhost                                        
12 Database username               :  <REDACTED>                                     
13 Database name                   :  <REDACTED>                                      
14 Database                        :  Connected                                        
15 Drupal bootstrap                :  Successful                                       
16 Drupal user                     :                                                   
17 Default theme                   :  bootstrap_examplesite                            
18 Administration theme            :  seven                                            
19 PHP configuration               :  /etc/php.ini                                     
20 PHP OS                          :  Linux                                            
21 Drush script                    :  /usr/local/bin/drush                             
22 Drush version                   :  8.2.3                                            
23 Drush temp directory            :  /tmp                                             
24 Drush configuration             :                                                   
25 Drush alias files               :                                                   
26 Install profile                 :  standard                                         
27 Drupal root                     :  /var/www/example.com/docroot  
28 Drupal Settings File            :  sites/default/settings.php                       
29 Site path                       :  sites/default                                    
30 File directory path             :  sites/default/files                              
31 Private file directory path     :  /var/www/sites/example.com/private-files 
32 Temporary file directory path   :  /var/www/sites/example.com/private-temp  

After a lot of cross-checking/troubleshooting/debugging, initial issue was pinpointed at a blank line (see line 18 below):

 1[me@example default]$ nl -ba /var/www/example.com/docroot/sites/default/settings.local.php 
 2     1  <?php
 3     2  $databases = array(
 4     3    'default' => array(
 5     4      'default' => array(
 6     5        'driver' => 'mysql',
 7     6        'database' => '<REDACTED>',
 8     7        'username' => '<REDACTED>',
 9     8        'password' => '<REDACTED>',
10     9        'host' => 'localhost',
11    10        'prefix' => '',
12    11      ),
13    12    ),
14    13  );
15    14  
16    15  $conf['file_temporary_path'] = '/var/www/example.com/private-temp';
17    16  $conf['file_private_path'] = '/var/www/example.com/private-files';
18    17  ?>
19    18  

BUT surprise, it turned out it could have been averted if the ending php tag '?>' was not added at all per excerpt from https://www.reddit.com/r/PHP/comments/5i0v26/is_using_a_php_closing_tag_considered_bad/

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later.

An excerpt from Drupal Coding Standard https://www.drupal.org/docs/develop/standards/coding-standards

Note that as of Drupal 4.7, the ?> at the end of code files is purposely omitted. This includes for module and include files. The reasons for this can be summarized as:

  • Removing it eliminates the possibility for unwanted whitespace at the end of files which can cause "header already sent" errors, XHTML/XML validation issues, and other problems.
  • The closing delimiter at the end of a file is optional. PHP.net itself removes the closing delimiter from the end of its files (example: prepend.inc), so this can be seen as a "best practice."