PHP If-Abfragen, Schleifen, Funktionen

From Coders.Bay Wiki
Revision as of 09:12, 14 April 2022 by Titzi (talk | contribs)
Jump to navigation Jump to search

Entscheidungen durch if-Abfragen

Beispiel 1
<?php
$var1 = 2;
$var2 = 7;
if($var1 < 5 || $var2 == 7) {
  print "Variable 1 ist kleiner als 5 oder Variabel 2 beträgt 7";
}
?>
Beispiel 2
<?php
$var1 = 2;
$var2 = 7;
if(!($var2 < 5)) {
  print "Variable 2 ist nicht kleiner als 5";
}
?>


Else und elseif

Beispiel 1
<?php
if($geschlecht == "m") {
  print "<h1>Hallo Herr ".$nachname."!</h1>\n";
} elseif ($geschlecht == "w") {
  print "<h1>Hallo Frau ".$nachname."!</h1>\n";
} else {
  print "Hi du";
}
?>
Beispiel 2
<?php
if($geschlecht == "m") {
  print "<h1>Hallo Herr ".$nachname."!</h1>\n";
} else {
  print "<h1>Hallo Frau ".$nachname."!</h1>\n";
}
?>


While und For-Schleife

Beispiel 1
<?php
for ($i= 0; i < 10; i++) {
  print "<p>Zehnmal Hallo</p>\n";
}
?>
Beispiel 2
<?php
$i = 0;
while ($i < 10) {
  $i++;
  print $i*$i."<br>\n";
}
?>
Beispiel 3
<?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;

/*count returned die Länge des Arrays (ähnlich wie .length)*/

while ($i < count($produkt)) {
  if($produkt[$i]['Anzahl'] > 0) {
    print "<p>Produkt: " .$produkt[$i]['Produktname']. " Preis: "
      .$produkt[$i]['Preis']. " Euro</p>\n";
  }
  $i++;
}
?>

Foreach Schleifen

Beispiel 1
<?php
$produkt = array(
  1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23
);
foreach ($produkt as $item) { 
  print $item . "<br>\n";
}
?>
Beispiel 2
<?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($produkt as $inhalt) {
  if($inhalt['Anzahl'] > 0) {
    print "<p>Produkt: " .$inhalt['Produktname'].
      " Preis: " .$inhalt['Preis']. " Euro.</p>\n";
  }
}
?>
Beispiel 3
<?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($produkt as $ebene1) {
  foreach($ebene1 as $ebene2) {
    print $ebene2. "<br>\n";
  }
  print "<br>";
}
?>
Beispiel 4
<?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($produkt as $ebene1) {
  foreach($ebene1 as $name => $ebene2) {
    print $name . ": " .$ebene2. "<br>\n";
  }
  print "<br>";
}
?>

Funktionen in PHP

// BPS 1
function begruessung() {
  print "Guten Morgen";
}
begruessung();

// BPS 2
function begruessung($text) {
  print $text;
}
begruessung("Guten Morgen");

// BSP 3
function begruessung($text, $ansprache) {
  print $text . " " . $ansprache;
}
$gruss = "Guten Morgen";
$leser = "Herr Müller";
begruessung($gruss, $leser);