Kurz: _GET _POST _SERVER _COOKIE _SESSION _FILES $_ENV, FILE DIR LINE FUNCTION CLASS, PHP_EOL PHP_INT_MAX PHP_VERSION M_PI.
Teil des Kurses PHP
Typen und Umwandlung
| Typ | Beispiel | Prüfen / Umwandeln |
|---|---|---|
int | 42 | is_int, (int), intval |
float | 3.14 | is_float, (float) |
string | "Text" | is_string, (string) |
bool | true | (bool); falsy: 0 "" "0" [] null |
array | [1, 2], ["a" => 1] | is_array, (array) |
null | null | is_null, isset, ?? |
object | new X | instanceof, is_object |
Arrays: Auswahl
| Aufgabe | Funktion |
|---|---|
| Größe | count |
| Anhängen/Entfernen | [] =, array_push, array_pop, array_shift, array_unshift |
| Suchen | in_array, array_search, array_key_exists, isset |
| Sortieren | sort rsort asort arsort ksort krsort usort uasort uksort |
| Umformen | array_map, array_filter, array_reduce, array_column |
| Kombinieren | array_merge, +, array_combine, array_slice, array_splice |
| Schlüssel/Werte | array_keys, array_values, array_flip, array_unique, array_reverse |
| Mathe | array_sum, array_product, min, max, range |
Strings: Auswahl
| Aufgabe | Funktion |
|---|---|
| Länge | strlen, mb_strlen |
| Teilstring | substr, mb_substr, strstr |
| Suchen | strpos, str_contains, str_starts_with, str_ends_with |
| Ersetzen | str_replace, preg_replace, strtr |
| Trennen/Verbinden | explode, implode, str_split |
| Format | sprintf, number_format, str_pad, ucfirst, trim |
| Regex | preg_match, preg_match_all, preg_split, preg_replace_callback |
Kontrollstrukturen
if () {} elseif () {} else {} match ($x) { 1 => "a", default => "b" }
for ($i = 0; $i < 10; $i++) {} foreach ($arr as $k => $v) {}
while () {} do {} while (); break; continue; return;
try {} catch (A | B $e) {} finally {}
$a ?? $b $a ??= $b $a?->b $a ?: $b $x ? $y : $zKlassen-Spickzettel
final class A extends B implements I, J {
use T;
public const X = 1;
public function __construct(private readonly int $x = 0) {}
public static function neu(): static {}
abstract protected function f(): void;
}
enum S: string { case A = 'a'; }
interface I { public function f(): int; }
trait T { public function g() {} }Nützliche Superglobals und Konstanten
$_GET $_POST $_SERVER $_COOKIE $_SESSION $_FILES $_ENV, __FILE__ __DIR__ __LINE__ __FUNCTION__ __CLASS__, PHP_EOL PHP_INT_MAX PHP_VERSION M_PI.
php.ini und Betrieb
| Einstellung | Produktion |
|---|---|
display_errors | Off |
log_errors | On |
error_reporting | E_ALL |
opcache.enable | 1 |
memory_limit, max_execution_time | angepasst |
session.cookie_httponly, session.cookie_secure | 1 |
Häufige Fehler
| Fehler | Ursache / Lösung |
|---|---|
| ”Headers already sent” | Ausgabe vor header() oder session_start(); auch Leerzeichen vor <?php |
0 == "a" | == wandelt um, nutze === |
| Array wird nicht verändert | foreach mit Kopie; nutze &$x oder Schlüssel |
Undefined array key | isset, ?? verwenden |
| Umlaute zerhackt | mb_*-Funktionen und UTF-8 |
| XSS durch Ausgabe | htmlspecialchars |
| SQL-Injection | Prepared Statements |
Weiterlernen
- Offizielle Dokumentation php.net/manual/de und “PHP: The Right Way” (phptherightway.com)
- Laravel- oder Symfony-Tutorials für größere Anwendungen
- PHPUnit, PHPStan, Composer im eigenen Projekt einsetzen
Merke
===, strikte Typen undhtmlspecialcharssind deine wichtigsten Gewohnheiten- Datenbank immer über Prepared Statements, Passwörter mit
password_hash - Composer + PSR-4 gliedern Projekte, Tests und statische Analyse sichern Qualität
Quiz zur Selbstkontrolle
Welcher Operator vergleicht Wert und Typ?
- === (richtig)
- ==
- =
- <=>
Wie vermeidest du die Meldung "Headers already sent"?
- Vor header() und session_start() nichts ausgeben (richtig)
- display_errors aktivieren
- echo verwenden
- exit() nutzen
Welche Einstellung gehört in Produktion auf Off?
- display_errors (richtig)
- log_errors
- opcache.enable
- error_reporting
Weiter im Kurs
Zurück: Modernes PHP (8.x)
Alle Kapitel: PHP im Überblick