PHP Skripts

String / Operatoren / if Abfragung / Schleifen / Dateisystem / Arrays / Formulare / Datum /
Function / PDF / eMail / IP
/ Cookies / Sessions / PHP Weiterleitung / JAVA

Allgemein Wichtig, Dateiname muss mit Endung .php enden
Beispiel dokument.php

Befehl Datentypen
Code

<?php

$a = 1;
$b = 2.35;
$c = "ABC";
$typea = gettype($a);
$typeb = gettype($b);
$typec = gettype($c);
echo "Datentyp von Variable a = " . "$typea" . "<br>";
echo "Datentyp von Variable b = " . "$typeb" . "<br>";
echo "Datentyp von Variable c = " . "$typec" . "<br>";
?>

Ausgabe Datentyp von Variable a = integer
Datentyp von Variable b = double
Datentyp von Variable c = string

Befehl Umwandlung Dateitypen
Code

<?php
$a = 1;
echo ("Alt: " . gettype($a) . "<br>");
settype($a, "double");
echo ("Neu: " . gettype($a). "<br>");
?>

Ausgabe Alt: integer
Neu: double

 

PHP "String" funktionen  
Befehl echo
Code <?php
echo "Text";
?>
Ausgabe Text

Befehl echo mit Variable
Code <?php
$text = "Texteingabe";
echo $text;
?>
Ausgabe Texteingabe

Befehl echo mit Variable (komplex)
Code <?php
$text = "Text ";
$text2 = "miteinander ";
$text3 = "verknüpfen";
echo $text . $text2 . $text3;
?>
Ausgabe Text miteinander verknüpfen

Befehl strlen
Code

<?php
$text = "Wie viel Zeichen hat dieser String?";
$i = strlen($text);
echo $i;
?>

Ausgabe 35

Befehl strtolower
Code

<?php
$text = "Alles Umwandeln in KLEINBUCHSTABEN";
$abc = strtolower($text);
echo $abc;
?>

Ausgabe alles umwandeln in kleinbuchstaben

Befehl strtoupper
Code

<?php
$text = "Alles Umwandeln in Grossbuchstaben";
$abc = strtoupper($text);
echo $abc;
?>

Ausgabe ALLES UMWANDELN IN GROSSBUCHSTABEN

Befehl ucfirst
Code

<?php
$text = "nur das erste Zeichen eines Strings in Grossbuchstaben umwandeln";
$abc = ucfirst($text);
echo $abc;
?>

Ausgabe Nur das erste Zeichen eines Strings in Grossbuchstaben umwandeln

Befehl ucwords
Code

<?php
$text = "jeder Anfangsbuchstaben jedes Wortes im String grossschreiben";
$abc = ucwords($text);
echo $abc;
?>

Ausgabe Jeder Anfangsbuchstaben Jedes Wortes Im String Grossschreiben

Befehl explode
Code

<?php
$text = "Jedes Wort in diesem String wird in ein Array konvertiert";
$abc = explode(" ", $text);
print_r($abc);
?>

Ausgabe Array ( [0] => Jedes [1] => Wort [2] => in [3] => diesem [4] => String [5] => wird [6] => in [7] => ein [8] => Array [9] => konvertiert )

Befehl implode / join
Code

<?php
$myarray = array('Dieses', 'Aaray', 'wird', 'in', 'einen', 'String', 'konvertiert');
$abc = implode(" ", $myarray);
print_r($abc);
?>

Ausgabe Dieses Aaray wird in einen String konvertiert
Code

<?php
$myarray = array('Dieses', 'Aaray', 'wird', 'in', 'einen', 'String', 'konvertiert');
$abc = join("-", $myarray);
print_r($abc);
?>

Ausgabe Dieses-Aaray-wird-in-einen-String-konvertiert

Befehl explode / implode
Code

<?php
$text = "Jedes Wort in diesem String wird in ein Array konvertiert und zurück in einen String";
$abc = explode(" ", $text);
print_r($abc);
echo "<br>";
$text = implode(" ", $abc);
print_r($text);
?>

Ausgabe Array ( [0] => Jedes [1] => Wort [2] => in [3] => diesem [4] => String [5] => wird [6] => in [7] => ein [8] => Array [9] => konvertiert [10] => und [11] => zurück [12] => in [13] => einen [14] => String )
Jedes Wort in diesem String wird in ein Array konvertiert und zurück in einen String

Befehl substr (Zuschneiden von Strings)
Code

<?php
$text = "Von diesem Text wird ab dem 8 Zeichen ausgegeben";
$abc = substr($text, 8);
echo $abc;
?>

Ausgabe em Text wird ab dem 8 Zeichen ausgegeben
Code

<?php
$text = "Von diesem Text wird ab dem 8 Zeichen von rechts ausgegeben";
$abc = substr($text, -8);
echo $abc;
?>

Ausgabe sgegeben
Code

<?php
$text = "Von diesem Text wird ab dem 5 Zeichen, die 10 nächsten Zeichen ausgegeben";
$abc = substr($text, 5, 10);
echo $abc;
?>

Ausgabe iesem Text
Code

<?php
$text = "Von diesem Text wird ab dem 5 bis 15 Zeichen von rechts ausgegeben";
$abc = substr($text, -15, -5);
echo $abc;
?>

Ausgabe chts ausge
Code

<?php
$text = "Von diesem Text wird ab dem 13 Zeichen von rechts 5 Zeichen ausgegeben";
$abc = substr($text, -13, 5);
echo $abc;
?>

Ausgabe en au
Code

<?php
$text = "Von diesem Text wird ab dem 5 Zeichen bis zum 2 Zeichen von rechts ausgegeben";
$abc = substr($text, 5, -2);
echo $abc;
?>

Ausgabe iesem Text wird ab dem 5 Zeichen bis zum 2 Zeichen von rechts ausgegeb

Befehl str_pad (anfügen von Zeichen in Strings)
Code

<?php
$text = "AB";
$abc = str_pad($text, 8, "+");
echo $abc;
?>

Ausgabe AB++++++
Code

<?php
$text = "AB";
$abc = str_pad($text, 8, "+", STR_PAD_LEFT);
echo $abc;
?>

Ausgabe ++++++AB
Code

<?php
$text = "AB";
$abc = str_pad($text, 8, "+0", STR_PAD_RIGHT);
echo $abc;
?>

Ausgabe AB+0+0+0
Code

<?php
$text = "AB";
$abc = str_pad($text, 8, "+", STR_PAD_BOTH);
echo $abc;
?>

Ausgabe +++AB+++

Befehl str_replace (ersetzen von Werten)
Code

<?php
$text = "Dieser Text mit Underlines kennzeichnen!";
$abc = str_replace(' ', '_', $text);
echo $abc;
?>

Ausgabe Dieser_Text_mit_Underlines_kennzeichnen!

Befehl strrev (Umdrehen eines Strings)
Code

<?php
$text = "Dieser Text wird umgedreht!";
$abc = strrev($text);
echo $abc;
?>

Ausgabe !therdegmu driw txeT reseiD

Befehl str_shuffle (Druchmisschen eines Strings)
Code

<?php
$text = "Dieser Text wird zufällig durchgemischt!";
$abc = str_shuffle($text);
echo $abc;
?>

Ausgabe cigäel tieflcmusixrihseurhgTzDdt! edwr

Befehl htmlentities
Code

<?php
$text = "Sonderzeichen \n wird \n in HTML Code \n umgewandelt (ä,ü,ö,&,+)";
$abc = htmlentities($text, ENT_QUOTES);
echo $abc;
?>

Ausgabe Sonderzeichen wird in HTML Code umgewandelt (ä,ü,ö,&,+)

Befehl nl2br
Code

<?php
$text = "Alle \n Zeilenumbrüche \n werden \n in br konvertiert";
$abc = nl2br ($text);
echo $abc;
?>

Ausgabe Alle
Zeilenumbrüche
werden
in br konvertiert

Befehl Zeilenunbrüche entfernen
Code

<?php
$text = "Alle \n Zeilenumbrüche \n werden \n in br konvertiert";
$text =str_replace("\n","",$text);
$text = str_replace("<br \>","",$text);
$text = = str_replace(chr(13), "", $text);
echo $text ;
?>

Ausgabe Alle Zeilenumbrüche werden in br konvertiert

Befehl String Trimmen
Code

<?php
$temp = trim("   vorundnachherlehrzeichen   ");
$text = $temp;
echo $text;
?>

Ausgabe vorundnachherlehrzeichen

 

PHP Operatoren
Befehl Addieren von Zahlen (+)
Code <?php
$zahl1 = 6;
$zahl2 = 10;
$zahl3 = 3;
echo $zahl1 + $zahl2 + $zahl;
?>
Ausgabe 19

Befehl Suptrahieren von Zahlen (-)
Code <?php
$zahl11 = 30;
$zahl12 = 10;
$zahl13 = 3;
echo $zahl11 - $zahl12 - $zah113;
?>
Ausgabe 17

Befehl Multiplizieren von Zahlen (x)
Code <?php
$zahl21 = 6;
$zahl22 = 10;
$zahl23 = 3;
echo $zahl21 * $zahl22 * $zahl23;
?>
Ausgabe 180

Befehl Dividieren von Zahlen (:)
Code <?php
$zahl31 = 60;
$zahl32 = 10;
$zahl33 = 2;
echo $zahl31 / $zahl32 / $zahl33;
?>
Ausgabe 3

Befehl PI
Code <?php
echo pi();
?>
Ausgabe 3.1415926535898

Befehl Runden von Zahlen
Code <?php
$zahl41 = 22;
$zahl42 = 7;
$ergebniss = $zahl41 / $zahl42;
$runden = round($ergebniss,4);
echo $runden;
?>
Ausgabe 3.1429

Befehl Runden von Fr. auf 0.05
Code

<?php
$zahl123 = 22.7234;
$zahl123 = $zahl123 / 5;
$runden1 = round($zahl123,2) * 5;
echo $runden1;
echo "<br>";
echo number_format($runden1, 2) . '&nbsp;CHF';
?>

Ausgabe 22.7
22.70 CHF

Befehl aufrunden auf Ganze Zahlen
Code <?php
$zahl41 = 22;
$zahl42 = 7;
$ergebniss = $zahl41 / $zahl42;
$runden = ceil($ergebniss);
echo $runden;
?>
Ausgabe 4

Befehl abrunden auf Ganze Zahlen
Code <?php
$zahl41 = 22;
$zahl42 = 7;
$ergebniss = $zahl41 / $zahl42;
$runden = floor($ergebniss);
echo $runden;
?>
Ausgabe 3

Befehl Zeichen zählen
Code

<?php
$nummer = "1234";
$zahl = strlen($nummer);
echo $zahl;
?>

Ausgabe 4

Befehl Umwandlung von Grad(360) ins Bogenmass(2xpi)
Code

<?php
$zahl = deg2rad(90);
echo $zahl;
?>

Ausgabe 1.5707963267949
Code

<?php
$zahl = deg2rad(180);
echo $zahl;
?>

Ausgabe 3.1415926535898
Code

<?php
$zahl = deg2rad(270);
echo $zahl;
?>

Ausgabe 4.7123889803847
Code

<?php
$zahl = deg2rad(360);
echo $zahl;
?>

Ausgabe 6.2831853071796

Befehl Winkelfunktionen tan sin cos
Code

<?php
$zahl = sin(deg2rad(30));
echo $zahl;
?>

Ausgabe 0.5
Code

<?php
$zahl = cos(deg2rad(60));
echo $zahl;
?>

Ausgabe 0.5
Code

<?php
$zahl = tan(deg2rad(45));
echo $zahl;
?>

Ausgabe 1

Befehl Winkelfunktionen atan asin acos
Code

<?php
$zahl = rad2deg(asin(0.5));
echo $zahl;
?>

Ausgabe 30
Code

<?php
$zahl = rad2deg(acos(0.5));
echo $zahl;
?>

Ausgabe 60
Code

<?php
$zahl = rad2deg(atan(1));
echo $zahl;
?>

Ausgabe 45

 


PHP if Abfragung
Befehl if
Code <?php
$a = 120;
$b = 100;
if ($a > $b)
echo "a ist grösser als b ";
?>
Ausgabe a ist grösser als b
Code <?php
$a = 20;
$b = 100;
if ($a > $b)
echo "a ist grösser als b";
?>
Ausgabe

Befehl else
Code <?php
$a = 100;
$b = 100;
if ($a == $b) {
print "a ist gleich gross wie b";
} else {
print "stimmt nicht überein";
}
?>
Ausgabe a ist gleich gross wie b
Code <?php
$a = 70;
$b = 100;
if ($a == $b) {
print "a ist gleich gross wie b";
} else {
print "stimmt nicht überein";
}
?>
Ausgabe stimmt nicht überein

Befehl else
Code <?php
$a = 120;
$b = 100;
if ($a > $b) {
print "a ist grösser als b";
} else {
print " a ist NICHT grösser als b";
}
?>
Ausgabe a ist grösser als b
Code <?php
$a = 80;
$b = 100;
if ($a > $b) {
print "a ist grösser als b";
} else {
print " a ist NICHT grösser als b";
}
?>
Ausgabe a ist NICHT grösser als b

Befehl elseif
Code <?php
$a = 120;
$b = 100;
if ($a > $b) {
print "a ist gösser als b";
} elseif ($a == $b) {
echo " a ist gleich b";
} else {
echo " a ist kleiner als b";
}
?>
Ausgabe a ist gösser als b
Code <?php
$a = 100;
$b = 100;
if ($a > $b) {
print "a ist gösser als b";
} elseif ($a == $b) {
echo " a ist gleich b";
} else {
echo " a ist kleiner als b";
}
?>
Ausgabe a ist gleich b
Code <?php
$a = 80;
$b = 100;
if ($a > $b) {
print "a ist gösser als b";
} elseif ($a == $b) {
echo " a ist gleich b";
} else {
echo " a ist kleiner als b";
}
?>
Ausgabe a ist kleiner als b

Befehl xor
Code

<?php
$a = 120;
$b = 100;
$c = 80;
if ($a == $b xor $b == $c xor $a == $c) {
echo "YES";
} else {
echo "NO";
}
?>

Ausgabe NO
Code

<?php
$a = 80;
$b = 100;
$c = 80;
if ($a == $b xor $b == $c xor $a == $c) {
echo "YES";
} else {
echo "NO";
}
?>

Ausgabe YES

Befehl switch
Code

<?php
$gehalt = 3000;

switch ($gehalt) {

case 1000:
echo "Angestellter";
break;

case 2000:
echo "Facharbeiter";
break;

case 3000:
echo "Spezialist";
break;

case 4000:
echo "Manager";
break;

case 5000:
echo "Boss";
break;

}
?>

Ausgabe Spezialist

 

PHP Schleifen
Befehl for schleifen
Code <?php
for ( $i = 0 ; $i <= 5 ; $i ++)
{
echo "Die Zahl ist " . $i ;
echo "<br>" ;
}
?>
Ausgabe Die Zahl ist 0
Die Zahl ist 1
Die Zahl ist 2
Die Zahl ist 3
Die Zahl ist 4
Die Zahl ist 5

Befehl while schleifen
Code <?php
$i = 1;
while ($i <= 10) {
echo $i;
$i++;
}
?>
Ausgabe 12345678910

 

PHP Dateisystem
Befehl auslesen einer Textdatei .txt
Code <?php
$t = fopen("text1.txt","r");
$tex1 = fgets($t,35);
fclose($t);
echo $tex1
?>
Ausgabe
Warning: fopen(text1.txt) [function.fopen.php]: failed to open stream: No such file or directory in /home/fligerc/public_html/fliger/php.php on line 1794

Warning: fgets(): supplied argument is not a valid stream resource in /home/fligerc/public_html/fliger/php.php on line 1795

Warning: fclose(): supplied argument is not a valid stream resource in /home/fligerc/public_html/fliger/php.php on line 1796

Befehl schreiben einer Textdatei .txt
Code <?php
$t2 = fopen("text2.txt","w");
fputs($t2,"neuer Text");
fclose($t2);
?>
Ausgabe

Befehl Löschen einer Datei
Code <?php
$filename = "../Pfad/zur/Datei.txt";
if (file_exists($filename)) {
unlink($filename);
?>

Befehl Kopieren einer Datei
Code <?php
copy("../Pfad/zur/Datei.txt" , "../Pfad/zur/neuen/Datei.txt");
?>

Befehl erstellen eines Ordners
Code <?php
mkdir ("testordner", 0700);
?>
Ausgabe

Befehl löschen eines Ordners
Code <?php
rmdir ("testordner");
?>
Ausgabe

Befehl löschen eines Ordners mit exec
Code <?php
$directory = "../Pfad/zum/Testordner";
$directory = escapeshellarg($directory);
exec("rmdir /s /q $directory");
?>

Befehl File oder Ordner prüfung
Code <?php
$filename = "text1.txt";
if (file_exists($filename)) {
print "Das File $filename ist bereits vorhanden";
} else {
print " Das File $filename ist noch nicht vorhanden";
}
?>
Ausgabe Das File text1.txt ist noch nicht vorhanden
Code <?php
$filename2 = "text3.txt";
if (file_exists($filename2)) {
print "Das File $filename2 ist bereits vorhanden";
} else {
print " Das File $filename2 ist noch nicht vorhanden";
}
?>
Ausgabe Das File text3.txt ist noch nicht vorhanden

Befehl Ordner Inhalt in Array schreiben
Code <?php
$scan = scandir("./temp", 0);
print_r($scan);
?>
Ausgabe
Warning: scandir(./temp) [function.scandir.php]: failed to open dir: No such file or directory in /home/fligerc/public_html/fliger/php.php on line 1986

Warning: scandir() [function.scandir.php]: (errno 2): No such file or directory in /home/fligerc/public_html/fliger/php.php on line 1986

Befehl Ordner Inhalt in Array schreiben und Einzeln ausgeben
Code <?php
$scan = scandir("./temp", 0);
print_r($scan);

echo "<br>";
$inhalt = count($scan);
echo $inhalt;
echo "<br>";
$i = 3;
while ($i <= $inhalt) {
echo $scan[$i];
echo "<br>";
$i = $i + 1;
}
?>
Ausgabe
Warning: scandir(./temp) [function.scandir.php]: failed to open dir: No such file or directory in /home/fligerc/public_html/fliger/php.php on line 2020

Warning: scandir() [function.scandir.php]: (errno 2): No such file or directory in /home/fligerc/public_html/fliger/php.php on line 2020

1

Befehl Ordner Inhalt in Array schreiben und Einzeln ausgeben (rückwerts)
Code <?php
$scan = scandir("./temp", 1);
print_r($scan);

echo "<br>";
$inhalt = count($scan) - 4;
echo $inhalt;
echo "<br>";
$i = 0;
while ($i <= $inhalt) {
echo $scan[$i];
echo "<br>";
$i = $i + 1;
}
?>
Ausgabe
Warning: scandir(./temp) [function.scandir.php]: failed to open dir: No such file or directory in /home/fligerc/public_html/fliger/php.php on line 2065

Warning: scandir() [function.scandir.php]: (errno 2): No such file or directory in /home/fligerc/public_html/fliger/php.php on line 2065

-3

Befehl Verzeichnis Auflisten
Code <?php

$dir = "temp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}

sort($files);
# giebt das ganze Array aus
print_r($files);
echo "<br>";
echo "<br>";

# gibt die einzelnen Textnamen aus
$f = count($files) - 1;

for ($zahl = 2; $zahl <= $f; $zahl++)
{
echo $files[$zahl];
echo "<br>";
}

# gibt die Anzahl Files im Ordner aus
echo "<br>";
$anz = count($files) - 2;
echo "Anzahl Files: " . $anz;
?>

Ausgabe
Warning: opendir(temp) [function.opendir.php]: failed to open dir: No such file or directory in /home/fligerc/public_html/fliger/php.php on line 2123

Warning: readdir(): supplied argument is not a valid Directory resource in /home/fligerc/public_html/fliger/php.php on line 2124

Warning: sort() expects parameter 1 to be array, null given in /home/fligerc/public_html/fliger/php.php on line 2128



Anzahl Files: -2

Befehl Ordnergrösse ausgeben
Code <?php
$path = "../Pfad/zum/Ordner";

function readsize_recursiv($path)
{
$s = 0;

$result[$path] = 0;

$handle = opendir($path);

if ($handle)
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$name = $path . "/" . $file;
if (is_dir($name))
{
$ar = readsize_recursiv($name);
while (list($key, $value) = each ($ar))
{
$s++;
$result[$key] = $value;
}
}
else
{
$result[$path] += filesize($name);
}
}
}
}
closedir($handle);
return $result;
}

$data = readsize_recursiv(".");

$summe = 0;

while (list($key, $value) = each ($data))
{
# echo "$key = $value byte<br>\n";
$summe += $value;
}

#echo "Gesamtgroeße: $summe byte";
$summeMB = $summe / 1000000 ;
$summeMBTot = round($summeMB,3);
echo $summeMBTot . " MB";
?>

Ausgabe 437.531 MB

 

PHP Arrays

Befehl Textfile in Array laden
Code

<?php
$array = file("text3.txt");
?>


Befehl Array Schreiben
Code

<?php
$jahre = array('Januar','Februar','März','April','Mai','Juni');
print_r($jahre);
?>

Ausgabe Array ( [0] => Januar [1] => Februar [2] => März [3] => April [4] => Mai [5] => Juni )

Befehl Bestimmte Werte aus einem Array auslesen
Code

<?php
$jahre = array('Januar','Februar','März','April','Mai','Juni');
echo $jahre[0];
?>

Ausgabe Januar

Befehl 1 Wert aus array löschen
Code

<?php
$jahre = array('Januar','Februar','März','April','Mai','Juni');

$wert = array_shift($jahre);
$wert2 = array_shift($jahre);
$wert3 = array_shift($jahre);

echo "$wert<br>$wert2<br>$wert3";
?>

Ausgabe Januar
Februar
März

Befehl Alle Keys und Werte aus einem Array auslesen
Code

<?php
$jahre = array("1" => "Januar","2" => "Februar", "3" => "März","4" => "April","5" => "Mai");

foreach ($jahre as $key => $wert) {
echo "$key = $wert<br>";
}
?>

Ausgabe 1 = Januar
2 = Februar
3 = März
4 = April
5 = Mai

Befehl 2 Arrays kombinieren / vereinen
Code

<?php
$monate = array("Januar","Februar","März","April","Mai");
$zahlen = array("1", "2", "3", "4", "5");

$neues_array = array_combine($zahlen, $monate);

foreach ($neues_array as $key => $wert) {
echo "$key = $wert<br>";
}
?>

Ausgabe 1 = Januar
2 = Februar
3 = März
4 = April
5 = Mai

Befehl Werte zum Array hinzufügen
Code

<?php
$monate = array("Januar","Februar","März","April","Mai");

array_push($monate, "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");

foreach ($monate as $wert) {
echo "$wert<br>";
}
?>

Ausgabe Januar
Februar
März
April
Mai
Juni
Juli
August
September
Oktober
November
Dezember

Befehl Array werte umkehren und Sortieren
Code

<?php
$jahre = array("2" => "Februar","5" => "Mai", "4" => "April", "1" => "Januar", "3" => "März");

$jahre = array_flip($jahre);

asort($jahre);

foreach ($jahre as $key => $wert) {
echo "$key = $wert<br>";
}
?>

Ausgabe Januar = 1
Februar = 2
März = 3
April = 4
Mai = 5

Befehl Leere Werte im Array entfernen
Code

<?php

$werte = array( "1", "2", " ", "3", "", "4", " ", "5");

for ($i=0 ; $i<=count($werte);$i++) {
if (trim($werte[$i])== "") {
unset ( $werte[$i] );
}
}

foreach ($werte as $key => $wert) {
echo "$key = $wert<br>";
}
?>

Ausgabe 0 = 1
1 = 2
3 = 3
5 = 4
7 = 5


Befehl Textfile in Array laden und anzeigen
Textfile
text3.txt

das
ist
ein
Mehrzeiliger
Text

Code

<?php
$array = file("text3.txt");
print_r($array);
echo "<br>";
echo $array[0];
echo " ";
echo $array[1];
echo " ";
echo $array[2];
echo " ";
echo $array[3];
echo " ";
echo $array[4];
?>

Ausgabe
Warning: file(text3.txt) [function.file.php]: failed to open stream: No such file or directory in /home/fligerc/public_html/fliger/php.php on line 2555



Befehl Abfrage ob wert existiert im Array
Code

<?php
if (array_key_exists('0', $array)) {
$wert = $array[0];
echo "Wert $wert existiert";
} else {
echo "Wert 0 existiert nicht";
}
?>

Ausgabe
Warning: array_key_exists() [function.array-key-exists.php]: The second argument should be either an array or an object in /home/fligerc/public_html/fliger/php.php on line 2591
Wert 0 existiert nicht

Befehl Wert in Array suchen
Code

<?php
$farbe = array (
"rot" => "FF0000",
"gruen" => "00FF00",
"blau" => "0000FF"
);
$result = array_search("0000FF", $farbe);
echo $result;
?>

Ausgabe blau

Befehl Werte Zählen im Array
Code

<?php
$farbe = array (
"rot" => "FF0000",
"gruen" => "00FF00",
"blau" => "0000FF"
);
$result = count($farbe);
echo $result;
?>

Ausgabe 3

Befehl Array in String umwandeln und nach einem Wert durchsuchen
Code

<?php
$stadt = array (
"schweiz" => "bern",
"deutschland" => "berlin",
"frankreich" => "paris",
"italien" => "rom",
"oestereich" => "wien"
);
$liste = implode(" ", $stadt);
echo $liste;

$test = substr_count($liste, "be");
echo "<br>";
echo "be kommt $test mal vor in diesem Array";
?>

Ausgabe bern berlin paris rom wien
be kommt 2 mal vor in diesem Array


 



PHP Formulare
Befehl Aus Formular über POST einlesen
Code

<?php
$nummer = $_POST[nummer];
echo $nummer;
?>


Befehl Aus Formular über GET einlesen
Code

<?php
$nummer = $_GET[nummer];
echo $nummer;
?>


Befehl Daten über Formular als POST senden
Code

<?php
$filesize = $_FILES["file1"]["size"];
$filesizetemp = (rtrim($filesize));
$filesize = $filesizetemp;
$filename = $_FILES["file1"]["name"];

if ($filesize != "0") {
copy($_FILES["file1"]["tmp_name"] , "../Pfad/zur/datei.txt");
echo "$filename" . " erfolgreich kopiert<br>";
} else {
echo "<br>Kein File gefunden<br>";
}
?>

 

 

PHP Datum

Befehl Schlüssel
Code

a
A
d
j
D
I
F
m
M
n
h
H
g
G
i
s
L
T
U
W
y
Y
z
Z

am oder pm
AM oder PM
Tag des Monats, mit führender Null (01-31)
Tag des Monats, ohne führende Null (1-31)
Wochentag Englisch abgekürzt (Thu)
Wochentag Englisch ausgeschrieben (Thursday)
Monat Englisch ausgeschrieben (October)
Monat mit führender Null (01-12)
Monat Englisch abgekürzt (Oct)
Monat ohne führende Null (1-12)
Stunde im 12 Std. Format mit führender Null (01-12)
Stunde im 24 Std. Format mit führender Null (01-24)
Stunde im 12 Std. Format ohne führende Null (1-12)
Stunde im 24 Std. Format ohne führende Null (1-24)
Minuten (00-59)
Sekunden mit führender Null (00-59)
Überprüft ob Datum in einem Schaltjahr liegt und gibt "True" zurück
Ermittelt die Anzahl der Tage des Monats
Zeigt den Begin der Unix Zeitzählung (1.1.1970)
Tag der Woche als Zahl (0 für Sonntag // 6 für Samstag)
Jahr, zweistellig (00-99)
Jahr, vierstellig (2008)
Tag im Jahr (0-365)
Differenz zu einer Zeitzone in Sekunden (-43200 bis +43200)

Befehl Datum
Code

<?php
$monate = date("m");
$tag = date("d");
$jahr = date("Y");
echo "$tag.$monate.$jahr";

echo "<br>";
echo date("D, d.m.Y");
?>

Ausgabe 07.02.2012
Tue, 07.02.2012 1328584502

Befehl Zeit
Code

<?php //Nullpunkt bei 01.01.1970 00:00 Uhr
$zeit = time();
echo $zeit;
echo "<br> M: ";
echo $zeit / 60;
echo "<br>H: ";
echo $zeit / 60 / 60;
echo "<br>T: ";
echo $zeit / 60 / 60 / 24;
echo "<br>M: ";
echo $zeit / 60 / 60 / 24 / 30;
echo "<br>Y: ";
echo $zeit / 60 / 60 / 24 / 365;
?>

Ausgabe 1328584502
M: 22143075.033333
H: 369051.25055556
T: 15377.135439815
M: 512.57118132716
Y: 42.129138191273

 

Function
Befehl Function
Code

<?php
function ausgeben() {
echo "Das ist meine erste Funktion";
}
echo ausgeben();
?>

Ausgabe Das ist meine erste Funktion

Befehl Function
Code

<?php
function ausgeben2($a, $b) {
return $a + $b;
}
echo ausgeben2(10, 20);
?>

Ausgabe 30

Befehl Function
Code

<?php
function quadratwurzel($a) {
return $a * $a;
}
echo quadratwurzel(10);
?>

Ausgabe 100

Befehl Function Kombiniert
Code

<?php
function quadratwurzel2($b) {
return $b * $b;
}

function subtrahieren($c, $b) {
return $c - $b;
}

echo quadratwurzel(10);
echo "<br>";
echo subtrahieren(48, 12);
?>

Ausgabe 100
36



PDF
Befehl PDF schreiben
Code

<?php
$file = fopen("welt.pdf", "w");
$dokument = pdf_open($file);
pdf_begin_page($dokument, 200, 100);
pdf_set_font($dokument, "Verdana", 16, "winansi");
pdf_set_text_pos($dokument, 20, 50);
pdf_show($dokument, "Hallo, Welt!");
pdf_end_page($dokument);
pdf_close($dokument);
fclose($file);
?>

Ausgabe error



E-Mail
Befehl E-Mailer
Code

<?php
$empfaenger = "raphael.h@fliger.ch";
$betreff = "Automatische E-Mail durch php";
$sender = "Raffi";
$sendermail = "raffi@djflow.ch";
$text = "Das ist eine Automatisch generierte E-Mail";
$from = "From: $sender < $sendermail >";
$geklappt = mail($empfaenger, $betreff, $text, $from);
if($geklappt)
echo "Die E-Mail an $empfaenger wurde versandt!";
?>

Ausgabe



IP
Befehl IP Adresse
Code

<?php
$ip=getenv("REMOTE_ADDR");
echo "IP: $ip";
?>

Ausgabe IP: 38.107.179.231




Cookies
Befehl Cookies
Code

<?php // Setcookie immer vor ausgabe vor <html><head>
setcookie("vorname", "Raphael");

echo $_COOKIE['vorname'];
?>

Ausgabe

 

Sessions
Befehl Sessions
Code

//Session Schreiben //// FILE 1 ////
<?php
session_start();
session_register('test');
$_SESSION['test'] = "Hallo Welt";
?>

//Session Auslesen //// FILE 2 ////
<?php
session_register('test');
echo $_SESSION['test'];
?>

Ausgabe


Befehl Login
Code

//User check & Session Schreiben //// FILE 1 ////////////////////////////////////////////////////////
<?php

$user = "mustermann";
$passwort = "1234";

session_start();
session_register(eingeloggt);

if($user == "mustermann" and $passwort == "1234") {
$_SESSION['eingeloggt'] = "True";
} else {
$_SESSION['eingeloggt'] = "False";
}
?>

//Session Auslesen //// FILE 2 ////////////////////////////////////////////////////////
<?php
session_register('eingeloggt');
$eingeloggt = $_SESSION['eingeloggt'];

if($eingeloggt != "True") {
header("Location:start.php");
} else {
echo "Loggen sie sich zuerst ein";
}
?>

 

 

Weiterleitung PHP
Befehl Sessions
Code

<?php
header("Location:start.php");
?>