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 Variable 2 beträgt 7";
   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($gender == "m") {
if($geschlecht == "m") {
   print "<h1>Hallo Herr ".$lastname."!</h1>\n";
   print "<h1>Hallo Herr ".$nachname."!</h1>\n";
} elseif ($gender == "w") {
} elseif ($geschlecht == "w") {
   print "<h1>Hallo Frau ".$lastname."!</h1>\n";
   print "<h1>Hallo Frau ".$nachname."!</h1>\n";
} else {
} else {
   print "Hi du";
   print "Hi du";
Line 40: Line 39:
<pre>
<pre>
<?php
<?php
if($gender == "m") {
if($geschlecht == "m") {
   print "<h1>Hallo Herr ".$lastname."!</h1>\n";
   print "<h1>Hallo Herr ".$nachname."!</h1>\n";
} else {
} else {
   print "<h1>Hallo Frau ".$lastname."!</h1>\n";
   print "<h1>Hallo Frau ".$nachname."!</h1>\n";
}
}
?>
?>
Line 53: Line 52:
<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 73: Line 72:
<pre>
<pre>
<?php
<?php
$product[0]['ProductName'] = "Semmel";
$produkt[0]['Produktname'] = "Semmel";
$product[0]['Price'] = 1.99;
$produkt[0]['Preis'] = 1.99;
$product[0]['Amount'] = 6;
$produkt[0]['Anzahl'] = 6;
$product[1]['ProductName'] = "Kornspitz";
$produkt[1]['Produktname'] = "Kornspitz";
$product[1]['Price'] = 1.49;
$produkt[1]['Preis'] = 1.49;
$product[1]['Amount'] = 0;
$produkt[1]['Anzahl'] = 0;
$product[2]['ProductName'] = "Laugenbrezl";
$produkt[2]['Produktname'] = "Laugenbrezl";
$product[2]['Price'] = 2.99;
$produkt[2]['Preis'] = 2.99;
$product[2]['Amount'] = 8;
$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($product)) {
while ($i < count($produkt)) {
   if($product[$i]['Amount'] > 0) {
   if($produkt[$i]['Anzahl'] > 0) {
     print "<p>Product: " .$product[$i]['ProductName']. " Price: "
     print "<p>Produkt: " .$produkt[$i]['Produktname']. " Preis: "
       .$product[$i]['Price']. " Euro</p>\n";
       .$produkt[$i]['Preis']. " Euro</p>\n";
   }
   }
   $i++;
   $i++;
Line 100: Line 99:
<pre>
<pre>
<?php
<?php
$product = array(
$produkt = array(
   1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23
   1, "Bohrmaschine", 45, "Kraftvolle Bohrmaschine für Handwerker", 23
);
);
foreach ($product as $item) {  
foreach ($produkt as $item) {  
   print $item . "<br>\n";
   print $item . "<br>\n";
}
}
Line 112: Line 111:
<pre>
<pre>
<?php
<?php
$product[0]['ProductName'] = "Bohrmaschine";
$produkt[0]['Produktname'] = "Bohrmaschine";
$product[0]['Price'] = 45.99;
$produkt[0]['Preis'] = 45.99;
$product[0]['Amount'] = 6;
$produkt[0]['Anzahl'] = 6;
$product[1]['ProductName'] = "Kreissäge";
$produkt[1]['Produktname'] = "Kreissäge";
$product[1]['Price'] = 79.99;
$produkt[1]['Preis'] = 79.99;
$product[1]['Amount'] = 0;
$produkt[1]['Anzahl'] = 0;
$product[2]['ProductName'] = "Bandschleifer";
$produkt[2]['Produktname'] = "Bandschleifer";
$product[2]['Price'] = 85.99;
$produkt[2]['Preis'] = 85.99;
$product[2]['Amount'] = 11;
$produkt[2]['Anzahl'] = 11;


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


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


// BSP 3
// BSP 3
function greeting($text, $salutation) {
function begruessung($text, $ansprache) {
   print $text . " " . $salutation;
   print $text . " " . $ansprache;
}
$greet = "Guten Morgen";
$reader = "Herr Müller";
greeting($greet, $reader);
</pre>
 
 
====Rückgabewerte der Funktionen====
=====Beispiel 1=====
<pre>
<?php
function doubleValue($value) {
  $value = $value*2;
  return $value;
}
print doubleValue(5);
/* oder */
function doubleValueAlternative($value) {
  return ($value*2);
}
}
print doubleValueAlternative(5);
$gruss = "Guten Morgen";
?>
$leser = "Herr Müller";
</pre>
begruessung($gruss, $leser);
 
=====Beispiel 2=====
<pre>
<?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";
?>
</pre>
 
====Eine Funktion in das PHP-Programm einbinden====
<pre>
File: doubleValue.php
 
<?php
    function doubleValue($value){
    return ($value * 2);
}
?>
</pre>
 
<pre>
File: MainFile
 
<?php
    include("doubleValue.php");
    print doubleValue(4);
?>
</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)