Bug #74339 [Nab]: BOGUS: Fatal error: strict_types declaration must be the very first statement

From: Date: Mon, 04 May 2020 17:24:14 +0000
Subject: Bug #74339 [Nab]: BOGUS: Fatal error: strict_types declaration must be the very first statement
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226915@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74339&edit=1 ID: 74339 Updated by: nikic@php.net Reported by: spam2 at rhsoft dot net Summary: BOGUS: Fatal error: strict_types declaration must be the very first statement Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Linux PHP Version: 7.1.3 Block user comment: N Private report: N New Comment: @everx80: When you say "from cli", what exactly do you mean? Invoking a file with CLI binary? Piping it as stdin? Entering it in interactive mode? Previous Comments: ------------------------------------------------------------------------ [2020-05-04 17:06:02] everx80 at gmail dot com @nikic@php.net i didn't want to open another bug if not neccesary, however with PHP 7.4.5 the following code from cli still doesn't work and gives the same error: #!/usr/bin/php <?php declare(strict_types=1); echo "Hello World\n"; ------------------------------------------------------------------------ [2019-10-07 15:04:43] nikic@php.net @kevin at kevinlocke dot name: The stdin issue will be fixed in PHP 7.4. ------------------------------------------------------------------------ [2019-10-07 13:38:09] kevin at kevinlocke dot name This issue also occurs when CLI scripts are read from stdin by php (e.g. for linting). For example, if mycli.php has the following content: #!/usr/bin/php <?php declare(strict_types=1); echo "Hello World\n"; Running php -l mycli.php prints "No syntax errors detected in mycli.php". Running php -l < mycli.php (as some lint tools like ALE do) prints "PHP Fatal error: strict_types declaration must be the very first statement in the script in Standard input code on line 3". ------------------------------------------------------------------------ [2019-01-19 17:50:54] spam2 at rhsoft dot net all the workarounds are fine but the first php staement is after <?php no matter how many shebangs and BOM are before and when i read something like "You shouldn't have CLI scripts within your webroot in the first place" i just get upset since the whole purpose of if(PHP_SAPI === 'cli') is to write proper software which bheaves corretly in CLI as well as in web-mode (require a web-login versus just do the job as example) ------------------------------------------------------------------------ [2019-01-18 16:36:17] stefansobick at gmx dot de Convert the file to utf without BOM, than it should work fine, when you have done it like this: <?php declare(strict_types=1); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Page title</title> </head> <body> ... ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=74339 -- Edit this bug report at https://bugs.php.net/bug.php?id=74339&edit=1

« previous php.bugs (#226915) next »