Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:php

Dies ist eine alte Version des Dokuments!


PHP

Variables

var Keyword

var was the keyword for defining a member variable within a class. Since php5 it should never appear anywhere.

Variable parsing

For hashtables use this syntax:

echo "A banana is {$fruits['banana']}.";

predefined stuff

Arrays

Making of

$fruits = array (
  "fruits"  => array ("a"=>"orange", "b"=>"banana", "c"=>"apple"),
  "numbers" => array (1, 2, 3, 4, 5, 6),
  "holes"   => array ("first", 5 => "second", "third")
);

Working with

foreach ($fruits as $fruit) {...}         // traverse the array
foreach ($fruits as $key => $fruit) {...} // same if indices are needed
$length= count($fruits); // get number of items
print_r($fruits);        // print content

Copying

By default, arrays are copied by value:

$a = array ('wespe', 'bine', 'hummel');
$b = $a;
$c = &$a;
array_shift($a); // shifts the first value of the array off
echo "b: "; print_r($b);
echo "c: "; print_r($c);

Result (with PHP5):

b: Array
(
    [0] => wespe
    [1] => bine
    [2] => hummel
)
c: Array
(
    [0] => bine
    [1] => hummel
)

Command Line Usage

2004-02-13 14:45

Prerequisites

  • see also php-docu: capter „Using Php from the command line“
  • /usr/bin/php can be CLI- or CGI- Sapi; test it with „php -v“
  • Only If CGI:
    • Optin -qC is necessary in shebang; therefore use it BY DEFAULT! (-q suppresses header; -C keeps working dir)
    • set register_argc_argv = On in /etc/apache/php.ini

Command Line Arguments

#!/usr/bin/php
echo "argument count: ".$_SERVER['argc']."\n";
foreach ($_SERVER['argv'] as $name => $value) {
    echo "$name: $value\n";
}

Script saved as tester and called with tester bee fly prints:

argument count: 3
0: /home/becki/bin/tester
1: bee
2: fly

argv[0] = name of script
Dont't use $arc and $argv, because they are not available under all circumstances!

Standard In- and Output

stdin

Read stdin line by line into the array lin:

char lin[MAX_LINE_LEN];
while (fgets(lin, MAX_LINE_LEN, stdin)) {/* do some thing */}

stdout, stderr

printf(); echo; // writing to stdout
;  // writing to stderr
error_log("Value $val"); // writing to System error log eg. /var/log/apache/error_log

Draft: Pages and Actions

FIXME Pre- and Postactions are probably obsolete. See http://skiclub-mitwitz.de/lib/libPage/

Pre-Actions

  1. Every page must have exactly 1 unique pre-action (prefix) which is used for building the page eg a database query
  2. Pages can only be accessed by their pre-actions not directly by other pages. If the page has no dynamic content, the pre-action just calls the page constructor or sets the $page global variable.
  3. Although different pages may require to do the same things in it's pre-actions do not use the same action, but let those actions call the same helper function. Thus the target page becomes clear.
  4. FIXME pre-action can change page by calling other pre-action

Post-Actions

  1. Each link or form of a page may be bound to exactly 1 unique post-action (suffix) eg a database update
  2. Post-actions may be followed by pre-actions (chaining of actions)
  3. Actions are named after the page they are bound to. To the name of pre-actions 'p_' is prepended and to post-actons (suffixes) 's_' is prepended
  4. Use a parameter called eg 'target' for (the pre-actions of) pages wich can be called by differnt other pages and have to jump back to ther calling pages after they are left.

Authentication

FIXME This section is in progress

Possible information ressources

To do

Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/linux/php.1234166872.txt.gz · Zuletzt geändert: 2009-11-13 10:23 (Externe Bearbeitung)

Impressum - Datenschutzerklärung