PHP — The basics

Guide 01 / 4

PHP glossary

Give meaning to PHP terms by tying them to the recipe mini-blog.

Expected outcome :

Explain the concepts in your own words and recognise them in an example.

Reading
0%

01 · Getting your bearings before you start

Step 4/6 of the “À table !” running project, a recipe mini-blog. Go at your own pace: read, reproduce, modify, then check.

The path and the outcome of this step

Produce a list and a recipe page from PHP arrays, search on the server side and check a form without yet saving any data. The same dataset runs through all six courses: Soupe de carottes (20 min), Salade de tomates (10 min), Tarte aux pommes (45 min).

01. HTML : structurer trois recettes dans une page sémantique.
02. CSS : rendre le blog lisible sur mobile et sur ordinateur.
03. JavaScript : filtrer les recettes sans recharger la page.
04. PHP : produire les pages depuis des tableaux et traiter un formulaire.
05. MySQL : conserver les recettes dans une base relationnelle.
06. PDO : lire et ajouter des recettes depuis PHP avec des requêtes préparées.
Remember :

Each course has four guides: glossary, essentials, workshop and resources. The complete files are at the end of the workshop.

Prerequisites and environment

Have practised HTML, CSS and JavaScript programming basics. Plan for PHP 8.x with the mbstring extension to handle UTF-8 text, a terminal and a browser. Check php -v and php -m. The Python server serves static files but does not run PHP: use PHP's built-in server here.

mini-blog/
  index.html       # HTML, CSS et JavaScript
  style.css        # ajouté à l’étape CSS
  app.js           # ajouté à l’étape JavaScript
  public/          # pages PHP accessibles par HTTP
  includes/        # fonctions, connexion et données PHP
  database/        # scripts SQL
Remember :

Create this working folder separately from the Technipédia course files. The example paths are relative to mini-blog/. Do not replace the HTML page of this platform.

The method: understand, reproduce, transform

Read a sheet, then type its example. Explain what changes on screen. Change one thing at a time and compare with the expected result. Keep a copy of the previous step before moving on.

1. Je lis et je reformule.
2. Je reproduis dans mon mini-blog.
3. Je change une valeur ou une règle.
4. Je vérifie et j’explique le résultat.
Remember :

Short snippets illustrate a concept; only the files announced as complete are to be copied as they are. Fragments are not to be concatenated into a single file.

02 · Understanding the server

The browser receives the result of the PHP program, not its source code.

PHP and the request-response cycle

PHP is a language executed on the server side. A request asks for a URL; PHP reads data, prepares HTML and the server sends it to the browser.

Navigateur → GET /index.php?q=soupe
Serveur → exécution PHP → réponse HTML
Navigateur → affichage du HTML
Remember :

PHP does not directly filter the DOM already displayed: it produces a new response.

PHP variables and types

A variable starts with $. String, integer, boolean, array and null are used to represent the blog's data.

$titre = "Soupe de carottes";
$duree = 20;
$rapide = $duree <= 20;
Remember :

The semicolon ends a statement. $titre and $Titre are two distinct variables.

Indexed array and associative array

An indexed array stores values by number; an associative array links them to named keys. A list of recipes contains associative arrays.

$recette = ['titre' => 'Soupe', 'duree_minutes' => 20];
$recettes = [$recette];
echo $recettes[0]['titre'];

Function and return value

A function groups a processing. Its parameters are the inputs; return gives a value back to the calling code.

function estRapide(int $duree): bool
{
    return $duree <= 20;
}
var_dump(estRapide(10));

Superglobal

Some variables are available in every scope. $_GET holds the URL parameters; $_POST the fields of a POST form; $_SERVER information about the request.

$q = $_GET['q'] ?? '';
$methode = $_SERVER['REQUEST_METHOD'];
Remember :

The request data is external to the program: check its shape and value.

03 · Separating responsibilities

Validation, display and persistence solve three different problems.

GET and POST

GET suits a search that can be shared in a URL. POST carries a request body for a submission. A write must not be triggered by a simple GET link.

<form method="get" action="index.php">…</form>
<form method="post" action="proposer.php">…</form>
Remember :

POST does not encrypt the data; HTTPS provides encryption in transit on a deployed site.

Validation and HTML escaping

Validating checks that a piece of data respects the blog's rules. Escaping on output turns special characters into harmless ones so that text does not become an HTML tag.

$titre = '<em>Soupe</em>';
echo htmlspecialchars($titre, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
Remember :

Validation does not replace escaping. HTML escaping does not replace a prepared SQL query.

Persistence

A variable in this workshop exists for the duration of one request. Changing the array in memory does not save it for the next one. MySQL will provide durable, shared storage.

POST /proposer.php → validation → confirmation
Requête suivante → aucune nouvelle recette enregistrée

HTTP codes 400 and 404

400 signals a malformed request, for example an invalid identifier. 404 indicates a missing resource, such as a recipe that does not exist.

http_response_code(404);
exit('Recette introuvable.');
Remember :

The workshop checks the identifier before looking for the recipe.