Something like this should work:
" Save the cursor position
let cursor_save = getpos('.')
" Go to the begining of the file
call cursor(1, 1)
" Get the line containing "<?php"
let phpLine = search('<?php')
" If the declaration isn't in the buffer and there is one line containing "<?php"
if search('<?php\ndeclare(strict_types=1);') == 0 && phpLine != 0
" Add the declaration after the line
call append(phpLine, "declare(strict_types=1);")
" Restore cursor position
call setpos('.', cursor_save)
autocmd! BufReadPost *php call AddPHPStrictTypes()
We declare a function which searches for the declaration. If the declaration is not found it search for the line containing
<?php and add the declaration after it.
Then we use an autocommand to execute the function each time you open a new php file.
To understand the function better you may want to read
And to change the autocommand: