|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-03-30 11:47 UTC] spam2 at rhsoft dot net
Description: ------------ for cli scripts it's no problem to start with a #!/usr/bin/php shebang followed by <?php declare(strict_types=1); as first line, in case of mod_php the sample below throws "PHP Fatal error: strict_types declaration must be the very first statement in the script" *it is* because PHP has no business to claim anything outside <?php ?> as part of "the script" Test script: --------------- <html> <head><title>Test</title></head> <body> <?php declare(strict_types=1); ?> </body> </html> PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 03:00:01 2025 UTC |
Shebangs are explicitly ignored. Everything else that appears outside PHP tags is considered to be script output. This is the way that PHP has always worked. To make your code work, do this: <?php declare(strict_types=1); ?> <html> <head><title>Test</title></head> <body> <?php // code here ?> </body> </html>than at least ignore shebangs also with mod_php #!/usr/bin/php <?php declare(strict_types=1); if(PHP_SAPI != 'cli') { exit('CLI ONLY'); } ?> is also broken when called via browser which leads to fatal erros and so flood logs which is bad in case of emial reporting every 30 minutes when any of some hundret vhsost triggers any php-warning/error> You shouldn't have CLI scripts within your webroot in the first place ... really? #!/usr/bin/php <?php declare(strict_types=1); switch(PHP_SAPI} { case 'cli': break; default: break; } ...shared..code.... ?> i have ton of examples where this works pretty fine by intention while in cron mode the only output are errors and when runnigng in the browser authenticated against the CMS you get runtime informations maybe i am the only guy on this planet which is using PHP and cli heavily for a decade and write any code so that it is 100% cli capable including the whole CMS bootstrap but that don't change the fact that "You shouldn't have CLI scripts within your webroot" is nonsense and frankly on shared hosting you have usually no way to run CLI or palce something outside the webroot at all and so you need to make sure that these tasks can be called via web and be it that your conjob running somewhere else running curl('https://url/cron.php?username=bla&pwd=bla'); and you break that all for no reason with claim "declare(strict_types=1)" is not the first line while IT IS and even bobody would care about "#!/usr/bin/php" in the output when called via websevrer #!/usr/bin/php <?php declare(strict_types=1);@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