Antwort schreiben

Rechner PHP

Do 22. Mär 2012, 20:30

Ein einfacher Rechner von mir in PHP .... ist schon etwas älter :)


index.php :
Code:
<?php
include 'head.php';#head einbinden
$PRINT='';
$PRINT.=$head;#head ausgeben
$result = "0";#variablen anlegen
$A=0;# var für 1 textbox
$B=0;# var für 2 textbox
$op="";# var für operator
$check=true;#prüft ob textboxen und op gesetzt
#css mit php

$PRINT.='<style type="text/css"></style>';

# main
$PRINT.= '
<Form Name ="Main" Method ="POST" ACTION = "index.php">
<br /><br /><br />
<input type="text" name="TB0" >
    <select name="op">
      <option selected>+</option>
      <option>-</option>
      <option>*</option>
      <option>/</option>
    </select>
<input type="text" name="TB1" >
<input type="submit" name="SB0" value="=" >
<input type="hidden" name="result" value="" >
<br />
</form>';


#1 textbox prüfen
if (!empty($_POST["TB0"])&&isset($_POST["TB0"]))
{
$A=$_POST["TB0"];
} else {
$check=false;$A='';
}
#2 textbox prüfen
if (!empty($_POST["TB1"])&&isset($_POST["TB1"]))
{
$B=$_POST["TB1"];
} else {
$check=false;$B='';
}
#operator textbox prüfen
if (!empty($_POST["op"])&&isset($_POST["op"]))
{
$op=$_POST["op"];
} else {
$check=false;$op='';
}
#variablen prüfen und auswerten
if($check==true&& !empty($A)&& !empty($B))
{
switch($op)
{
case "+":$result=$A+$B;break;
case "-":$result=$A-$B;break;
case "*":$result=$A*$B;break;
case "/":$result=$A/$B;break;
}
#ausgabe
$PRINT.= '<H1><p><a style="color:blue">'.$A.'</a> <a style="color:crimson">'.$op.'</a> <a style="color:blue">'.$B.'</a> ergibt = <a style="color:green">'.$result.'</a></p></H1>
<script type="text/javascript">
document.Main.result.value = "'.$result.'";
document.Main.result.type="text";
</script>
';
}else{$check=true;}



#footer ausgeben
include 'foter.php';
$PRINT.=$foter;
echo($PRINT);
?>


in der Datei head.php und foter.php ist nur ein HTML Gerüst drinnen ....

Do 22. Mär 2012, 20:30

Re: Rechner PHP

Fr 23. Mär 2012, 07:27

Code:
<?php
namespace
{
   require_once('./classes/html.php');
   $html = new \html\html;
   $html -> html_head('Rechenprogrammzeugsdingens, Rechner'); #head einbinden
   $result = '0';
   # main
   ?>
   <form name = 'main' method = 'post' action = 'index.php'>
      <p>
         <input type = 'text' name = 'TB0'>
            <select name='op'>
               <option selected>+</option>
               <option>-</option>
               <option>*</option>
               <option>/</option>
            </select>
         <input type = 'text' name = 'TB1'>
         <button type = 'submit'>Berechne!</button>
         <input type = 'hidden' name = 'result' value = ''>
      </p>
   </form>';
   <?php
   if(isset($_POST['TB0']) && isset($_POST['TB1']) && isset($_POST['op']) && is_numeric($_POST['TB0']) && is_numeric($_POST['TB1']))
   {
      // Schnelle Version
      if($_POST['op'] === '+') $result = $_POST['TB0'] + $_POST['TB1'];
      else if($_POST['op'] === '-') $result = $_POST['TB0'] - $_POST['TB1'];
      else if($_POST['op'] === '*') $result = $_POST['TB0'] * $_POST['TB1'];
      else if($_POST['op'] === '/') $result = $_POST['TB0'] / $_POST['TB1'];
      else $result = 'Unbekannter Fehler';
      // Schneckentempo: http://phpbench.com/
      /*
      switch($_POST['op'])
      {
         case '+': $result = $_POST['TB0'] + $_POST['TB1']; break;
         case '-': $result = $_POST['TB0'] - $_POST['TB1']; break;
         case '*': $result = $_POST['TB0'] * $_POST['TB1']; break;
         case '/': $result = $_POST['TB0'] / $_POST['TB1']; break;
         default: $result = 'Unbekannter Fehler'; break;
      }
      */
   }
   else print 'Bitte korrekt ausfüllen!';
   
   #variablen prüfen und auswerten
   
   #ausgabe
   print '<p class = \'blue\'>'.$_POST['TB0'].'</p><p class = \'crimson\'>'.$_POST['op'].'</p><p class = \'blue\'>'.$_POST['TB1'].'</p> ergibt = <p class = \'green\'>'.$result.'</p>';
   }
   $html -> html_foot();
}

// HTML Namespace & Class

namespace html
{
   final class html
   {
      public function html_head($title, $h2)
      {
         ?>
         <!DOCTYPE HTML>
         <!-- Ein Volk sollte keine Angst vor der Regierung haben. Die Regierung sollte Angst vor ihrem Volk haben! -->
         <html>
            <head>
               <link rel = 'shortcut icon' type = 'image/x-icon' href = './images/fav.ico'>
               <link rel = 'stylesheet' type = 'text/css' href = './styles/style.css'>
               <title>Rechner</title>
            </head>
            <body>
               <img class = 'bg' src = './images/bg.jpg' alt = 'Hintergrund'>
               <div id = 'kopf'>
                  <h1><? print $title; ?></h1>
               </div>
               <div id= 'mitte'>
                  <div id = 'links'>
                     <a href = 'index' tabindex = '0'>Rechner</a><br>
                     <!-- Und weitere Links und so -->
                  </div>
                  <div id = 'inhalt'>
                     <h2><?php print $h2; ?></h2>
         <?php
      }
      public function html_foot()
      {
         ?>
                  </div>
               </div>
            </body>
         </html>
         <?php
      }
   }
}
/*
CSS file
*{font-size:10pt; font-family:arial, helvetica, sans-serif;}
body{padding:0px; margin:0px; background-color:#000; color:#000; text-align:left;}
img.bg{position:fixed; top:0; left:0; width:100%; height:100%;}
h1{font-size:25pt; font-weight:bold; margin:5px;}
h2{font-size:20pt; font-weight:bold; margin:10px 0px 20px 10px;}
p{margin:0px 10px;}
p.blue{color:00f#;}
p.crimson{color:#f00;}
p.green{color:#0f0;}
button{width:100px; height:40px; margin:10px;}
input[type = text]{background-color:#fff; cursor:text; width:150px; height:15px;}
a{font-weight:bold; text-decoration:none; line-height:20px; margin:5px;}
a:link{color:#666; font-size:10pt;}
a:visited{color:#000;}
a:focus{color:#f00;}
a:hover{color:#f00;}
a:active{color:#fff;}
#kopf{position:relative; line-height:50px; height:50px; margin:25px auto 10px auto; width:80%; background-color:#88d; text-align:center;}
#mitte{position:relative; margin:auto; width:80%;}
#links{position:absolute; margin:auto; width:150px; top:0px; bottom:0px; padding:10px 5px 10px 5px; background-color:#fb6;}
#inhalt{position:relative; min-height:250px; margin:0px 0px 0px 170px; padding:25px 50px; background-color:#ace;}
*/
?>

Re: Rechner PHP

Fr 23. Mär 2012, 07:57

Apache : "Fatal error: No code may exist outside of namespace {} in C:\xampp\htdocs\calc.php on line 52"

Re: Rechner PHP

Fr 23. Mär 2012, 10:01

Logisch, das CSS musst in ne CSS-File packen (./styles/style.css) und den Namespace HTML ebenso (./classes/html.php)... wollte nur nit 3 Nachrichten schreiben. ^^

Re: Rechner PHP

Fr 23. Mär 2012, 10:06

das mit dem css war mir klar nur normal include ich mit include' '; und gebe mit echo ' '; aus .....
Diese schreibweisen sind mir etwas fremd .....

Re: Rechner PHP

Fr 23. Mär 2012, 10:34

Nja, ich bins gewohnt, größere Applikationen zu programmieren, deshalb Namespaces, Klassen, etc. - sind relativ praktisch, vor allem, wenn du nicht immer alles einbinden willst. Reine Funktionen sind nicht ganz so praktikabel, mMn. OOP is zwar für normale Websiten nicht wirklich nötig, aber bezüglich Erweiterkarbeit und Schnelligkeit sehr praktisch...

Dafür sind dann aber auch Faktoren wie Speicherverbrauch und ne schnelle Abarbeitung notwendig. Ist zwar für stark frequentierte Websiten ebenso praktikabel, aber fürn Normaluser eigentlich redundant...

Empfehlen kann ich dir bezüglich Benchmarks: http://phpbench.com/ --> Bzw. selbst mittels microtime() und ner Schleife, die so 1000x durchläuft, die einzelnen Prozesse abtesten...
Code:
$start = microtime();
/* codeteil */
echo microtime() - $start;

Und MemoryUsage: http://xtainment.net/wiki/index.php/PHPMemoryUsage --> Bzw. selbst mittels memory_get_usage() die einzelnen Anwendungsblöcke überprüfen und optimieren...
Code:
$m1 = memory_get_usage();
$m1 = memory_get_usage(); #ohne die zweite Variableninitialisierung bekommst nen etwas verfälschten Wert, da $m1 vermutlich erst danach initialisiert wird (also memory_get_usage() -> Erstellung von $m1 (!) -> Zuweisung zu $m1)
/* codeteil */
echo memory_get_usage() - $m1;


€dith[0]: Dafür mach ich glaub gleich n eigenes Posting auf...
€dith[1]: Wegen include und require - include gibt nur nen Fehler zurück, require bricht kritisch ab, wenn die Datei nicht vorhanden ist... _once sagt nur, dass es nur einmal inkludiert bzw. requiriert werden soll, um es nicht sinnlos ständig erneut einzubinden, vor allem innerhalb von Schleifen, oder wenn der Codeteil bereits läuft.

Re: Rechner PHP

Fr 23. Mär 2012, 11:01

aso ok Danke :)

ich kann zwar OOP und Vererbung aber ich bin nicht wirklich ein Freund davon .... ich mache es lieber von hand
Ich mache aber meist eher C oder Java ....

Re: Rechner PHP

Fr 23. Mär 2012, 13:57

Dann kannst plz eventuell meinen Beitrag zur OOP durchschaun und evtl. auch korrigieren, sollt ich Fehler gemacht haben? Bin grad fast am Ende... xD War grad n bissl zu viel Stress... jetzt brauch ich meine 10 Minuten Pause... Und DANN die Weltherrschaft!

Re: Rechner PHP

Fr 23. Mär 2012, 16:34

ja falls ich was finde sag ich dir :)

Re: Rechner PHP

Fr 23. Mär 2012, 16:48

thx... puh, hier drüberscrollen war gefährlich... wenn ich noch n Codeschnipsel seh heute, lauf ich Amok im Sinne von "THOU CAST FIRE AND BRIMSTONE!"... xD
Antwort schreiben



Bei iphpbb3.com bekommen Sie ein kostenloses Forum mit vielen tollen Extras
Forum kostenlos einrichten - Hot Topics - Tags
Beliebteste Themen: Erde, Österreich, Wien, USA, NES

Impressum | Datenschutz