Why PHP?
PHP is a server-side language: the code runs on the server, which sends HTML (or JSON) back to the browser. It powers a very large part of the web, from WordPress to Laravel and Symfony. This course uses PHP 8.5, a modern, typed and fast version.
You can run a script in the terminal with php script.php, or through a web server. Let’s start with the terminal.
A first script
<?php
// Single-line comment
# Another single-line comment style
/* Comment
on several lines */
echo "Hello world!\n"; // echo prints text; \n = line break
Two rules to remember: a PHP file starts with <?php, and every statement ends with a semicolon. In a file that contains only PHP, you don’t close the ?> tag at the end (this avoids sending stray whitespace).
Variables
A variable starts with $. You don’t declare its type: it is inferred from the value.
<?php
$prenom = "Inès"; // string
$age = 29; // int (integer)
$taille = 1.68; // float (decimal number)
$estAbonne = true; // bool (true / false)
$parrain = null; // null: no value
var_dump($age); // int(29)
var_dump($taille); // float(1.68)
echo gettype($prenom); // string
Names are case-sensitive: $age and $Age are two different variables. camelCase is generally used ($prixTotal).
For a value that must never change, you declare a constant: const TAUX_TVA = 0.2; (no $).
Strings
<?php
$prenom = "Inès";
$ville = 'Lyon';
// Concatenation with the dot
echo 'Hello ' . $prenom . '!' . "\n";
// Interpolation: only double quotes replace variables
echo "Hello $prenom, you live in $ville.\n";
echo 'Hello $prenom'; // prints literally: Hello $prenom
// Curly braces to remove ambiguity
$produit = "book";
echo "Two {$produit}s bought\n"; // Two books bought
// A few useful functions
echo strlen("PHP") . "\n"; // 3 (number of bytes)
echo mb_strlen("Inès") . "\n"; // 4 (number of characters, handles accents)
echo strtoupper("php") . "\n"; // PHP
echo number_format(1234.5, 2, ',', ' '); // 1 234,50
Careful: strlen counts bytes. With accented characters (UTF-8), use the mb_ functions such as mb_strlen.
Comparing: == or ===?
<?php
var_dump(5 == "5"); // bool(true) : type conversion before comparison
var_dump(5 === "5"); // bool(false) : different types (int vs string)
var_dump(0 == "a"); // bool(false) since PHP 8
== converts types before comparing, which can be surprising. === checks the value and the type: it is the comparison to prefer.
Key takeaways
<?php, semicolon,echoto print.- Variables as
$name, inferred types,var_dump()to inspect. - Double quotes = interpolation; dot = concatenation.
- Use
===by default.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Welcome message
Declare
$prenomequal to"Lina"and$ageequal to31(an integer). Then build$messageusing interpolation to get exactly:Bonjour Lina, tu as 31 ans.$prenom = ; $age = ; $message = ;Price including tax calculation
From
$prixHt = 50.0and the constantTAUX_TVA = 0.2, compute$prixTtc(float). Then create$affichagewithnumber_formatto get60,00 €(decimal comma, then a space and the euro symbol).const TAUX_TVA = 0.2; $prixHt = 50.0; $prixTtc = ; $affichage = ;Strict comparison
What does
var_dump(10 === "10");return?- bool(true)
- bool(false)
- A fatal error
- int(10)
End-of-lesson quiz
What is the concatenation operator in PHP?
- +
- .
- &
- ,
What does
echo 'Total : $total';print if $total equals 12?- Total : 12
- Total : $total
- An error
Which function displays the type and value of a variable?
- echo
- var_dump
- gettype
The rest of the course
- Conditions, match and Loops15 min
- Indexed and Associative Arrays18 min
- Typed and Reusable Functions18 min
- PHP and the Web: Forms and Basic Security20 min