Suppose that we have constructed a web application with PHP.
If we inspect the code inside one of the files of the application,
what we usually see is a jam of HTML code, JavaScript code,
CSS code, PHP code, SQL code etc. This kind of mixing many languages
inside the same file is a really bad thing, for these reasons:
1 - It makes the editing of the application very difficult.
Most of the editors have code colorizing features, automatic
indentation features, etc. for many languages, in order to
simplify editing and to make it easy. But when they are faced
with such a mix of languages, very often they get confused.
As a result, such features become useless, and even they
become harmful, because they may colorize wrongly or indent
wrongly.
2 - It makes the maintenance of the application very difficult.
It is very difficult for somebody else to try to read, to
understand and to modify such a code, and even for the
author of the code himself (after some time has passed).
3 - Makes difficult the team work. Suppose that a web application
is constructed by a team that is composed of PHP programmers,
who construct the server side logic of the application,
web designers (HTML+CSS) who create the look and feel of the
application, JavaScript programmers who construct the client
side logic of the application, and DB specialists who construct
the database and the queries. In this case it is very difficult
for any one of them to make modifications in a big file that
has lots of unfamiliar code to them, without making any mistake.
Take for example the web designer, it is very difficult for him
to change the look and layout of a web page that is tightly
interwoven with PHP code.
4 - Increases the complexity of web applications. Different from
the previous example, suppose now that a web application is
constructed by one or more programmers and each of them is
responsible for a certain part of the application. In this
case each of them has to have good skills in all of them:
PHP, HTML, CSS, JavaScript, database, etc. This is difficult
to happen and it increases the complexity and the difficulties
of constructing a web application.
It is clear that such a mix of codes is a bad thing and a bad practice
and it must be avoided as much as possible.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The HTML syntax offers a possibility to take out the JavaScript code and
the CSS code in separate files. It is like this:
<script language="javascript" src="file.js"></script>
<link rel="stylesheet" href="file.css" type="text/css" />
However there is no standard way to clearly separate the HTML, PHP and
SQL codes. You can try to keep the degree of mixing of HTML and PHP code
at a minimum level by carefully designing the application. E.g. you
can place all the PHP code unrelated to the HTML generation (the code
that contains the logic of the application) in a separate file and keep
the file that generates the HTML page as lean as possible. E.g.:
This does not solve the problem completely, however. First, you still
have some PHP code inside the HTML page. Second, the 'HTML' file has the
extenssion ".php", which deceives some editors and they colorize it
wrongly.
A better soluttion to separate the HTML and PHP codes is to use a
templating engine. It uses templates for generating the final HTML
page. Each template is a pure HTML file, which also has some slots
or variables in it. The values of these variables are calculated by
the PHP code and they are passed to the engine. Then the engine
reads the templates, replaces the variables by their values and thus
generates an HTML page which is sent to the browser.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The phpWebApp framework takes the templating approach for separating
the HTML code from the PHP code, but it does it in a different way
from the other templating engines. It also provides a way for taking
out of the PHP code the SQL code and queries. This is useful because
in real web applications there are usually big queries; putting them
in a separate file reduces the complexity of the PHP code (makes it
smaller and more easily understandable and modifyable) and also makes
the understanding and modification of the queries easier.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Suppose that we have constructed a web application with PHP.
If we inspect the code inside one of the files of the application,
what we usually see is a jam of HTML code, JavaScript code,
CSS code, PHP code, SQL code etc. This kind of mixing many languages
inside the same file is a really bad thing, for these reasons:
1 - It makes the editing of the application very difficult.
Most of the editors have code colorizing features, automatic
indentation features, etc. for many languages, in order to
simplify editing and to make it easy. But when they are faced
with such a mix of languages, very often they get confused.
As a result, such features become useless, and even they
become harmful, because they may colorize wrongly or indent
wrongly.
2 - It makes the maintenance of the application very difficult.
It is very difficult for somebody else to try to read, to
understand and to modify such a code, and even for the
author of the code himself (after some time has passed).
3 - Makes difficult the team work. Suppose that a web application
is constructed by a team that is composed of PHP programmers,
who construct the server side logic of the application,
web designers (HTML+CSS) who create the look and feel of the
application, JavaScript programmers who construct the client
side logic of the application, and DB specialists who construct
the database and the queries. In this case it is very difficult
for any one of them to make modifications in a big file that
has lots of unfamiliar code to them, without making any mistake.
Take for example the web designer, it is very difficult for him
to change the look and layout of a web page that is tightly
interwoven with PHP code.
4 - Increases the complexity of web applications. Different from
the previous example, suppose now that a web application is
constructed by one or more programmers and each of them is
responsible for a certain part of the application. In this
case each of them has to have good skills in all of them:
PHP, HTML, CSS, JavaScript, database, etc. This is difficult
to happen and it increases the complexity and the difficulties
of constructing a web application.
It is clear that such a mix of codes is a bad thing and a bad practice
and it must be avoided as much as possible.
The HTML syntax offers a possibility to take out the JavaScript code and
the CSS code in separate files. It is like this:
<script language="javascript" src="file.js"></script>
<link rel="stylesheet" href="file.css" type="text/css" />
However there is no standard way to clearly separate the HTML, PHP and
SQL codes. You can try to keep the degree of mixing of HTML and PHP code
at a minimum level by carefully designing the application. E.g. you
can place all the PHP code unrelated to the HTML generation (the code
that contains the logic of the application) in a separate file and keep
the file that generates the HTML page as lean as possible. E.g.:
<?php
include "app_logic.php";
$title = ...;
. . . . .
?>
<html>
<head>
<title><? echo $title ?></title>
</head>
<body>
. . . . .
</body>
</html>
This does not solve the problem completely, however. First, you still
have some PHP code inside the HTML page. Second, the 'HTML' file has the
extenssion ".php", which deceives some editors and they colorize it
wrongly.
A better soluttion to separate the HTML and PHP codes is to use a
templating engine. It uses templates for generating the final HTML
page. Each template is a pure HTML file, which also has some slots
or variables in it. The values of these variables are calculated by
the PHP code and they are passed to the engine. Then the engine
reads the templates, replaces the variables by their values and thus
generates an HTML page which is sent to the browser.
The phpWebApp framework takes the templating approach for separating
the HTML code from the PHP code, but it does it in a different way
from the other templating engines. It also provides a way for taking
out of the PHP code the SQL code and queries. This is useful because
in real web applications there are usually big queries; putting them
in a separate file reduces the complexity of the PHP code (makes it
smaller and more easily understandable and modifyable) and also makes
the understanding and modification of the queries easier.