A website is built from four different tools. Each one has its own job.
| Tool | What it does | Where it runs |
|---|---|---|
| HTML | Describes the structure and content of a page | Browser |
| CSS | Describes how the page looks | Browser |
| JavaScript | Makes the page react to the user | Browser (mostly) |
| PHP | Builds the page, handles data, talks to the database | Server |
An example with all four
Imagine a page that lists blog posts.
- PHP reads the posts from the database and writes the HTML for each one.
- HTML is the result: headings, paragraphs and links.
- CSS styles those headings and paragraphs.
- JavaScript adds a “load more” button that asks the server for the next posts.
<?php
$posts = ["Learning PHP", "Understanding forms", "Using sessions"];
foreach ($posts as $post) {
echo "<article><h2>$post</h2></article>\n";
}
<article><h2>Learning PHP</h2></article>
<article><h2>Understanding forms</h2></article>
<article><h2>Using sessions</h2></article>
The browser gets only this HTML. It does not know that PHP made it.
The simple rule
Ask one question: does this need to happen on the server or in the browser?
- Reading from a database, checking a password or saving a file must happen on the server, so use PHP.
- Opening a menu, validating a field while the user types or animating something must happen in the browser, so use JavaScript and CSS.
Real projects use both. A form can be checked by JavaScript for a fast message and checked again by PHP, because the browser check can always be bypassed. You will see why in PHP Security.
If you are new to the front-end parts, the HTML course, the CSS course and the JavaScript course are on this site.