PHP: Execution of code after "die" function in PHP/Laravel

3k Views Asked by At

I'm new to PHP and I want to start my laravel project that I copied from another source. I get a 500 error. If I try to debug the index.php in public folder I get an odd behaviour that die function did not function if the kernel line in code is there. I don't understand that, I thought that die function stops the execution.

<?php

die ('hi'); //this die

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
    require __DIR__.'/../storage/framework/maintenance.php';
}



require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

//If I comment out kernel line "die" on top doesn't return 'hi'. The response is 500 html code.
//$kernel = $app->make(Kernel::class);
/*
$response = tap($kernel->handle(
    $request = Request::capture()
))->send();

$kernel->terminate($request, $response);
*/

EDIT: the get log from the access log of server is:

"GET /index2.php HTTP/2.0" 500 86 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"

I doesn't get any error in error log. So I have activated PHP-error log. I get this error message:

[24-Sep-2020 11:00:39 Europe/Berlin] PHP Parse error:  syntax error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /usr/.../inertia/public/index2.php on line 44
2

There are 2 best solutions below

2
Lukáš Michalec On BEST ANSWER

I think it is because PHP is not interpreting your code line by line, but it has to first convert it into a suitable format for the runtime engine.

  1. Ignore comments
  2. Resolve variables, function names, and so forth and create the symbol table
  3. Construct the abstract syntax tree of your program
  4. Write the bytecode

The server returns 500 code even before code execution starts. The same will happen if you will have any syntax error after the "die();" method. If there is an issue with "use ..." command it will crash during that preparation phase.

0
Tony On

So after googling the PHP error message I've found my PHP Version was 5.6 and not 7.4, it was a problem. Thank you all for the help! It helped me a lot. The laravel has .htaccess file in public folder. This file has overriden the PHP-Version to default PHP-Version 5.6.