In httpd.conf:
AddType application/x-httpd-php3 .phtml
AddType application/x-httpd-php3-source .phps Dateiendung .php3 oder .phtml, in der Webserverconfig einstellbar.
Adabas | dBase | Empress | FilePro |
Informix | InterBase | mSQL | MySQL |
Oracle | PostgreSQL | Solid | Sybase |
Unix dbm | Velocis |
Aktuell sind PHP 3.0.15 (stabil) und PHP 4.0 Beta 4
<html> <body> <script language="php"> echo date("D, d M Y"); </script> </body> </html>
Typ | Cast-Operator | Beispiel |
---|---|---|
Ganzzahliger Wert | (int) oder (integer) | $PI=4; |
Fliesskommazahl | (real), (double), (float) | $SQRT2D2=0.7 |
Array, Feld | (array) | $c[0]=1;$c[1]=13;$c[2]="hallo"; |
Objekt | (object) | - |
Zeichenkette | - | $b="Dies ist ein Text"; |
define("MEINEKONSTANTE", 3.14);
Verschiedene Konstanten vorbelegt:__FILE__, __LINE__, ...
$a=10; if($a==10) { print "a ist 10"; } elseif($a>10) { print "a ist grösser 10"; } else { printf "a ist kleiner 10"; }
while($a <= 15) { print $a++; } do { print $a--; } while ($a > 1); for($a=1; $a<=10; $a++) { print $a; }
function meinefunktion1() { return 42; } function meinefunktion2($parm1, $parm2) { return $parm1+$parm2; }
class Selbermachen { var $a; function mache1() { $this->a=42; } function mache2() { print "In Klasse \"Selbermachen\" a="; print $this->a; } } $a=33; $C=new Selbermachen; $C->mache1(); $C->mache2(); print "Global: a=$a";Klassenvererbung:
class tochter extends mutter { ... }
CREATE TABLE anwalt ( nr int(11) DEFAULT '0' NOT NULL auto_increment, name varchar(100), str varchar(100), stadt varchar(100), PRIMARY KEY (nr) );
INSERT INTO anwalt VALUES (1,'Hans Meier','Kleine Str 17','11111 Berlin'); INSERT INTO anwalt VALUES (2,'Klaus Schmidt','Grosse Aue 17','22222 Hamburg'); INSERT INTO anwalt VALUES (3,'Irmgard Apfel','Kamener Str 22','80000 München');
<ul> <li><a href="insm.html">Daten eingeben</a></li> <li><a href="allm.phtml">Alle Daten ausgeben</a></li> <li><a href="sucm.html">Daten suchen</a></li> </ul>
<form action="insm1.phtml" method=get> <table border> <tr> <td>Name:</td> <td><input type=text name="INAME"></td> </tr> <tr> <td>Strasse:</td> <td><input type=text name="ISTRASSE"></td> </tr> <tr> <td>Stadt:</td> <td><input type=text name="ISTADT"></td> </tr> </table> <input type=submit value="Eingeben"> </form>
<script language="php"> $connection=mysql_connect("localhost", "elug"); if(!mysql_select_db("lamp", $connection)) { print "FEHLER1: Konnte nicht auf die DB zugreifen"; } else { $sql="INSERT INTO anwalt VALUES(\"\", \"$INAME\", \"$ISTRASSE\", \"$ISTADT\")"; if(!mysql_query($sql, $connection)) { print "FEHLER2: Konnte nicht in die DB schreiben"; } else { print "Die Daten sind drinne ! :-)"; } } mysql_close($connection); </script>
<script language="php"> $connection=mysql_connect("localhost", "elug"); if(!mysql_select_db("lamp", $connection)) { print "FEHLER1: Konnte nicht auf die DB zugreifen"; } else { $sql="SELECT * FROM anwalt"; if(!($res=mysql_query($sql, $connection))) { print "FEHLER2: Konnte nicht auf die DB zugreifen"; } else { print "Alle Anwälte:"; print "<p><table border>"; while( ($data=mysql_fetch_row($res)) ) { print "<tr>"; print "<td>$data[0]</td>"; print "<td>$data[1]</td>"; print "<td>$data[2]</td>"; print "<td>$data[3]</td>"; print "</tr>"; } print "</table>"; } } mysql_close($connection); </script>
<form action="sucm1.phtml" method=get> <table border> <tr> <td>Suchen nach Stadt:</td> <td><input type=text name="SSTADT"></td> </tr> </table> <input type=submit value="Suchen"> </form>
<script language="php"> $connection=mysql_connect("localhost", "elug"); if(!mysql_select_db("lamp", $connection)) { print "FEHLER1: Konnte nicht auf die DB zugreifen"; } else { $sql="SELECT name, str, stadt FROM anwalt WHERE stadt LIKE \"%$SSTADT%\""; if(!($res=mysql_query($sql, $connection))) { print "FEHLER2: Konnte nicht auf die DB zugreifen"; } else { print "Gefundene Anwälte:"; print "<p><table border>"; while( ($data=mysql_fetch_row($res)) ) { print "<tr>"; print "<td>$data[0]</td>"; print "<td>$data[1]</td>"; print "<td>$data[2]</td>"; print "</tr>"; } print "</table>"; } } mysql_close($connection); </script>
<script language="php"> $connection=pg_connect("host=localhost user=elug dbname=lamp"); if(!$connection) { print "FEHLER1: Konnte ncht auf die DB zugreifen"; } else { $sql="SELECT * FROM anwalt"; if(!($result=pg_exec($connection, $sql))) { print "FEHLER2: Konnte nicht auf die DB zugreifen"; } else { print "<p> <p><center>Alle Anwälte"; print "<p><table border>"; for($i=0; $i<pg_NumRows($result); $i++) { print "<tr>"; $row=pg_fetch_row($result, $i); print "<td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>"; print "</tr>"; } print "</table></center>"; } } pg_close($connection); </script>