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 only at the beginning of the code, do not disturb anything put straight into the function. If you will use this data after other logical parts of the code, leaving the data in variables at the beginning of the code makes it easier to maintain.
Now, as in the first example, where the connection function is right next to the variables, it doesn’t make much sense. In short: give anyway, use whatever is convenient or desirable for your specific case.
It is not part of the question, but I think it is good to comment that more important than separating the variables, for reuse purposes, is to have the connection in a separate file, for example dbconnect.php
that makes the connection for you, and on all pages that use DB you use require_once( 'dbconnect.php' );
, so will only have a place to adjust in case of a server change or configuration in general.
Note: in principle the question appears to be one that generates answers "based on opinion", which is usually reason for closure, but as it is clarification to know if there is any reason, and not "which is better", I thought it appropriate to answer
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 only at the beginning of the code, do not disturb anything put straight into the function. If you will use this data after other logical parts of the code, separating the variables at the beginning of the code is easier to organize. Now, as in the first example, where the connection function is right next to the variables, it doesn’t make much sense. In short: give anyway, use whatever is convenient or desirable for your specific case.
– Bacco
I understood now, as you commented "as in the first example, where the connection function is right after the variables, it doesn’t make much sense", also for me it didn’t make any sense. But if you are going to use this data elsewhere is better put in variables, thanks for the explanation.
– Junior
I ended up posting as an answer to be able to complement.
– Bacco