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

From: Date: Mon, 13 Dec 2021 23:45: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-238387@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: jake at qzdesign dot co dot uk 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: > You shouldn't have CLI scripts within your webroot in the first place ... Are you also saying the Composer-installed 3rd party libraries should be in a separate part of the filesystem? Because in vendor/bin it places several files with a shebang which cause this error (with the reported PHP version). Perhaps you'd like to take that issue up with the folk at Composer. By extension, what you're actually saying is that there shouldn't be any PHP files whatsoever within your web root, and the .htaccess file (or whatever) should invoke a PHP file in a different part of the filesystem. That is surely ridiculous. Previous Comments: ------------------------------------------------------------------------ [2020-12-15 10:14:14] andergmartins at gmail dot com I started having this fatal error for a file UTF-8 without BOM. I have tried diverse charsets, with no success. I'm running PHP 7.3.5 but tried with 7.4.1. Same result. It say's: ------------- PHP Fatal error: strict_types declaration must be the very first statement in the script in ..../vendor/php-di/php-di/src/ContainerBuilder.php on line 3 ------------- And here are the first lines of the file: ------------- <?php declare(strict_types=1); ------------- I looked for any hidden char, but didn't find anything: ------------- $ hexdump -n 20 -C "...../vendor/php-di/php-di/src/ContainerBuilder.php" 00000000 3c 3f 70 68 70 0a 0a 64 65 63 6c 61 72 65 28 73 |<?php..declare(s| 00000010 74 72 69 63 |tric| 00000014 -------------- Then I tried moving the declare statement to the first line: -------------- <?php declare(strict_types=1); namespace DI; -------------- Same error, but on line 1: -------------- PHP Fatal error: strict_types declaration must be the very first statement in the script in ..../vendor/php-di/php-di/src/ContainerBuilder.php on line 1 -------------- And here is the hexdump: -------------- $ hexdump -n 20 -C "..../vendor/php-di/php-di/src/ContainerBuilder.php" 00000000 3c 3f 70 68 70 20 64 65 63 6c 61 72 65 28 73 74 |<?php declare(st| 00000010 72 69 63 74 |rict| 00000014 -------------- If I remove the "declare(strict_types=1)", then it stops on a next file. This issue started a few days ago... I wasn't having problems before that. And it happens for other libraries like PHP Unit, or even for new files I create to test. ------------------------------------------------------------------------ [2020-05-04 18:36:25] everx80 at gmail dot com @nikic@php.net I am sorry. After re-checking everything once more, it seems that an invisible UTF-8 character somehow snuck into the file, before the <?php So it's actually not an issue, but quite difficult to see the reason. It would be nice if PHP could somehow point that out, but idk if it's worth it... ------------------------------------------------------------------------ [2020-05-04 18:29:31] everx80 at gmail dot com @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 ------------------------------------------------------------------------ [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"; ------------------------------------------------------------------------ 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 (#238387) next »