Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use both shebang and strict type declaration in PHP 7

I'm writing a command line PHP script with PHP7.

When I put the shebang (#!/usr/bin/php) on top of the file, if I add the strict mode with declare(strict_types=1), I get the following error :

PHP Fatal error: strict_types declaration must be the very first statement in the script in index.php on line 3

The only way I found to get the strict types working is to remove the shebang line.

Is there a way to use both shebang and strict types or it is a php bug ?

like image 772
ᴄʀᴏᴢᴇᴛ Avatar asked Aug 04 '16 10:08

ᴄʀᴏᴢᴇᴛ


1 Answers

Ok so here is the reason why the strict types declaration did not work : i had a new line between the shebang and the php opening tag.

working example :

#!/usr/bin/php
<?php
declare(strict_types=1);

// your code
like image 95
ᴄʀᴏᴢᴇᴛ Avatar answered Oct 16 '22 20:10

ᴄʀᴏᴢᴇᴛ