PHP Built-in web server and syntax error in a router script

410 Views Asked by At

I am using PHP Built-in web server for testing purposes. For example, with a simple router script:

<?php
echo 'Hello world';

Launching it with php -S localhost:8000 router.php. Works.

Now, let't say I make a syntax error in the router script (e.g. missing semicolon):

<?php
echo 'Hello world'

and launch the dev server with the same command.

Reaching localhost:8000 in the browser gives me 404 error: The requested resource / was not found on this server.. It gives me absolutely no clue about the problem or a syntax error placement. The terminal I lauched the web server in does not show any error (besides 404) either.

I have error reporting set to E_ALL in php.ini. I have the display_errors set to On.

Where to look for the error message in such case? How to force PHP to render such errors to the browser instead of a 404 error page?

1

There are 1 best solutions below

0
On

After some trial and error, I came to conclusion that php-builtin-server won't log error in any file but directly print to console and send to browser, but only if the php file not contains any lexical error. I can't find any documentation on this so feel free to correct me..

A simple work around can be done, check php file lexically with php -l before running php-builtin-server. To make things easier, create a batch file for it.

windows

create php-srv.bat (or any valid name) with content:

php -l %1 | find /i "No syntax errors"
if not errorlevel 1 (
   php -S localhost:8000 %1
)

execute it with php-srv route.php

linux

create php-srv.sh (or any valid name) with content:

if php -l $1 | grep 'No syntax errors detected in'; then
    php -S localhost:8000 $1
fi

make it executable with chmod and then execute it with ./php-srv.sh route.php

Hope that's help