First Steps: Syntax, Variables and Types

First lesson of the PHP Fundamentals course, free to read: about 15 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Write and run a first PHP script from the command line
  • Declare variables and tell the scalar types apart (int, float, string, bool, null)
  • Build strings by concatenation and by interpolation

Basic syntaxVariables and typesStrings

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, echo to 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.

  1. CodeDifficulty 1Server sandbox1 hint

    Welcome message

    Declare $prenom equal to "Lina" and $age equal to 31 (an integer). Then build $message using interpolation to get exactly: Bonjour Lina, tu as 31 ans.

    $prenom = ;
    $age = ;
    $message = ;
  2. CodeDifficulty 1Server sandbox2 hints

    Price including tax calculation

    From $prixHt = 50.0 and the constant TAUX_TVA = 0.2, compute $prixTtc (float). Then create $affichage with number_format to get 60,00 € (decimal comma, then a space and the euro symbol).

    const TAUX_TVA = 0.2;
    $prixHt = 50.0;
    $prixTtc = ;
    $affichage = ;
  3. Multiple choiceDifficulty 11 hint

    Strict comparison

    What does var_dump(10 === "10"); return?

    • bool(true)
    • bool(false)
    • A fatal error
    • int(10)

End-of-lesson quiz

  1. What is the concatenation operator in PHP?

    • +
    • .
    • &
    • ,
  2. What does echo 'Total : $total'; print if $total equals 12?

    • Total : 12
    • Total : $total
    • An error
  3. Which function displays the type and value of a variable?

    • echo
    • var_dump
    • print
    • gettype

The rest of the course

  1. Conditions, match and Loops15 min
  2. Indexed and Associative Arrays18 min
  3. Typed and Reusable Functions18 min
  4. PHP and the Web: Forms and Basic Security20 min

PHP Fundamentals · Beginner · 2 h 26 min