Editing PHP If-Abfragen, Schleifen, Funktionen
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 6: | Line 6: | ||
$var2 = 7; | $var2 = 7; | ||
if($var1 < 5 || $var2 == 7) { | if($var1 < 5 || $var2 == 7) { | ||
print "Variable 1 ist kleiner als 5 oder | print "Variable 1 ist kleiner als 5 oder Variabel 2 beträgt 7"; | ||
} | } | ||
?> | ?> | ||
</pre> | </pre> | ||
=====Beispiel 2 ===== | =====Beispiel 2 ===== | ||
<pre> | <pre> | ||
Line 27: | Line 26: | ||
<pre> | <pre> | ||
<?php | <?php | ||
if($ | if($geschlecht == "m") { | ||
print "<h1>Hallo Herr ".$ | print "<h1>Hallo Herr ".$nachname."!</h1>\n"; | ||
} elseif ($ | } elseif ($geschlecht == "w") { | ||
print "<h1>Hallo Frau ".$ | print "<h1>Hallo Frau ".$nachname."!</h1>\n"; | ||
} else { | } else { | ||
print "Hi du"; | print "Hi du"; | ||
Line 40: | Line 39: | ||
<pre> | <pre> | ||
<?php | <?php | ||
if($ | if($geschlecht == "m") { | ||
print "<h1>Hallo Herr ".$ | print "<h1>Hallo Herr ".$nachname."!</h1>\n"; | ||
} else { | } else { | ||
print "<h1>Hallo Frau ".$ | print "<h1>Hallo Frau ".$nachname."!</h1>\n"; | ||
} | } | ||
?> | ?> | ||
Line 53: | Line 52: | ||
<pre> | <pre> | ||
<?php | <?php | ||
for ($i= 0; | for ($i= 0; i < 10; i++) { | ||
print "<p>Zehnmal Hallo</p>\n"; | print "<p>Zehnmal Hallo</p>\n"; | ||
} | } | ||
Line 73: | Line 72: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$ | $produkt[0]['Produktname'] = "Semmel"; | ||
$ | $produkt[0]['Preis'] = 1.99; | ||
$ | $produkt[0]['Anzahl'] = 6; | ||
$ | $produkt[1]['Produktname'] = "Kornspitz"; | ||
$ | $produkt[1]['Preis'] = 1.49; | ||
$ | $produkt[1]['Anzahl'] = 0; | ||
$ | $produkt[2]['Produktname'] = "Laugenbrezl"; | ||
$ | $produkt[2]['Preis'] = 2.99; | ||
$ | $produkt[2]['Anzahl'] = 8; | ||
$i = 0; | $i = 0; | ||
/*count returned die Länge des Arrays (ähnlich wie .length)*/ | /*count returned die Länge des Arrays (ähnlich wie .length)*/ | ||
while ($i < count($ | while ($i < count($produkt)) { | ||
if($ | if($produkt[$i]['Anzahl'] > 0) { | ||
print "<p> | print "<p>Produkt: " .$produkt[$i]['Produktname']. " Preis: " | ||
.$ | .$produkt[$i]['Preis']. " Euro</p>\n"; | ||
} | } | ||
$i++; | $i++; | ||
Line 95: | Line 94: | ||
?> | ?> | ||
</pre> | </pre> | ||
====Foreach Schleifen==== | ====Foreach Schleifen==== | ||
Line 100: | Line 100: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$ | $produkt = array( | ||
1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23 | 1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23 | ||
); | ); | ||
foreach ($ | foreach ($produkt as $item) { | ||
print $item . "<br>\n"; | print $item . "<br>\n"; | ||
} | } | ||
Line 112: | Line 112: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$ | $produkt[0]['Produktname'] = "Bohrmaschine"; | ||
$ | $produkt[0]['Preis'] = 45.99; | ||
$ | $produkt[0]['Anzahl'] = 6; | ||
$ | $produkt[1]['Produktname'] = "Kreissäge"; | ||
$ | $produkt[1]['Preis'] = 79.99; | ||
$ | $produkt[1]['Anzahl'] = 0; | ||
$ | $produkt[2]['Produktname'] = "Bandschleifer"; | ||
$ | $produkt[2]['Preis'] = 85.99; | ||
$ | $produkt[2]['Anzahl'] = 11; | ||
foreach($ | foreach($produkt as $inhalt) { | ||
if($ | if($inhalt['Anzahl'] > 0) { | ||
print "<p>Produkt: " .$ | print "<p>Produkt: " .$inhalt['Produktname']. | ||
" Preis: " .$ | " Preis: " .$inhalt['Preis']. " Euro.</p>\n"; | ||
} | } | ||
} | } | ||
Line 134: | Line 134: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$ | $produkt[0]['Produktname'] = "Bohrmaschine"; | ||
$ | $produkt[0]['Preis'] = 45.99; | ||
$ | $produkt[0]['Anzahl'] = 6; | ||
$ | $produkt[1]['Produktname'] = "Kreissäge"; | ||
$ | $produkt[1]['Preis'] = 79.99; | ||
$ | $produkt[1]['Anzahl'] = 0; | ||
$ | $produkt[2]['Produktname'] = "Bandschleifer"; | ||
$ | $produkt[2]['Preis'] = 85.99; | ||
$ | $produkt[2]['Anzahl'] = 11; | ||
foreach($ | foreach($produkt as $ebene1) { | ||
foreach($ | foreach($ebene1 as $ebene2) { | ||
print $ | print $ebene2. "<br>\n"; | ||
} | } | ||
print "<br>"; | print "<br>"; | ||
Line 155: | Line 155: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$ | $produkt[0]['Produktname'] = "Bohrmaschine"; | ||
$ | $produkt[0]['Preis'] = 45.99; | ||
$ | $produkt[0]['Anzahl'] = 6; | ||
$ | $produkt[1]['Produktname'] = "Kreissäge"; | ||
$ | $produkt[1]['Preis'] = 79.99; | ||
$ | $produkt[1]['Anzahl'] = 0; | ||
$ | $produkt[2]['Produktname'] = "Bandschleifer"; | ||
$ | $produkt[2]['Preis'] = 85.99; | ||
$ | $produkt[2]['Anzahl'] = 11; | ||
foreach($ | foreach($produkt as $ebene1) { | ||
foreach($ | foreach($ebene1 as $name => $ebene2) { | ||
print $name . ": " .$ | print $name . ": " .$ebene2. "<br>\n"; | ||
} | } | ||
print "<br>"; | print "<br>"; | ||
Line 176: | Line 176: | ||
<pre> | <pre> | ||
// BPS 1 | // BPS 1 | ||
function | function begruessung() { | ||
print "Guten Morgen"; | print "Guten Morgen"; | ||
} | } | ||
begruessung(); | |||
// BPS 2 | // BPS 2 | ||
function | function begruessung($text) { | ||
print $text; | print $text; | ||
} | } | ||
begruessung("Guten Morgen"); | |||
// BSP 3 | // BSP 3 | ||
function | function begruessung($text, $ansprache) { | ||
print $text . " " . $ | print $text . " " . $ansprache; | ||
} | } | ||
$ | $gruss = "Guten Morgen"; | ||
$ | $leser = "Herr Müller"; | ||
begruessung($gruss, $leser); | |||
</pre> | </pre> | ||
Line 201: | Line 201: | ||
<pre> | <pre> | ||
<?php | <?php | ||
function | function verdoppelung($wert) { | ||
$ | $wert = $wert*2; | ||
return $ | return $wert; | ||
} | } | ||
print | print verdoppelung(5); | ||
/* oder */ | /* oder */ | ||
function | function verdoppelungAlternative($wert) { | ||
return ($ | return ($wert*2); | ||
} | } | ||
print | print verdoppelungAlternative(5); | ||
?> | ?> | ||
</pre> | </pre> | ||
Line 217: | Line 217: | ||
<pre> | <pre> | ||
<?php | <?php | ||
function | function verdoppelung_quadrat($wert) { | ||
$ | $doppelt = $wert*2; | ||
$ | $quadrat = $wert*$wert; | ||
$ | $ergebnis = array( | ||
'Verdopplung' => $ | 'Verdopplung' => $doppelt, | ||
'Quadrat' => $ | 'Quadrat' => $quadrat | ||
); | ); | ||
return $ | return $ergebnis; | ||
} | } | ||
$ | $rueckgabewert = verdoppelung_quadrat(3); | ||
$ | $verdoppelter_wert = $rueckgabewert['Verdopplung']; | ||
$ | $wert_zum_quadrat = $rueckgabewert['Quadrat']; | ||
print "Der doppelte Wert dieser Zahl beträgt " . $ | print "Der doppelte Wert dieser Zahl beträgt " . $verdoppelter_wert .".<br>\n"; | ||
print "Das Quadrat dieser Zahl beträgt " . $ | print "Das Quadrat dieser Zahl beträgt " . $wert_zum_quadrat .".<br>\n"; | ||
?> | ?> | ||
</pre> | </pre> | ||
Line 236: | Line 236: | ||
====Eine Funktion in das PHP-Programm einbinden==== | ====Eine Funktion in das PHP-Programm einbinden==== | ||
<pre> | <pre> | ||
File: | File: verdoppelung.php | ||
<?php | <?php | ||
function | function verdoppelung($wert){ | ||
return ($ | return ($wert * 2); | ||
} | } | ||
?> | ?> | ||
Line 246: | Line 246: | ||
<pre> | <pre> | ||
File: | File: Hauptfile | ||
<?php | <?php | ||
include(" | include("verdoppelung.php"); | ||
print | print verdoppelung(4); | ||
?> | ?> | ||
</pre> | </pre> |