PHP – Code only executes when error is thrown [closed]

The issue is that the code in router.php only executes successfully if the code throws an error. This means that the http://localhost/test/db endpoint is returning a 200 Status OK (see bottom of post) response only when an error is thrown, and not when no error is thrown. I run my site on http://localhost/test/.

I enabled the following error reporting configurations in php.ini, but the error.log and access.log shows no error:

error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On

I use the Bramus router library in the router.php file, but I doubt that is related to the issue: https://github.com/bramus/router. These are my affected files:

test.conf (enabled)

Alias /test "/var/www/html/test"

<Directory "/var/www/html/test">
 AllowOverride None
 Options Indexes FollowSymLinks
 Require all granted

 # Required for Bramus router
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . index.php [L]

/var/www/html/test/index.php

<?php
require 'router.php';

// Create Router instance
$router = new Router();

// Run it!
$router->run();
?>

/var/www/html/test/router.php

<?php
require __DIR__ . '/libraries/Bramus/Router.php';
require_once("database.router.php");

class Router {

    var $router = null;

    function __construct() {

        // Create Router instance
        $this->router = new BramusRouterRouter();

        $this->debug_to_console("This is never printed to console...");

        // If I uncomment below line, the code is executed (and throws an error because it should be $this->debug_to_console("..."))
        // debug_to_console("Testing...");

        new DatabaseRouter($this->router);

        // Set default 404
        $this->router->set404(function () {
            header("HTTP/1.1 404 Not Found");
            exit();
        });
    }
    
    function debug_to_console($data) {
        $output = $data;
        if (is_array($output))
            $output = implode(',', $output);

        echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
    }

    function run() {
        $this->router->run();
    }
}
?>

/var/www/html/test/database.router.php

<?php
class DatabaseRouter
{
    function __construct($router) 
    {
        $router->get('/db', function() {
            echo true;
        });
    }
}
?>

Here is proof of different GET response codes when performing curl GET requests to the http://localhost/test/db endpoint with debug_to_console("Testing..."); commented and uncommented in router.php (this is the only code change done to result in either a 200 or 404 response doing a GET request)

This is with debug_to_console(“Testing…”); commented in router.php

root@ubuntu:~$ curl -I http://localhost/test/db
HTTP/1.1 404 Not Found
Date: Sun, 21 Apr 2024 09:13:17 GMT
Server: Apache/2.4.52 (Ubuntu)
Set-Cookie: PHPSESSID=iviq2fkk1ts3nu5ki8fjc5k2qv; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: text/html; charset=UTF-8

Error log shows nothing added while doing this request.

This is with debug_to_console(“Testing…”); commented out in router.php

root@ubuntu:~$ curl -I http://localhost/test/db
HTTP/1.1 200 OK
Date: Sun, 21 Apr 2024 09:14:24 GMT
Server: Apache/2.4.52 (Ubuntu)
Set-Cookie: PHPSESSID=dmvqs7jea58puv67o23l6os1mm; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: text/html; charset=UTF-8

Error log shows the following information:

[Sun Apr 21 09:22:42.912553 2024] [php:error] [pid 1005114] [client 192.168.8.0:34000] PHP Fatal error:  Uncaught Error: Call to undefined function debug_to_console() in /var/www/html/test/router.php:17nStack trace:n#0 /var/www/html/test/index.php(5): Router->__construct()n#1 {main}n  thrown in /var/www/html/test/router.php on line 17, referer: http://localhost/test

Thankful for all help! Please let me know if you need more debugging information.