Difference between revisions of "PHP If-Abfragen, Schleifen, Funktionen"

From Coders.Bay Wiki
Jump to navigation Jump to search
 
(14 intermediate revisions by 3 users not shown)
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 Variabel 2 beträgt 7";
   print "Variable 1 ist kleiner als 5 oder Variable 2 beträgt 7";
}
}
?>
?>
</pre>
</pre>
=====Beispiel 2 =====
=====Beispiel 2 =====
<pre>
<pre>
Line 26: Line 27:
<pre>
<pre>
<?php
<?php
if($geschlecht == "m") {
if($gender == "m") {
   print "<h1>Hallo Herr ".$nachname."!</h1>\n";
   print "<h1>Hallo Herr ".$lastname."!</h1>\n";
} elseif ($geschlecht == "w") {
} elseif ($gender == "w") {
   print "<h1>Hallo Frau ".$nachname."!</h1>\n";
   print "<h1>Hallo Frau ".$lastname."!</h1>\n";
} else {
} else {
   print "Hi du";
   print "Hi du";
Line 39: Line 40:
<pre>
<pre>
<?php
<?php
if($geschlecht == "m") {
if($gender == "m") {
   print "<h1>Hallo Herr ".$nachname."!</h1>\n";
   print "<h1>Hallo Herr ".$lastname."!</h1>\n";
} else {
} else {
   print "<h1>Hallo Frau ".$nachname."!</h1>\n";
   print "<h1>Hallo Frau ".$lastname."!</h1>\n";
}
}
?>
?>
Line 52: Line 53:
<pre>
<pre>
<?php
<?php
for ($i= 0; i < 10; i++) {
for ($i= 0; $i < 10; $i++) {
   print "<p>Zehnmal Hallo</p>\n";
   print "<p>Zehnmal Hallo</p>\n";
}
}
Line 72: Line 73:
<pre>
<pre>
<?php
<?php
$produkt[0]['Produktname'] = "Semmel";
$product[0]['ProductName'] = "Semmel";
$produkt[0]['Preis'] = 1.99;
$product[0]['Price'] = 1.99;
$produkt[0]['Anzahl'] = 6;
$product[0]['Amount'] = 6;
$produkt[1]['Produktname'] = "Kornspitz";
$product[1]['ProductName'] = "Kornspitz";
$produkt[1]['Preis'] = 1.49;
$product[1]['Price'] = 1.49;
$produkt[1]['Anzahl'] = 0;
$product[1]['Amount'] = 0;
$produkt[2]['Produktname'] = "Laugenbrezl";
$product[2]['ProductName'] = "Laugenbrezl";
$produkt[2]['Preis'] = 2.99;
$product[2]['Price'] = 2.99;
$produkt[2]['Anzahl'] = 8;
$product[2]['Amount'] = 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($produkt)) {
while ($i < count($product)) {
   if($produkt[$i]['Anzahl'] > 0) {
   if($product[$i]['Amount'] > 0) {
     print "<p>Produkt: " .$produkt[$i]['Produktname']. " Preis: "
     print "<p>Product: " .$product[$i]['ProductName']. " Price: "
       .$produkt[$i]['Preis']. " Euro</p>\n";
       .$product[$i]['Price']. " Euro</p>\n";
   }
   }
   $i++;
   $i++;
Line 99: Line 100:
<pre>
<pre>
<?php
<?php
$produkt = array(
$product = array(
   1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23
   1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23
);
);
foreach ($produkt as $item) {  
foreach ($product as $item) {  
   print $item . "<br>\n";
   print $item . "<br>\n";
}
}
Line 111: Line 112:
<pre>
<pre>
<?php
<?php
$produkt[0]['Produktname'] = "Bohrmaschine";
$product[0]['ProductName'] = "Bohrmaschine";
$produkt[0]['Preis'] = 45.99;
$product[0]['Price'] = 45.99;
$produkt[0]['Anzahl'] = 6;
$product[0]['Amount'] = 6;
$produkt[1]['Produktname'] = "Kreissäge";
$product[1]['ProductName'] = "Kreissäge";
$produkt[1]['Preis'] = 79.99;
$product[1]['Price'] = 79.99;
$produkt[1]['Anzahl'] = 0;
$product[1]['Amount'] = 0;
$produkt[2]['Produktname'] = "Bandschleifer";
$product[2]['ProductName'] = "Bandschleifer";
$produkt[2]['Preis'] = 85.99;
$product[2]['Price'] = 85.99;
$produkt[2]['Anzahl'] = 11;
$product[2]['Amount'] = 11;


foreach($produkt as $inhalt) {
foreach($product as $content) {
   if($inhalt['Anzahl'] > 0) {
   if($content['Amount'] > 0) {
     print "<p>Produkt: " .$inhalt['Produktname'].
     print "<p>Produkt: " .$content['ProductName'].
       " Preis: " .$inhalt['Preis']. " Euro.</p>\n";
       " Preis: " .$content['Price']. " Euro.</p>\n";
   }
   }
}
}
Line 133: Line 134:
<pre>
<pre>
<?php
<?php
$produkt[0]['Produktname'] = "Bohrmaschine";
$product[0]['ProductName'] = "Bohrmaschine";
$produkt[0]['Preis'] = 45.99;
$product[0]['Price'] = 45.99;
$produkt[0]['Anzahl'] = 6;
$product[0]['Amount'] = 6;
$produkt[1]['Produktname'] = "Kreissäge";
$product[1]['ProductName'] = "Kreissäge";
$produkt[1]['Preis'] = 79.99;
$product[1]['Price'] = 79.99;
$produkt[1]['Anzahl'] = 0;
$product[1]['Amount'] = 0;
$produkt[2]['Produktname'] = "Bandschleifer";
$product[2]['ProductName'] = "Bandschleifer";
$produkt[2]['Preis'] = 85.99;
$product[2]['Price'] = 85.99;
$produkt[2]['Anzahl'] = 11;
$product[2]['Amount'] = 11;
foreach($produkt as $ebene1) {
foreach($product as $level1) {
   foreach($ebene1 as $ebene2) {
   foreach($level1 as $level2) {
     print $ebene2. "<br>\n";
     print $level2. "<br>\n";
   }
   }
   print "<br>";
   print "<br>";
Line 154: Line 155:
<pre>
<pre>
<?php
<?php
$produkt[0]['Produktname'] = "Bohrmaschine";
$product[0]['ProductName'] = "Bohrmaschine";
$produkt[0]['Preis'] = 45.99;
$product[0]['Price'] = 45.99;
$produkt[0]['Anzahl'] = 6;
$product[0]['Amount'] = 6;
$produkt[1]['Produktname'] = "Kreissäge";
$product[1]['ProductName'] = "Kreissäge";
$produkt[1]['Preis'] = 79.99;
$product[1]['Price'] = 79.99;
$produkt[1]['Anzahl'] = 0;
$product[1]['Amount'] = 0;
$produkt[2]['Produktname'] = "Bandschleifer";
$product[2]['ProductName'] = "Bandschleifer";
$produkt[2]['Preis'] = 85.99;
$product[2]['Price'] = 85.99;
$produkt[2]['Anzahl'] = 11;
$product[2]['Amount'] = 11;
foreach($produkt as $ebene1) {
foreach($product as $level1) {
   foreach($ebene1 as $name => $ebene2) {
   foreach($level1 as $name => $level2) {
     print $name . ": " .$ebene2. "<br>\n";
     print $name . ": " .$level2. "<br>\n";
   }
   }
   print "<br>";
   print "<br>";
Line 175: Line 176:
<pre>
<pre>
// BPS 1
// BPS 1
function begruessung() {
function greeting() {
   print "Guten Morgen";
   print "Guten Morgen";
}
}
begruessung();
greeting();


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


// BSP 3
// BSP 3
function begruessung($text, $ansprache) {
function greeting($text, $salutation) {
   print $text . " " . $ansprache;
   print $text . " " . $salutation;
}
}
$gruss = "Guten Morgen";
$greet = "Guten Morgen";
$leser = "Herr Müller";
$reader = "Herr Müller";
begruessung($gruss, $leser);
greeting($greet, $reader);
</pre>
</pre>


Line 199: Line 200:
=====Beispiel 1=====
=====Beispiel 1=====
<pre>
<pre>
function verdoppelung($wert) {
<?php
   $wert = $wert*2;
function doubleValue($value) {
   return $wert;
   $value = $value*2;
   return $value;
}
}
print verdoppelung(5);
print doubleValue(5);
/* oder */
/* oder */
function verdoppelungAlternative($wert) {
function doubleValueAlternative($value) {
   return ($wert*2);
   return ($value*2);
}
}
print verdoppelungAlternative(5);
print doubleValueAlternative(5);
?>
</pre>
</pre>


=====Beispiel 2=====
=====Beispiel 2=====
<pre>
<pre>
function verdoppelung_quadrat($wert) {
<?php
   $doppelt = $wert*2;
function doubleAndPowerOf2($value) {
   $quadrat = $wert*$wert;
   $double = $value*2;
   $ergebnis = array(
   $powerOf2 = $value*$value;
     'Verdopplung' => $doppelt,
   $result = array(
     'Quadrat' => $quadrat
     'Verdopplung' => $double,
     'Quadrat' => $powerOf2
   );
   );
   return $ergebnis;
   return $result;
}
$returnValue = doubleAndPowerOf2(3);
$doubleValue = $returnValue['Verdopplung'];
$valuePowerOf2 = $returnValue['Quadrat'];
print "Der doppelte Wert dieser Zahl beträgt " . $doubleValue .".<br>\n";
print "Das Quadrat dieser Zahl beträgt " . $valuePowerOf2 .".<br>\n";
?>
</pre>
 
====Eine Funktion in das PHP-Programm einbinden====
<pre>
File: doubleValue.php
 
<?php
    function doubleValue($value){
    return ($value * 2);
}
}
$rueckgabewert = verdoppelung_quadrat(3);
?>
$verdoppelter_wert = $rueckgabewert['Verdopplung'];
</pre>
$wert_zum_quadrat = $rueckgabewert['Quadrat'];
 
print "Der doppelte Wert dieser Zahl beträgt " . $verdoppelter_wert .".<br>\n";
<pre>
print "Das Quadrat dieser Zahl beträgt " . $wert_zum_quadrat .".<br>\n";
File: MainFile
 
<?php
    include("doubleValue.php");
    print doubleValue(4);
?>
</pre>
</pre>

Latest revision as of 10:54, 10 January 2023

Entscheidungen durch if-Abfragen[edit]

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


Else und elseif[edit]

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


While und For-Schleife[edit]

Beispiel 1[edit]
<?php
for ($i= 0; $i < 10; $i++) {
  print "<p>Zehnmal Hallo</p>\n";
}
?>
Beispiel 2[edit]
<?php
$i = 0;
while ($i < 10) {
  $i++;
  print $i*$i."<br>\n";
}
?>
Beispiel 3[edit]
<?php
$product[0]['ProductName'] = "Semmel";
$product[0]['Price'] = 1.99;
$product[0]['Amount'] = 6;
$product[1]['ProductName'] = "Kornspitz";
$product[1]['Price'] = 1.49;
$product[1]['Amount'] = 0;
$product[2]['ProductName'] = "Laugenbrezl";
$product[2]['Price'] = 2.99;
$product[2]['Amount'] = 8;
$i = 0;

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

while ($i < count($product)) {
  if($product[$i]['Amount'] > 0) {
    print "<p>Product: " .$product[$i]['ProductName']. " Price: "
      .$product[$i]['Price']. " Euro</p>\n";
  }
  $i++;
}
?>

Foreach Schleifen[edit]

Beispiel 1[edit]
<?php
$product = array(
  1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23
);
foreach ($product as $item) { 
  print $item . "<br>\n";
}
?>
Beispiel 2[edit]
<?php
$product[0]['ProductName'] = "Bohrmaschine";
$product[0]['Price'] = 45.99;
$product[0]['Amount'] = 6;
$product[1]['ProductName'] = "Kreissäge";
$product[1]['Price'] = 79.99;
$product[1]['Amount'] = 0;
$product[2]['ProductName'] = "Bandschleifer";
$product[2]['Price'] = 85.99;
$product[2]['Amount'] = 11;

foreach($product as $content) {
  if($content['Amount'] > 0) {
    print "<p>Produkt: " .$content['ProductName'].
      " Preis: " .$content['Price']. " Euro.</p>\n";
  }
}
?>
Beispiel 3[edit]
<?php
$product[0]['ProductName'] = "Bohrmaschine";
$product[0]['Price'] = 45.99;
$product[0]['Amount'] = 6;
$product[1]['ProductName'] = "Kreissäge";
$product[1]['Price'] = 79.99;
$product[1]['Amount'] = 0;
$product[2]['ProductName'] = "Bandschleifer";
$product[2]['Price'] = 85.99;
$product[2]['Amount'] = 11;
foreach($product as $level1) {
  foreach($level1 as $level2) {
    print $level2. "<br>\n";
  }
  print "<br>";
}
?>
Beispiel 4[edit]
<?php
$product[0]['ProductName'] = "Bohrmaschine";
$product[0]['Price'] = 45.99;
$product[0]['Amount'] = 6;
$product[1]['ProductName'] = "Kreissäge";
$product[1]['Price'] = 79.99;
$product[1]['Amount'] = 0;
$product[2]['ProductName'] = "Bandschleifer";
$product[2]['Price'] = 85.99;
$product[2]['Amount'] = 11;
foreach($product as $level1) {
  foreach($level1 as $name => $level2) {
    print $name . ": " .$level2. "<br>\n";
  }
  print "<br>";
}
?>

Funktionen in PHP[edit]

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

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

// BSP 3
function greeting($text, $salutation) {
  print $text . " " . $salutation;
}
$greet = "Guten Morgen";
$reader = "Herr Müller";
greeting($greet, $reader);


Rückgabewerte der Funktionen[edit]

Beispiel 1[edit]
<?php 
function doubleValue($value) {
  $value = $value*2;
  return $value;
}
print doubleValue(5);
/* oder */
function doubleValueAlternative($value) {
  return ($value*2);
}
print doubleValueAlternative(5);
?>
Beispiel 2[edit]
<?php 
function doubleAndPowerOf2($value) {
  $double = $value*2;
  $powerOf2 = $value*$value;
  $result = array(
    'Verdopplung' => $double,
    'Quadrat' => $powerOf2
  );
  return $result;
}
$returnValue = doubleAndPowerOf2(3);
$doubleValue = $returnValue['Verdopplung'];
$valuePowerOf2 = $returnValue['Quadrat'];
print "Der doppelte Wert dieser Zahl beträgt " . $doubleValue .".<br>\n";
print "Das Quadrat dieser Zahl beträgt " . $valuePowerOf2 .".<br>\n";
?>

Eine Funktion in das PHP-Programm einbinden[edit]

File: doubleValue.php

<?php 
    function doubleValue($value){
    return ($value * 2);
}
?>
File: MainFile

<?php
    include("doubleValue.php");
    print doubleValue(4);
?>