db & is ii - ss 2020smiffy.de/db-is-ii/folien/v21-php-teil1.pdf · the first official php...
TRANSCRIPT
Andreas Schmidt PHP Einführung 1/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Objektrelationale
Abbildungsschicht
Andreas Schmidt PHP Einführung 2/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Einbettung von SQL in Programmiersprache
• zwei Welten:
• deskriptive Anfragesprache SQL (mengenwertige Ergebnisse)
• imperative Programmiersprache (tupelorientiert)
• Einbindung am Beispiel von PHP
Rec. on?
Andreas Schmidt PHP Einführung 3/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Einbettung von SQL in Programmiersprache
• Inhalt des Blockes
• Architektur webbasierter Anwendungen
• Einführung in PHP (PHP7)
• Grundlagen • Einbindung von SQL in PHP • Objektorientierung
• Objektrelationale Abbildungsschicht (CRUD-Interface)
• Model View Controler Pradigma in PHP
Andreas Schmidt PHP Einführung 4/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
GUI (MVC)
OO-Applikationslogik
Objekt-relationales-Mapping
Relationales DBMS
Views
Controlers
Geschäftsobjekte
Architektur von Informationssystemen
Andreas Schmidt PHP Einführung 5/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
• serverseitige Skriptsprache zur Erstellung dynamischer Webseiten (webbasierter
Informationssysteme)
• PHP Code wird in HTML-Seite eingebettet
• PHP ist Open Source, Cross plattform
• Sehr gute Libraries für Zugriff auf Datenbanken
• PHP Extension and Application Repository (PEAR) ist Zentrale Stelle für frei ver-
fügbare Erweiterungen/Libraries (inkl. automatisierte Installation)
• Syntax ähnlich C oder Perl
• PHP Code befindet sich in besonders gekennzeichneten Stellen in einer Seite:
<?php ... ?>
(Kurzformen: <? ... ?> bzw. <?= ... ?> für <?php echo ... ?>)
• Einsatz auch als Cross-Plattform Scripting language !!
• PHP bietet Möglichkeit HTTP-Header Informationen zu verschicken (MVC-Pattern)
was ist PHP ?
Andreas Schmidt PHP Einführung 6/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
first example
<html> <body> <h1>My first PHP program:</h1> <?php // PHP Codefragment: echo "Simple hardcoded
calculator"; $a = 10; $b = 32; $c = $a + $b;
/* really, not very difficult, or ?
*/ ?> <p> <?php echo $a ?> + <?php echo $b ?> =
<?php echo $c ?></body>
</html>
Andreas Schmidt PHP Einführung 7/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Variablen
• Typen:
• Integer
• String/Zeichenkette
• Fließkomma-Zahlen
• Array
• Dictionary
• Object
• Variablen beginnen in PHP mit
einem $-Zeichen
• Beispiele:
$a = 131;$d ="I'm a string";$a = '... and I converted to a string';
$friends = array('Ingo','Andrea');
$c[2] = 12;$c[] = "store me at the next free
position"; // like $c[3]
$country['I'] = 'Italy';$country['F'] = "France";
$obj = new Film('Roots', 1986);
Andreas Schmidt PHP Einführung 8/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Datentyp Array
• Array
$namen = array('Klaus','Inge', 'Sandra','Ingo');
$hobbies = array();
echo $namen[0];array_push($hobbies,
'Extremjojo');
echo count($hobbies);echo array_pop($hobbies);echo count($hobbies);
Funktionen für Arrays
• array_pop($arr)
// entfernt letztes Element
// und gibt es zurück
• array_push($arr, $element)// fügt hinten $element ein
• in_array($nadel, $heuhaufen)// prüft, ob $nadel in $heuhaufen
// existiert
• sort($arr) // sortiert die Elemente in $arr
• count($arr)// gibt Anzahl Elemnte in $arr zurück
Andreas Schmidt PHP Einführung 9/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Datentyp assoziatives Array
• Assoziative Arrays
(Schlüssel-Wert Paare)
$laender['GR'] = 'Griechenland';$laender['E'] = 'Spanien';$laender['AND'] = 'Andorra';
$andere_laender = array('D'=>'Deutschland','F'=>'Frankreich','I'=>'Italien');
echo $laender['E'];
$kuerzel = array_keys($laender);$laendernamen = array_values($laender);
Funktionen für assoziative Arrays
• array_keys($dic)
// liefert alle Schlüssel als Array
• array_values($dic)
// liefert alle Werte als Array
• asort($dic) // sortiert die Elemente in $dic
// entsprechend ihrer Werte
• ksort($dic)// sortiert nach Schlüssel
• array_key_exists($key, $dic)// existiert $key in $dic ?
Andreas Schmidt PHP Einführung 10/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Operatoren
• Arithmetische Operatoren$a + $b Addition$a - $b Subtraktion.$a * $b Multiplikation.$a / $b Division.$a % $b Modulo (Rest einer
Division)
• Zuweisungsoperatoren$a =3;$a += 5; // wie $a = $a + 5;$b = "Hallo ";$c = "Halli ";$d = $c . $b; // $d enthält "Halli Hallo"$b .= "Du!"; // wie $b = $b . "Du!";
• Vergleichs-Operatoren$a == $b$a != $b$a < $b$a <= $b$a > $b$a >= $b
• Inkrement- bzw. Dekrementierungsoperator++$a // Prä-Inkrement$a++ // Post-Inkrement--$a // Prä-Dekrement$a-- // Post-Dekrement
• Logische Operatoren$a and $b bzw. $a && $b // Und$a or $b bzw. $a || $b // Oder$a xor $b // Entweder Odernot $a bzw. ! $a // Nicht
Andreas Schmidt PHP Einführung 11/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
String Operationen
• code
$a = 5;$b = "five";$c = "$b ($a)";$d = '$b ($a)';$e = "\$a = $a";
$h = "simple quote ' in a text";$i = 'in a text " in a text';$j = "single ' and double quotes \" in text";
$k = "Backslash \\ in a text ";
• output
$a: 5 $b: five $c: five (5) $d: $b ($a) $e: $a = 5
$h: simple quote ' in a text $i: in a text " in a text $j: single ' and double quotes " in text
$k: Backslash \ in a text
• Resume:
• Variablen innerhalb von " ... " werden evaluiert, innerhalb von ’... ’ nicht !!
• \ - Zeichen (Backslash) zur Ausgabe spezieller Zeichen mit Sonderbedeutung ( ',", $, \)
Andreas Schmidt PHP Einführung 12/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Kontrollstrukturen (1)
• if
if ($a > $b) echo "a ist grösser als b";
if ($a > $b) { echo "a ist grösser als b"; $b = $a;
}
• if - else
if ($a > $b) { echo "a ist grösser als b"; } else { echo "a ist NICHT grösser als b";
}
• if - elseif - ... - else
if ($a > $b) { echo "a ist grösser als b"; } elseif ($a == $b) { echo "a ist gleich b"; } else { echo "a ist kleiner als b";
}
Andreas Schmidt PHP Einführung 13/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Kontrollstrukturen (2)
• while-Schleife
$i =1;while ($i <= 10) { echo $i++; }
• do - Schleife
$i =1;do { echo $i; $i = $i + 1;} while ($i<=10);
• for - Schleife
for ($i = 1; $i <= 10; $i++) { echo $i;}
• break-Anweisung
$i =1;while (True) { { if ($i > 10) { break; } echo $i; $i++;
}
Andreas Schmidt PHP Einführung 14/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Kontrollstrukturen (3)
• foreach Schleife
• mit Arrays:
$namen = array('Klaus','Inge','Sandra','Ingo');
foreach ($namen as $name) { echo "$name <br>";}
• Ausgabe (HTML):
Klaus
Inge
Sandra
Ingo
• mit Dictionary
$laender['GR'] = 'Griechenland';$laender['E'] = 'Spanien';$laender['AND'] = 'Andorra';
foreach ($laender as $kennung=>$name) {
echo "$name ($kennung)<br>";
}
• Ausgabe (HTML):
Griechenland (GR)
Spanien(E)
Andorra (AND)
Andreas Schmidt PHP Einführung 15/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Exceptions
<?phptry {
print "Hier ist noch alles in Ordnung\n";throw new Exception('Hier knallts!!');print "das hier wird niemals ausgeführt\n";
} catch (Exception $e) {print "Fehler gefunden: {$e->getMessage()}\n";
} finally {print "Der Code hier wird auf jeden Fall ausgeführt\n";
}
• Ausgabe:
$ c:/xampp/php/php.exe exception-example.phpHier ist noch alles in OrdnungFehler gefunden: Hier knallts!!Der Code hier wird auf jeden Fall ausgeführt
Andreas Schmidt PHP Einführung 16/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Funktionen in PHP
• Beispieldefinition:
function mach_was($arg_1,$arg_2,...,$arg_n) {
// sau komplexe sache passiere hier// ...return $irgendwas;
}
function square ($x) {return $x * $x;
}
• Default Funktionsparameter
function abrunden($zahl,
$nachkommastellen = 0)
{ ... }
• Aufruf:
abrunden(121.4234); // --> 121
abrunden(121.4234, 2); // --> 121.42
<?phpfunction square ($x) {
return $x * $x; }?>
<html> <body> <?php $v = 3; $z = 12; ?> Das Quadrat von <?php echo $v ?> ist
<?php echo square($v) ?> <p> Das Quadrat von <?php echo $z ?> ist
<?php echo square($z) ?> </body></html>
Andreas Schmidt PHP Einführung 17/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Ausgabe
Andreas Schmidt PHP Einführung 18/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Modularisierung
• Mit dem include (...) - Statement können
externe Dateien in PHP-Seiten eingebun-
den werden.
• die externen Seiten können enthalten:
• HTML-Code (z.B. header/footer)
• PHP-Code (einbinden von zentral
definierten Funktionen/Klassen/Co-
defragmenten)
• PHP und HTML-Code
Andreas Schmidt PHP Einführung 19/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Quellcode
<?php include ('header.php'); ?>
<h1>PHP Conference</h1>
The first official PHP Conference, part of the O'Reilly Open Source
Convention, brought the community together for sessions and tutorials designed to explore
and strengthen PHP in the open source space. The conference was at the Sheraton San Diego
Hotel and Marina in San Diego, California between July 23-27, 2001. Some presentations,
you can see online:
<ul>
<li> Rasmus Lerdorf's PHP - Spinal Injection
<li> Thomas Arzt on Making efficient use of Oracle18c thru Apache and PHP 7
<li> Angela Petters and Frankie C. Krox with PHP-PDO
<li> Jon Parise on The PHP Extension and Application Repository
</ul>
You can also download some papers from the O'Reilly FTP site
<?php include ('footer.php'); ?>
Andreas Schmidt PHP Einführung 20/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Quellcode
• header.php
<html> <head> <title>...</title> </head> <body> <hr>
<table> <tr> <td><img src="waterfall.jpg">
</td> <td>
•footer.php
</td></tr>
</table><hr>
Webmaster: <a href="mailto:[email protected]">
Andreas Schmidt</a>
</body></html>
Andreas Schmidt PHP Einführung 21/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Beispiel
• Erstellung einer HTML Seite, welche das
kleine 1 x 1 ausgibt:
• HTML Tabellencode:
<table border="1">
<tr>
<td>1</td>...<td>9</td>
</tr>
...
<tr>
<td>9</td>...<td>81</td>
</tr>
</table>
Andreas Schmidt PHP Einführung 22/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Beispiel
• Code:
<html>
<head>
<title>PHP Beispiel 3</title>
</head>
<body>
<table border="1">
<?php $n = 10;
for ($i=1; $i < $n; $i++) { ?>
<tr>
<?php for ($j=1; $j < $n; $j++) {
echo "<td>".($i*$j)."</td>";
}
?>
</tr>
<?php } ?>
</table>
</body>
</html>
gibt eine Zeile aus
gibt n Zeilen aus
Andreas Schmidt PHP Einführung 23/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
parameter passing (POST Mode)
• formular
<html> <body> <form method="post" action="addition.php"> Please enter two numbers: <p> Number 1: <input type="text"
name="number_1"> <p> Number 2: <input type="text"
name="number_2"> <p> <input type="submit" value="add"> </form> </body></html>
Andreas Schmidt PHP Einführung 24/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
calculation program (addition.php)
<!-- Script: addition.php --><?php $num1 = $_REQUEST['number_1']; $num2 = $_REQUEST['number_2']; if (empty($num1))
die('Parameter number_1 not defined');
if (empty($num2))die('Parameter number_2 not defined');
?><html> <body> <?php $sum = $num1 + $num2; ?> The sum out of <?php echo $num1 ?> and <?php echo $num2 ?> is <b><?php echo $sum ?></b> <p> <a href="addition_mask.php">back</a> </body></html>
Andreas Schmidt PHP Einführung 25/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
calculation program (output)
Andreas Schmidt PHP Einführung 26/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
parameter passing (GET Mode)
Andreas Schmidt PHP Einführung 27/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
PHP-Run Modes
• Als Webserver-Modul
• Aufruf über Browser: http://localhost/wwwenv/test.php
• Zeilenumbruch mittels "<br>" (HTML-Zeilenumbruch)
• Standalone:
• c:\xampp\php\php.exe test.php
• Zeilenumbruch: "\n"
• Interaktiver Modus (nur wenn PHP mit der --with-readline Option kompiliert wurde)
• Aufruf: c:\Programme\php\php.exe -a
• Interaktive Shell mit php > - Prompt, bei der Kommandos direkt eingegeben und ausgeführt werden können
• Details dazu: http://php.net/manual/de/features.commandline.interactive.php
• Unterstützt Autovervollständigung, Kommandohistorie
• Beispiel:
Andreas Schmidt PHP Einführung 28/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Beispiel Interaktiver Modus
C:\Users\scan0004>h:\schmidt\php7\php.exe -aInteractive shell
php > echo Date("H:i:s");14:53:42php > echo Date("m.d.y");11.03.17php > function demo($str) [return strtoupper($str); }PHP Parse error: syntax error, unexpected '[', expecting '{' in php shell codeon line 1php > function demo($str) {return strtoupper($str); }php > echo demo("das ziel ist im weg!");DAS ZIEL IST IM WEG!php > exitC:\Users\scan0004>
Andreas Schmidt PHP Einführung 29/29
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - SS 2020
Literatur zu PHP
• Online Buch: http://www.professionelle-softwareentwicklung-mit-php5.de/
• PHP - Ein praktischer Einstieg; Ulrich Günther; 2. Auflage 2004; O’Reilly; EUR19.00
• PHP Kochbuch; David Sklar & Adam Trachtenberg; Deutsche Übersetzung von Jörg Staudemeyer & Ulrich Speidel; 1. Auflage Juli 2003; ISBN 3-89721-351-6;O’Reilly; Seiten 656; EUR 44.00
• PHP - kurz & gut, 2. Auflage; Rasmus Lerdorf; Deutsche Übersetzung von Raimund Eimann & Lars Schulten; 2. Auflage März 2003; ISBN 3-89721-251-X; O’Reilly; Seiten 144; EUR 8.90
• PHP Online Manual: http://www.php.net/docs.php
• HTML Tutorial: http://de.selfhtml.org/
• PEAR - PHP Extension and Application Repository - http://pear.php.net/