Stax
Stax

Reputation: 41

Including php in one file

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>

Upvotes: 0

Views: 72

Answers (1)

Tadeck
Tadeck

Reputation: 137350

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 ;)

Upvotes: 1

Related Questions