PHP ยท Les 2
Variabelen en datatypes
$var, string, int, float, bool, null, type juggling en operatoren ยท 25 min
Variabelen met $
In PHP beginnen alle variabelen met een dollarteken ($). Er is geen aparte declaratie nodig โ je wijst gewoon een waarde toe.
<?php
$naam = "Jan";
$leeftijd = 20;
$gemiddeld = 7.5;
$isActief = true;
$niets = null;
echo $naam; // Jan
echo $leeftijd; // 20
Naamgevingsregels
- โ
Mag letters, cijfers en
_bevatten - โ Mag niet beginnen met een cijfer
- โ
Is case-sensitief:
$naamโ$Naam - โ
Conventie:
camelCasevoor variabelen
โ ๏ธ Let op verschil met JS
In JS gebruik je let of const voor variabelen. In PHP gebruik je gewoon $ โ geen keyword nodig. PHP heeft ook geen const-variabelen (wel define() en klasse-constanten).
Datatypes
PHP heeft 8 ingebouwde datatypes. De vier meest gebruikte zijn string, int, float en bool.
string
$s = "Hallo"; $s = 'ook string'; gettype($s); // "string"
int
$n = 42; $n = -7; gettype($n); // "integer"
float
$f = 3.14; $f = 1.0; gettype($f); // "double"
bool
$b = true; $b = false; gettype($b); // "boolean"
null
$x = null; gettype($x); // "NULL" is_null($x); // true
array
$a = [1, 2, 3]; gettype($a); // "array" // โ Les 5
๐ Komt terug in Symfony
In Symfony-entities en controllers gebruik je type declarations: string $naam, int $leeftijd, ?string $optioneel = null. Die ? voor het type betekent "nullable" โ de waarde mag ook null zijn.
String operatoren
<?php
$voornaam = "Jan";
$achternaam = "de Vries";
// Concatenatie: punt (.)
$volledig = $voornaam . " " . $achternaam;
echo $volledig; // Jan de Vries
// Concatenatie-toewijzing: .=
$bericht = "Welkom, ";
$bericht .= $voornaam;
echo $bericht; // Welkom, Jan
// Interpolatie in dubbele quotes
echo "Hallo $voornaam!"; // Hallo Jan!
echo "Hallo {$voornaam}!"; // Hallo Jan! (met accolades, duidelijker)
echo "Naam: {$volledig}.\n";
Handige stringfuncties
<?php
strlen("Hallo"); // 5
strtoupper("hallo"); // HALLO
strtolower("HALLO"); // hallo
trim(" spaties "); // "spaties"
str_replace("a","@","Jan"); // "J@n"
substr("Hallo", 1, 3); // "all"
strpos("Hallo", "ll"); // 2
โ ๏ธ Let op verschil met JS
In JS: "Hallo " + naam of `Hallo ${naam}`. In PHP: "Hallo " . $naam of "Hallo $naam". JS-methoden als .toUpperCase() zijn in PHP losse functies: strtoupper($s).
Rekenoperatoren
<?php
$a = 10;
$b = 3;
echo $a + $b; // 13 โ optellen
echo $a - $b; // 7 โ aftrekken
echo $a * $b; // 30 โ vermenigvuldigen
echo $a / $b; // 3.333... โ delen
echo $a % $b; // 1 โ modulo (rest)
echo $a ** $b; // 1000 โ machtsverheffing (PHP 5.6+)
// Verkorte notatie
$x = 5;
$x += 3; // $x = 8
$x -= 1; // $x = 7
$x *= 2; // $x = 14
$x++; // $x = 15 (post-increment)
++$x; // $x = 16 (pre-increment)
Wiskundige functies
<?php round(3.7); // 4 floor(3.9); // 3 ceil(3.1); // 4 abs(-5); // 5 max(3, 7, 1); // 7 min(3, 7, 1); // 1 sqrt(16); // 4.0 rand(1, 100); // willekeurig getal 1โ100
BTW berekening โ praktisch voorbeeld
<?php $prijsExcl = 100.00; $btwTarief = 0.21; $btwBedrag = $prijsExcl * $btwTarief; $prijsIncl = $prijsExcl + $btwBedrag; echo "Excl. BTW: โฌ" . number_format($prijsExcl, 2) . "\n"; echo "BTW (21%): โฌ" . number_format($btwBedrag, 2) . "\n"; echo "Incl. BTW: โฌ" . number_format($prijsIncl, 2) . "\n";
Type juggling en casting
PHP is loosely typed: het past types automatisch aan op basis van context. Dit heet type juggling.
<?php
// Type juggling โ automatisch
echo "5" + 3; // 8 โ string "5" wordt int
echo "5 km" + 3; // 8 โ getal aan het begin wordt gebruikt
echo true + true; // 2 โ bool wordt 0 of 1
// gettype() โ type opvragen
$x = "42";
echo gettype($x); // string
// Casting โ expliciet omzetten
$s = "42";
$n = (int) $s; echo gettype($n); // integer
$f = (float) "3.14"; echo $f; // 3.14
$b = (bool) 0; var_dump($b); // bool(false)
$s = (string) 100; echo $s; // "100"
// settype() โ type direct wijzigen
$val = "3.14";
settype($val, "float");
var_dump($val); // float(3.14)
Falsy waarden in PHP
// De volgende waarden zijn "falsy" in PHP: false, 0, 0.0, "", "0", [], null // Alles anders is truthy: true, 1, -1, "string", [0], "false"
โ ๏ธ Let op verschil met JS
PHP en JS zijn beide loosely typed, maar de regels verschillen. In JS is "5" + 3 = "53" (string). In PHP is "5" + 3 = 8 (int). PHP neigt naar numerieke conversie; JS neigt naar string-concatenatie.
Sandbox
Bereken de eindprijs inclusief BTW en toon alle variabelen met var_dump.
// output
Klik op Uitvoeren...
Kennischeck
Les 2 afronden
Ga door naar voorwaarden โ