0

My question is regarding the php function include();

Is it okay to include one main php file to store all variables, functions, queries etc.. and from within the html, tag in and out of php for the function and variables needed?

I feel this would keep my html still looking pretty but I'm not sure of it's efficiency.

config.php

<?php
$username = 'foo';
function getPoints(){};
mysqli_query(,);

So basically index.php would be

<?php include('config.php') ?>
<hmtl>
<head></head>
<div id="ui"><? echo $variable?></div>
<body></body>
</html>
3
  • What kind of website or webserver (how complex, what scale) are you building? Commented Oct 27, 2012 at 19:20
  • couple thousand users with regular queries to the database. I assume the include is using an extra http request? Commented Oct 27, 2012 at 19:30
  • @Stax No, it is included at compile time (when the PHP script is requested). It is only one HTTP request, but PHP internally reads the other file on your server. Commented Oct 27, 2012 at 19:33

1 Answer 1

1

I am not sure whether I properly understood what you are trying to achieve, but since the application seems pretty simple (and you do not need complex solutions), keeping the actual business logic separated from the presentation (HTML code) is completely okay.

So in fact you may have two files like that:

  • index.php (your "HTML"),
  • library.php (your business logic),

and in this case it should be very efficient. Of course unless you will break something, but this is not connected to the structure itself ;)

The nature of PHP allows it to be embedded in HTML, but do not overdo it. Keep your logic as separated as possible from the presentation - preferably define every function / class / variable in library.php, and only use the results when displaying in index.php.

Also keeping such structure, with code in one place avoids the overload that could be introduced by autoloading (finding files, determining where to find specific class etc.). Some even optimize their code by putting it all in one place ("compiling").

Later in the learning process you will probably need to learn some framework, that will take different approach. But you probably do not need it right away ;)

Sign up to request clarification or add additional context in comments.

1 Comment

PS. In the code you have just provided, omit the closing PHP tag - it is not necessary and often harmful, so if your file (config.php) contains only PHP (as it should), you definitely should remove this tag (?>).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.