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

From: Date: Mon, 04 May 2020 18:29:31 +0000
Subject: Bug #74339 [Com]: 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-226916@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 Comment by: everx80 at gmail dot com 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: @nikic@php.net Here in full detail: $ /usr/bin/php -v PHP 7.4.5 (cli) (built: Apr 30 2020 04:42:29) ( ZTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies $ cat test.php #!/usr/bin/php <?php declare(strict_types=1); echo "test"; $ chmod +x test.php $ file test.php test.php: PHP script, UTF-8 Unicode text executable $ ./test.php PHP Fatal error: strict_types declaration must be the very first statement in the script in /home/test/test.php on line 2 Previous Comments: ------------------------------------------------------------------------ [2020-05-04 17:24:14] nikic@php.net @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? ------------------------------------------------------------------------ [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) ------------------------------------------------------------------------ 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 (#226916) next »