Basically, both are personal choices. In practice, neither way is "better" or "worse" than the other, and they work the same way.
If you have a connection just at the beginning of the code, it does not get in the way of putting it directly into the function. If you are going to use this data after other logical parts of the code, leaving the data in variables at the beginning of the code makes maintenance easier.
Now, as in the first example, where the connection function is next to the variables, it does not make much sense. In short: give it anyway, use whatever is most convenient or desirable for your specific case.
It is not part of the question, but I think it is good to comment that it is more important to separate the variables, for reuse purposes, to have the connection in a separate file, for example dbconnect.php
that makes the connection for you, and all pages that use the DB you use require_once( 'dbconnect.php' );
, so it will only have one place to adjust in case of a server change or configuration in general.
Note: In principle the question appears to be one that generates "opinion-based" answers, which is usually a reason for closing, but how is it clarified to see if there is any reason, not "which is better", I found it appropriate to answer