Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 1
Lecture 11
Log into Linux, also log into csserver Reminder: Project 3 due next Tuesday Questions?
Lecture 11 Log into Linux, also log into csserver Reminder: Project - - PowerPoint PPT Presentation
Lecture 11 Log into Linux, also log into csserver Reminder: Project 3 due next Tuesday Questions? Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 1 Outline More PHP Remote files Directory operations
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 1
Log into Linux, also log into csserver Reminder: Project 3 due next Tuesday Questions?
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 2
More PHP Remote files Directory operations Functions Variable scope Interacting with the OS Regular expression matching and
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 3
Since PHP was designed for use with web
$slash = fopen("http://www.slashdot.org", "rt"); $site = fread($slash, 200000); // 1st 200K bytes fclose($slash); print $site;
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 4
PHP directory operations are similar to C:
$handle = opendir ("."); // current dir if ($handle) { // check that open succeeded while (false !== ($filename = readdir($handle))){ if ($filename != "." && $filename != "..") print "$file\n"; } closedir ($handle); }
The use of !== (not identical to) is to handle the
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 5
Other directory operations include:
mkdir ( ) - create a directory rmdir ( ) - remove an empty directory get_cwd ( ) - get the current working directory chdir ( ) - change directories
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 6
PHP functions are defined as in C++ without
Function definitions can appear anywhere in
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 7
PHP has default parameters
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 8
A PHP function may be called with more arguments
function someFunc ($a) { for ($i = 0; $i < func_num_args(); ++$i) { $param = func_get_arg($i); print "Received parameter $i: $param\n"; } $params = func_get_args(); $paramlist = implode(",", $params); print "Received parameters: $paramlist\n"; } someFunc (5,4,3,2,1);
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 9
In PHP, any variable not set inside a function is
In particular, this means that variables span any
Variables set inside functions are local to that
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 10
Special variables like $_SERVER are called
$GLOBALS is a superglobal associative array
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 11
$bar = "baz"; print "global \$bar is: $bar\n"; // baz foo1(); print "global \$bar is: $bar\n"; // baz print "global \$foo is: $foo\n"; // null foo2(); print "global \$bar is: $bar\n"; // wombat function foo1 () { print "local \$bar is: $bar\n"; // null $foo = "foo"; print "local \$foo is: $foo\n"; // foo } function foo2 () { $GLOBALS['bar'] = "wombat"; }
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 12
Backticks (`) can be used to run external
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 13
When the basic string operations are not
Regexps are formed by starting and ending
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 14
The special characters used to form regexps
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 15
Quantifiers indicate how many times the
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 16
Here are some regexp examples:
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 17
A regular expression can be followed by
Common modifiers include
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 18
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 19
There are special symbols that can be used
The last two are used with multi-line strings ('m'
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 20
The function to use regexps to identify text to
The replacement string can contain $n to refer
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 21
$a = "Foo moo boo tool foo"; $b = preg_replace("/[A-Za-z]oo\b/", "Got word: $0\n", $a); print $b; // Output: // Got word: Foo // Got word: moo // Got word: boo // tool Got word: foo $match = "/the (car|cat) sat on the (drive|mat)/"; $input = "the cat sat on the mat"; print preg_replace($match, "Matched $0, $1, and $2\n", $input); // Output: Matched the cat sat on the mat, cat, and mat
Thursday, September 30 CS 375 UNIX System Programming - Lecture 11 22
Write a PHP program that tests whether or not
The following should be considered valid phone