PHP basics Sun Feb 18 17:11:16 EST 2007 This is a small summary of a small part of PHP; it shows mostly the common things and a few that I have trouble remembering. I am not a PHP user; caveat lector. The CS machines are running PHP 4; PHP 5 is a better choice because it supports object oriented programmer better. Program structure: ============================================ PHP was derived from Perl, at least in spirit, so it shares some of the syntactic rules and oddities, though it's a lot simpler. One major difference: PHP code has to be enclosed within ; when processed by a web server, anything outside those delimiters is just sent back to the client, while anything within the delimiters is processed by PHP and the result is included in what goes to the client. When used standalone, it looks like anything outside the delimiters is just ignored, though there is probably some option to make it just read code and run it. Scalar variable names begin with $, as in Perl. Variables are typeless. You can add a number and a string, and the result is to take the prefix of the string that is numeric and use that. Variables are not declared except by initializing them, or by a global declaration within a function to state that a variable is external; otherwise variables are local to their functions. Variables need not be initialized; they have a null (empty string) value. There are some built-in variables, like those that come back from a web form, that are global or even super-global. The most useful are $_SERVER, $_GET and $_POST, which are arrays of the values returned from the web page; each of these is an array so the subscripts are names like HTTP_REFERRER. String constants are quoted with '...' or "..."; backslashes are interpreted within strings, as are $references to variables. Arrays can have numeric and/or string subscripts; there does not seem to be a distinction between indexed and associative arrays. Set array values with $a[index] = value and elements are accessed with $a[index] where indices run from 0 to count($a). Add new elements at the end with $a[] = whatever. Relational operators are more or less as in C, but there are both && || and "and" "or"; the latter have lower priority, for things like do something or die() It looks like the same operator for string and numeric comparisions, but there are === and !== operators that test for equality of type and content. Control flow is like Perl in most respects, except that braces are not needed around a body consisting of a single statement. Semicolons are needed at the end of each statement, but you don't need braces {} for a single statement as in Perl. if (whatever) ... elseif (...) ... else ... while (expression) ... do ... while (expression) for (init; expression; re-init) ... foreach ($array as $value) ... loops over the values in the array foreach ($array as $key => $value) ... loops over the values, also providing the keys switch (expression) { case ...: default: } Within a loop, break, as in C continue, as in C Functions are declared without arguments and can appear anywhere: function foo(args) { variables are local unless global v1, v2 return whatever } Arguments are passed by value (including arrays? I can't tell) though there is a & operator that means call by reference. ============================================================= You should be able to run php -r '...' in standalone mode; you don't need and there's no spurious output at the end. The little text formatter: $line = ''; $space = ''; #$rh = fopen($argv[1], "r"); #if ($rh === false) { # echo "can't open file " . $argv[1] . "\n"; # ' vs " matters # return; #} $rh = STDIN; while (!feof($rh)) { $d = rtrim(fgets($rh)); if (strlen($d) == 0) { printline(); print "\n"; } else { #$words = split("/[\s]+/", $d); # slashes needed! doesn't work in 4.3.11 $words = explode(" ", $d); # regular string split doesn't handle mult blanks $c = count($words); for ($i = 0; $i < $c; $i++) if (strlen($words[$i]) > 0) addword($words[$i]); } } fclose($rh); printline(); function addword($w) { global $line, $space; if (strlen($line) + strlen($w) > 60) printline(); $line .= $space . $w; $space = ' '; } function printline() { global $line, $space; if (strlen($line) > 0) print "$line\n"; $line = ''; $space = ''; } # the \n after the next line shows up in the output!! ?> The stdin etc file handles are there as STDIN, STDOUT, STDERR. Regular expressions have to be surrounded by slashes even if they are provided by strings for functions like preg_match. I had a lot of trouble with this, and some things still don't work, which suggests that my understanding is imperfect.