Editing PHP Einführung - Erste Programmierschritte

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 22: Line 22:


====Dein erstes PHP Programm====
====Dein erstes PHP Programm====
*Erstelle eine '''.php''' Datei
*PHP wird geöffnet mit '''<?php und geschlossen mit '''?>'''
*PHP wird geöffnet mit '''<?php und geschlossen mit ?>'''
*Dazwischen ist der Programmcode
*Dazwischen ist der Programmcode
*'''print''' oder '''echo''' erzeugt eine Ausgabe am Bildschirm
*'''print''' oder '''echo''' erzeugt eine Ausgabe am Bildschirm
Line 149: Line 148:


*Zeichenketten und Variablen können zusammen ausgegeben werden.
*Zeichenketten und Variablen können zusammen ausgegeben werden.
*In JavaScript, Java,... fügt man sie mit einem Plus zusammen (+)
*In JS fügt man sie mit einem Plus zusammen (+)
*In PHP fügt man sie mit einem Punkt zusammen (.)
*In PHP fügt man sie mit einem Punkt zusammen (.)
*Additionen, Multiplikationen, Divisionen, Modulo funktionieren im gleichen Schema wie in anderen Programmiersprachen
*Additionen, Multiplikationen, Divisionen, Modulo funktionieren im gleichen Schema wie in JS


<pre>
<pre>
Line 159: Line 158:


// Text mit einer Variablen erfassen
// Text mit einer Variablen erfassen
$textContent= "Meine erste Variable";
$textbaustein = "Meine erste Variable";
print $textContent;
print $textbaustein;




// Zahlen als Variable abspeichern
// Zahlen als Variable abspeichern
/* Variablen können neben Strings auch numerische oder boolsche Werte besitzen */
/* Variablen können neben Strings auch numerische oder boolsche Werte besitzen */
$int= 3;
$ganzeZahl = 3;
$float= 5.39281;
$kommaZahl = 5.39281;
$boolean = true;
$boolean = true;
print "Ganze Zahl: " . $int. "<br>Kommazahl: " . $float. "<br>Boolean: " . $boolean;
print "Ganze Zahl: " . $ganzeZahl . "<br>Kommazahl: " . $kommaZahl . "<br>Boolean: " . $boolean;




// Additionen, Multiplikationen, usw
// Additionen, Multiplikationen, usw
$inStore= 5;
$bestand = 5;
print "<p>Verfügbare Artikel: " . $inStore. "</p>\n";
print "<p>Verfügbare Artikel: " . $bestand . "</p>\n";
$inStore= $inStore - 1;
$bestand = $bestand - 1;
/* Kurzschreibweise: $inStore--; */
/* Kurzschreibweise: $bestand--; */
print "<p>Verfügbare Artikel: " . $inStore. "</p>\n";
print "<p>Verfügbare Artikel: " . $bestand . "</p>\n";


?>
?>
</pre>
</pre>


====Numerisches Array====
====Numerisches Array====
Line 185: Line 185:
<?php
<?php
/* Array deklarieren */
/* Array deklarieren */
$product = array();
$produkt = array();
$product[0] = 1;
$produkt[0] = 1;
$product[1] = "Roggensemmel";
$produkt[1] = "Roggensemmel";
$product[2] = 0.79;
$produkt[2] = 0.79;
$product[3] = "Bioweckerl vom feinsten";
$produkt[3] = "Bioweckerl vom feinsten";
$product[4] = 50;
$produkt[4] = 50;


foreach($product as $productInfo) {
foreach($produkt as $info) {
   print "<p>" . $productInfo. "</p>\n";
   print "<p>" . $info . "</p>\n";
}
}
?>
?>
Line 201: Line 201:
<?php  
<?php  
/* oder: kürzere Schreibweise */
/* oder: kürzere Schreibweise */
$productAlternative = array(1, "Roggensemmel", 0.79, "Bioweckerl vom feinsten", 50);
$produktAlternative = array(1, "Roggensemmel", 0.79, "Bioweckerl vom feinsten", 50);


foreach($productAlternative as $productInfo) {
foreach($produktAlternative as $info) {
   print "<p>" . $productInfo. "</p>\n";
   print "<p>" . $info . "</p>\n";
}
}




// Einzelnen Wert ausgeben
// Einzelnen Wert ausgeben
print "<p>" . $product[4] . "</p>\n";
print "<p>" . $produkt[4] . "</p>\n";
?>
?>
</pre>
</pre>
Line 219: Line 219:
<?php
<?php
/* Array deklarieren */
/* Array deklarieren */
$product = array();
$produkt = array();
$product['itemnumber'] = 1;
$produkt['Artikelnummer'] = 1;
$product['productname'] = "Roggensemmel";
$produkt['Produktname'] = "Roggensemmel";
$product['price'] = 0.79;
$produkt['Preis'] = 0.79;
$product['description'] = "Bioweckerl vom feinsten";
$produkt['Beschreibung'] = "Bioweckerl vom feinsten";
$product['amount'] = 50;
$produkt['Anzahl'] = 50;
foreach($product as $key => $info) {
foreach($produkt as $i => $info) {
   print "<p>" . $key . ": " . $info . "</p>\n";
   print "<p>" . $i . ": " . $info . "</p>\n";
}
}
?>
?>
<hr>
<hr>
<?php
<?php
/* Alternative Schreibweise */
/* Alternative Schreibweise */
$productAlternative = array(
$produktAlternative = array(
   'itemnumber' => 1,
   'Artikelnummer' => 1,
   'productname' => "Roggensemmel",
   'Produktname' => "Roggensemmel",
   'price' => 0.79,
   'Preis' => 0.79,
   'description' => "Bioweckerl vom feinsten",
   'Beschreibung' => "Bioweckerl vom feinsten",
   'amount' => 50
   'Anzahl' => 50
);
);
 
foreach($produktAlternative as $i => $info) {
foreach($productAlternative as $key => $info) {
   print "<p>" . $i . ": " . $info . "</p>\n";
   print "<p>" . $key . ": " . $info . "</p>\n";
}
}




// Array Ausgabe einzelnen Wertes
// Array Ausgabe einzelnen Wertes
print "<p>" . $productAlternative["price"] . "</p>\n";
print "<p>" . $produktAlternative["Preis"] . "</p>\n";
?>
</pre>
 
====Mehrdimensionales Array====
 
<pre>
<?php
/* Array deklarieren */
$productRange= array();
$productRange[0]['itemnumber'] = 1;
$productRange[0]['productname'] = "Roggensemmel";
$productRange[0]['price'] = 0.79;
$productRange[1]['itemnumber'] = 2;
$productRange[1]['productname'] = "Kornspitz";
$productRange[1]['price'] = 0.59;
$productRange[2]['itemnumber'] = 3;
$productRange[2]['productname'] = "Semmel";
$productRange[2]['price'] = 0.99;
 
/* Debugging mit var_dump um zu sehen was in dem Array drin ist */
var_dump($productRange);
print "<hr>";
 
/* Ausgabe einzelner Position */
print "<p>" . $productRange[2]['productname'] . "</p>\n";
print "<hr>";
 
/* Ausgabe des gesamten Arrays */
foreach($productRange as $array) {
  foreach($array as $key => $entry) {
    print "<p>" . $key . ": " . $entry . "</p>\n";
  }
}
?>
?>
</pre>
</pre>

Please note that all contributions to Coders.Bay Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see CB Wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)