Dies ist eine alte Version des Dokuments!
var
was the keyword for defining a member variable within a class. Since php5 it should never appear anywhere.
For hashtables use this syntax:
echo "A banana is {$fruits['banana']}.";
$_SERVER['PHP_SELF']
: filename of the currently executing script, relative to the document root, see reserved.variables$_POST
$fruits = array ( "fruits" => array ("a"=>"orange", "b"=>"banana", "c"=>"apple"), "numbers" => array (1, 2, 3, 4, 5, 6), "holes" => array ("first", 5 => "second", "third") );
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
Access to a nonexisting key generates a PHP notice. To prevent this, you can check before the access if the key exists or if the value is set. More
array_key_exists('key', $fruits)) // returns true if the key exists isset($fruits['key']) // returns true if the key exists AND value != NULL
By default, arrays are copied by value (Unlike PHP objects and Java arrays and objects):
$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 )
Objects are copied by „something like a reference“ more. This means the reference operator is not necessary for ojects (anlike PHP arrays)
Add some code to prove this.
Use error_log() for debug logging.
2004-02-13 14:45
#!/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!
Read stdin line by line into the array lin:
char lin[MAX_LINE_LEN]; while (fgets(lin, MAX_LINE_LEN, stdin)) {/* do some thing */}
printf(); echo; // writing to stdout ; // writing to stderr error_log("Value $val"); // writing to System error log eg. /var/log/apache/error_log
Pre- and Postactions are probably obsolete. See http://skiclub-mitwitz.de/lib/libPage/
See also: Build your own MVC Framework
This section is in progress