Skip to content

declare(strict_types=1) does not ignore null AST nodes #19719

Closed

Description

@adrian-schenk

Description

The following code:

<?php
// e.g some comments
?>
<?php
declare(strict_types=1);
?>

Resulted in this output:

Fatal error: strict_types declaration must be the very first statement in the script in /home/tmp/test.php on line 5
Stack trace:
#0 {main}

But I expected this output instead:

PHP Version

nightly

Operating System

No response

Activity

  1. devnexen commented on Sep 5, 2025

    @devnexen
    Member

    Hi @FearXenon what s the practical reason for not doing this ?

    <?php
    // e.g some comments
    
    declare(strict_types=1);
    ?>

    AFAIK, it might be possible to "fix" it, but do not know how trivial it is. cc @iluuu1994

  2. iluuu1994 commented on Sep 5, 2025

    @iluuu1994
    Member

    Not really a bug. ?> includes an implicit ;, so it's not actually the first statement.

  3. adrian-schenk commented on Sep 5, 2025

    @adrian-schenk
    Author

    Hi @devnexen,
    i´ve seen some projects that use a separate script to prepend licensing/copyright info to php files.
    some of them did stuff like this:

    <?php
    Copyright (c) 2025 John Doe
    ...
    ?>
    <?php
    //start of normal code
    ?>

    besides, I don´t really see the point of why it shouldn´t work, since technically nothing gets "executed" before, right?

  4. devnexen commented on Sep 5, 2025

    @devnexen
    Member

    I see, but again that seems a very particular case. "How much the juice is worth the squeeze" is the question. But I ll let the lexer expert speak :)

  5. ndossche commented on Sep 7, 2025

    @ndossche
    Member

    We do allow it for namespaces, so why not also for strict_types? I don't see a reason why this wouldn't work (I could be missing some detail):

    diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
    index e01b985b6d6..90d305dba34 100644
    --- a/Zend/zend_compile.c
    +++ b/Zend/zend_compile.c
    @@ -7004,7 +7004,7 @@ static void zend_compile_declare(zend_ast *ast) /* {{{ */
     		} else if (zend_string_equals_literal_ci(name, "strict_types")) {
     			zval value_zv;
     
    -			if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
    +			if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
     				zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
     					"the very first statement in the script");
     			}
    
  6. iluuu1994 commented on Sep 7, 2025

    @iluuu1994
    Member

    @nielsdos Should be ok, but would handle it as a feature rather than bug. Not sure if the same goes for declare(encoding=);.

  7. ndossche commented on Sep 7, 2025

    @ndossche
    Member

    @nielsdos Should be ok, but would handle it as a feature rather than bug. Not sure if the same goes for declare(encoding=);.

    Yeah sure, I can submit it as a new feature (for after 8.5). I don't think we should do encoding as that may have influence on some special characters that may have influence.

  8. added a commit that references this issue on Sep 17, 2025
    e8a6d91
  9. added a commit that references this issue on Sep 21, 2025
    1f6ac30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions