programming:php
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programming:php [2023/11/01 07:31] – removed - external edit (Unknown date) 127.0.0.1 | programming:php [2023/11/02 07:11] (current) – skipidar | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== PHP ===== | ||
+ | ==== Basic Syntax ==== | ||
+ | |||
+ | |||
+ | == $bar= == | ||
+ | Normal local variable declaration | ||
+ | < | ||
+ | function Sum() | ||
+ | { | ||
+ | $locvar = " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == global == | ||
+ | < | ||
+ | $a = 1; //global var | ||
+ | $a = 2; //global var | ||
+ | |||
+ | function Sum() | ||
+ | { | ||
+ | global $a, $b; //reference to the global vars | ||
+ | $b = $a + $b; // | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == => == | ||
+ | => us used to get values in arrays | ||
+ | < | ||
+ | $creator[' | ||
+ | $creator = array(' | ||
+ | ' | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | or to retrieve objects values | ||
+ | < | ||
+ | class Person { | ||
+ | var $name = " | ||
+ | var $age = 35; | ||
+ | } | ||
+ | $o = new Person; | ||
+ | $a = (array) $o; | ||
+ | print_r($a); | ||
+ | Array | ||
+ | ( | ||
+ | [name] => Fred | ||
+ | [age] => 35 | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | ==$$varname== | ||
+ | The double dollar sign declares that the value of $a should be used as the name of newly defined variable. \\ | ||
+ | For example, the code below creates a variable called $hello and outputs the string “world” | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | $a = ' | ||
+ | $$a = ' | ||
+ | echo $hello; | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | <?php | ||
+ | $a = ' | ||
+ | $$a = ' | ||
+ | echo "$a ${$a}"; | ||
+ | echo "$a $hello"; | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | == var keyword == | ||
+ | Is for declaring class member variables. | ||
+ | < | ||
+ | class foo { | ||
+ | var $x = ' | ||
+ | function bar() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ==== GET ==== | ||
+ | Wenn du einen text, einen Dateipfad usw. als URL parameter übergeben musst - nutze vor dem Übergeben die Funktion **rawurlencode**: | ||
+ | < | ||
+ | $filename | ||
+ | $urlfilename = rawurlencode ( $filename ); // bibtexdata1%20-%20Copy.bib | ||
+ | </ | ||
+ | |||
+ | und am anderem Ende | ||
+ | < | ||
+ | $filename = rawurldecode ( $urlfilename | ||
+ | </ | ||
+ | |||
+ | ==== OOP Syntax in PHP ==== | ||
+ | // Details about the syntax can be read [[http:// | ||
+ | |||
+ | === Call by reference === | ||
+ | Kopier- vs. Referenzsemantik | ||
+ | Beim Aufruf einer Methode kann man dieser - vorausgesetzt, | ||
+ | |||
+ | Grundsätzlich ist es natürlich immer möglich, mittels einer return-Anweisung die lokale Referenz zurückzugeben und der ursprünglichen Objektvariable zuzuweisen. | ||
+ | |||
+ | |||
+ | Ausweg aus dem Referenzdilemma | ||
+ | Wie also kann man sicherstellen, | ||
+ | |||
+ | < | ||
+ | |||
+ | $a = &$b; | ||
+ | $c =& $a; | ||
+ | $c = 42; | ||
+ | </ | ||
+ | |||
+ | Die beiden obigen Zuweisung bewirken gleichermaßen, | ||
+ | |||
+ | Auch bei Funktions- und Methodenaufrufen wird normalerweise Kopiersemantik angewandt. Um explizit Referenzsemantik zu fordern, stellt man hierbei bei einer Funktions-/ | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | function Plus($var) { $var++; } | ||
+ | function Minus(& | ||
+ | $var = 42; | ||
+ | Plus($var); | ||
+ | Minus($var); | ||
+ | echo $var; | ||
+ | |||
+ | </ | ||
+ | |||
+ | Die Ausgabe obigen Codes ist natürlich 41, da nur die Funktion Minus den Wert der Variablen $var ändert. In Java wäre das nicht so offensichtlich, | ||
+ | |||
+ | |||
+ | === Methoden === | ||
+ | |||
+ | ==static== | ||
+ | Manchmal ist es nötig, eine Methode einer Klasse aufzurufen, ohne ein Objekt derselben erzeugt zu haben. In diesem Fall kann man einen sogenannten ,, | ||
+ | |||
+ | Ein primitives Beispiel: | ||
+ | |||
+ | < | ||
+ | class Auto { | ||
+ | ... | ||
+ | function Erfinder() { | ||
+ | return " | ||
+ | } | ||
+ | ... | ||
+ | } | ||
+ | echo Auto:: | ||
+ | </ | ||
+ | |||
+ | ==parent== | ||
+ | Im Falle von Zugriffen auf geerbte, aber überschriebene Methoden sollte man statt des Klassennamens das Schlüsselwort parent benutzen. Dadurch taucht der Name der beerbten Klasse nur hinter extends auf, was das nachträgliche Vornehmen von Änderungen z.T. stark erleichtert. Außerdem wird dadurch auch der Unterschied zwischen ,,static`` (kein Objekt vorhanden) und normalem Instanz-Methodenaufruf deutlich. | ||
+ | |||
+ | Beispiel: Angenommen, wir hätten in der Klasse DSP_CR die Methode lenke(drehung) der Klasse Auto überschrieben, | ||
+ | |||
+ | < | ||
+ | ... | ||
+ | function lenke($drehung) { | ||
+ | // Servo aktivieren | ||
+ | $drehung = $this-> | ||
+ | // Methode der Basisklasse aufrufen | ||
+ | parent:: | ||
+ | } | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | ==== Debugging ==== | ||
+ | |||
+ | use the method to echo the variable' | ||
+ | < | ||
+ | var_export($VARIABLE); | ||
+ | $var = var_export($VARIABLE, | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Importing Files ==== | ||
+ | |||
+ | <sxh php> | ||
+ | //To import a file and immediately print it - do | ||
+ | require file.php; | ||
+ | |||
+ | |||
+ | //To import a file into a variable - do: | ||
+ | //getting variables by ob_start / ob_get_clean makes php evaluate the php in those files variables | ||
+ | ob_start(); | ||
+ | include($ROOT.'/ | ||
+ | $toolbar = ob_get_clean(); | ||
+ | |||
+ | |||
+ | //To load a file as it is, without filling the variables | ||
+ | $toolbar = @file_get_contents($ROOT.'/ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====INSTALLATION, | ||
+ | |||
+ | This Page is about tips and fallpits while configurating PHP: | ||
+ | |||
+ | ===Find out, which User Account PHP uses=== | ||
+ | **Problem: | ||
+ | < | ||
+ | <?php | ||
+ | echo 'Name des Benutzers: ' . get_current_user(); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===Install modules=== | ||
+ | |||
+ | < | ||
+ | sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json | ||
+ | </ | ||
+ | ====Usage==== | ||
+ | |||
+ | ===Findout the path to the current directory=== | ||
+ | When including other files, they can't be refered relatively to the current dir. To do so use | ||
+ | < | ||
+ | define(' | ||
+ | $path = (__ROOT__ . '/ | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Php Script as a cron job === | ||
+ | < | ||
+ | */20 * * * * / | ||
+ | </ |