EMZETT.
Login

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

Kapitel 15 von 15 im Kurs PHP (Abschnitt „Nachschlagen“). Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Typen und Umwandlung

TypBeispielPrüfen / Umwandeln
int42is_int, (int), intval
float3.14is_float, (float)
string"Text"is_string, (string)
booltrue(bool); falsy: 0 "" "0" [] null
array[1, 2], ["a" => 1]is_array, (array)
nullnullis_null, isset, ??
objectnew Xinstanceof, is_object

Arrays: Auswahl

AufgabeFunktion
Größecount
Anhängen/Entfernen[] =, array_push, array_pop, array_shift, array_unshift
Suchenin_array, array_search, array_key_exists, isset
Sortierensort rsort asort arsort ksort krsort usort uasort uksort
Umformenarray_map, array_filter, array_reduce, array_column
Kombinierenarray_merge, +, array_combine, array_slice, array_splice
Schlüssel/Wertearray_keys, array_values, array_flip, array_unique, array_reverse
Mathearray_sum, array_product, min, max, range

Strings: Auswahl

AufgabeFunktion
Längestrlen, mb_strlen
Teilstringsubstr, mb_substr, strstr
Suchenstrpos, str_contains, str_starts_with, str_ends_with
Ersetzenstr_replace, preg_replace, strtr
Trennen/Verbindenexplode, implode, str_split
Formatsprintf, number_format, str_pad, ucfirst, trim
Regexpreg_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 : $z

Klassen-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

EinstellungProduktion
display_errorsOff
log_errorsOn
error_reportingE_ALL
opcache.enable1
memory_limit, max_execution_timeangepasst
session.cookie_httponly, session.cookie_secure1

Häufige Fehler

FehlerUrsache / Lösung
”Headers already sent”Ausgabe vor header() oder session_start(); auch Leerzeichen vor <?php
0 == "a"== wandelt um, nutze ===
Array wird nicht verändertforeach mit Kopie; nutze &$x oder Schlüssel
Undefined array keyisset, ?? verwenden
Umlaute zerhacktmb_*-Funktionen und UTF-8
XSS durch Ausgabehtmlspecialchars
SQL-InjectionPrepared 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 und htmlspecialchars sind 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

Weiter im Kurs

Zurück: Modernes PHP (8.x)

Alle Kapitel: PHP im Überblick