diff options
Diffstat (limited to 'test/scanners/php')
-rw-r--r-- | test/scanners/php/class.expected.raydebug | 83 | ||||
-rw-r--r-- | test/scanners/php/class.in.php | 83 | ||||
-rw-r--r-- | test/scanners/php/elvis.expected.raydebug | 27 | ||||
-rw-r--r-- | test/scanners/php/elvis.in.php | 27 | ||||
-rw-r--r-- | test/scanners/php/html+php_faulty.expected.raydebug | 1 | ||||
-rw-r--r-- | test/scanners/php/html+php_faulty.in.php | 1 | ||||
-rw-r--r-- | test/scanners/php/labels.expected.raydebug | 34 | ||||
-rw-r--r-- | test/scanners/php/labels.in.php | 34 | ||||
-rw-r--r-- | test/scanners/php/namespaces.expected.raydebug | 9 | ||||
-rw-r--r-- | test/scanners/php/namespaces.in.php | 9 | ||||
-rw-r--r-- | test/scanners/php/pleac.expected.raydebug | 5516 | ||||
-rw-r--r-- | test/scanners/php/pleac.in.php | 5516 | ||||
-rw-r--r-- | test/scanners/php/strings.expected.raydebug | 360 | ||||
-rw-r--r-- | test/scanners/php/strings.in.php | 360 | ||||
-rw-r--r-- | test/scanners/php/suite.rb | 2 | ||||
-rw-r--r-- | test/scanners/php/test.expected.raydebug | 498 | ||||
-rw-r--r-- | test/scanners/php/test.in.php | 498 |
17 files changed, 0 insertions, 13058 deletions
diff --git a/test/scanners/php/class.expected.raydebug b/test/scanners/php/class.expected.raydebug deleted file mode 100644 index cdef7d4..0000000 --- a/test/scanners/php/class.expected.raydebug +++ /dev/null @@ -1,83 +0,0 @@ -comment(<!-- from http://www.php.net/manual/en/keyword.class.php -->) -inline_delimiter(<?php) -reserved(class) class(Cart) operator({) - reserved(var) local_variable($items)operator(;) comment(// Items in our shopping cart) - - comment(// Add $num articles of $artnr to the cart) - - reserved(function) function(add_item)operator(()local_variable($artnr)operator(,) local_variable($num)operator(\)) operator({) - local_variable($this)operator(->)ident(items)operator([)local_variable($artnr)operator(]) operator(+=) local_variable($num)operator(;) - operator(}) - - comment(// Take $num articles of $artnr out of the cart) - - reserved(function) function(remove_item)operator(()local_variable($artnr)operator(,) local_variable($num)operator(\)) operator({) - reserved(if) operator(()local_variable($this)operator(->)ident(items)operator([)local_variable($artnr)operator(]) operator(>) local_variable($num)operator(\)) operator({) - local_variable($this)operator(->)ident(items)operator([)local_variable($artnr)operator(]) operator(-=) local_variable($num)operator(;) - reserved(return) pre_constant(true)operator(;) - operator(}) reserved(elseif) operator(()local_variable($this)operator(->)ident(items)operator([)local_variable($artnr)operator(]) operator(==) local_variable($num)operator(\)) operator({) - predefined(unset)operator(()local_variable($this)operator(->)ident(items)operator([)local_variable($artnr)operator(])operator(\))operator(;) - reserved(return) pre_constant(true)operator(;) - operator(}) reserved(else) operator({) - reserved(return) pre_constant(false)operator(;) - operator(}) - operator(}) -operator(}) -inline_delimiter(?>) - - -inline_delimiter(<?php) -reserved(class) class(Cart) operator({) - comment(/* None of these will work in PHP 4. */) - reserved(var) local_variable($todays_date) operator(=) predefined(date)operator(()string<delimiter(")content(Y-m-d)delimiter(")>operator(\))operator(;) - reserved(var) local_variable($name) operator(=) local_variable($firstname)operator(;) - reserved(var) local_variable($owner) operator(=) string<delimiter(')content(Fred )delimiter(')> operator(.) string<delimiter(')content(Jones)delimiter(')>operator(;) - comment(/* Arrays containing constant values will, though. */) - reserved(var) local_variable($items) operator(=) predefined(array)operator(()string<delimiter(")content(VCR)delimiter(")>operator(,) string<delimiter(")content(TV)delimiter(")>operator(\))operator(;) -operator(}) - -comment(/* This is how it should be done. */) -reserved(class) class(Cart) operator({) - reserved(var) local_variable($todays_date)operator(;) - reserved(var) local_variable($name)operator(;) - reserved(var) local_variable($owner)operator(;) - reserved(var) local_variable($items) operator(=) predefined(array)operator(()string<delimiter(")content(VCR)delimiter(")>operator(,) string<delimiter(")content(TV)delimiter(")>operator(\))operator(;) - - reserved(function) function(Cart)operator(()operator(\)) operator({) - local_variable($this)operator(->)ident(todays_date) operator(=) predefined(date)operator(()string<delimiter(")content(Y-m-d)delimiter(")>operator(\))operator(;) - local_variable($this)operator(->)ident(name) operator(=) predefined($GLOBALS)operator([)string<delimiter(')content(firstname)delimiter(')>operator(])operator(;) - comment(/* etc. . . */) - operator(}) -operator(}) -inline_delimiter(?>) - -inline_delimiter(<?php) -reserved(class) class(A) -operator({) - reserved(function) function(foo)operator(()operator(\)) - operator({) - reserved(if) operator(()predefined(isset)operator(()local_variable($this)operator(\))operator(\)) operator({) - predefined(echo) string<delimiter(')content($this is defined ()delimiter(')>operator(;) - predefined(echo) predefined(get_class)operator(()local_variable($this)operator(\))operator(;) - predefined(echo) string<delimiter(")content(\))char(\\n)delimiter(")>operator(;) - operator(}) reserved(else) operator({) - predefined(echo) string<delimiter(")char(\\$)content(this is not defined.)char(\\n)delimiter(")>operator(;) - operator(}) - operator(}) -operator(}) - -reserved(class) class(B) -operator({) - reserved(function) function(bar)operator(()operator(\)) - operator({) - constant(A)operator(::)ident(foo)operator(()operator(\))operator(;) - operator(}) -operator(}) - -local_variable($a) operator(=) reserved(new) constant(A)operator(()operator(\))operator(;) -local_variable($a)operator(->)ident(foo)operator(()operator(\))operator(;) -constant(A)operator(::)ident(foo)operator(()operator(\))operator(;) -local_variable($b) operator(=) reserved(new) constant(B)operator(()operator(\))operator(;) -local_variable($b)operator(->)ident(bar)operator(()operator(\))operator(;) -constant(B)operator(::)ident(bar)operator(()operator(\))operator(;) -inline_delimiter(?>) diff --git a/test/scanners/php/class.in.php b/test/scanners/php/class.in.php deleted file mode 100644 index bfc9e2d..0000000 --- a/test/scanners/php/class.in.php +++ /dev/null @@ -1,83 +0,0 @@ -<!-- from http://www.php.net/manual/en/keyword.class.php --> -<?php -class Cart { - var $items; // Items in our shopping cart - - // Add $num articles of $artnr to the cart - - function add_item($artnr, $num) { - $this->items[$artnr] += $num; - } - - // Take $num articles of $artnr out of the cart - - function remove_item($artnr, $num) { - if ($this->items[$artnr] > $num) { - $this->items[$artnr] -= $num; - return true; - } elseif ($this->items[$artnr] == $num) { - unset($this->items[$artnr]); - return true; - } else { - return false; - } - } -} -?> - - -<?php -class Cart { - /* None of these will work in PHP 4. */ - var $todays_date = date("Y-m-d"); - var $name = $firstname; - var $owner = 'Fred ' . 'Jones'; - /* Arrays containing constant values will, though. */ - var $items = array("VCR", "TV"); -} - -/* This is how it should be done. */ -class Cart { - var $todays_date; - var $name; - var $owner; - var $items = array("VCR", "TV"); - - function Cart() { - $this->todays_date = date("Y-m-d"); - $this->name = $GLOBALS['firstname']; - /* etc. . . */ - } -} -?> - -<?php -class A -{ - function foo() - { - if (isset($this)) { - echo '$this is defined ('; - echo get_class($this); - echo ")\n"; - } else { - echo "\$this is not defined.\n"; - } - } -} - -class B -{ - function bar() - { - A::foo(); - } -} - -$a = new A(); -$a->foo(); -A::foo(); -$b = new B(); -$b->bar(); -B::bar(); -?> diff --git a/test/scanners/php/elvis.expected.raydebug b/test/scanners/php/elvis.expected.raydebug deleted file mode 100644 index 894ad01..0000000 --- a/test/scanners/php/elvis.expected.raydebug +++ /dev/null @@ -1,27 +0,0 @@ -local_variable($this)operator(?)local_variable($is)operator(+)constant(NO)operator(:)local_variable($label)operator(;) - -comment(// Two labels:) -label(foo)operator(:) reserved(switch) operator(()constant(TYPE)operator(()ident(v)operator(\))operator(\)) operator({) - reserved(case) constant(T_CLASS)operator(:) reserved(case) constant(T_MODULE)operator(:) - ident(rb_str_append)operator(()ident(s)operator(,) ident(rb_inspect)operator(()ident(v)operator(\))operator(\))operator(;) - reserved(break)operator(;) - reserved(default)operator(:) - label(bar)operator(:) - ident(rb_str_append)operator(()ident(s)operator(,) ident(rb_any_to_s)operator(()ident(v)operator(\))operator(\))operator(;) - reserved(break)operator(;) -operator(}) -comment(// These are two more labels:) -reserved(function)operator(()ident(call)operator(\))operator(;) -label(label2)operator(:) ident(label3)operator(:) ident(a) operator(=) ident(b) operator(+) ident(c)operator(;) - -comment(// Another label:) -reserved(if) operator(()integer(1)operator(\)) operator({) - label(label4)operator(:) ident(a) operator(=) ident(b) operator(+) ident(c)operator(;) -operator(}) - -comment(// Not a label.) -ident(test)operator(() - ident(a)operator(?) - ident(b)operator(:) - ident(c) -operator(\))
\ No newline at end of file diff --git a/test/scanners/php/elvis.in.php b/test/scanners/php/elvis.in.php deleted file mode 100644 index 4626a93..0000000 --- a/test/scanners/php/elvis.in.php +++ /dev/null @@ -1,27 +0,0 @@ -$this?$is+NO:$label; - -// Two labels: -foo: switch (TYPE(v)) { - case T_CLASS: case T_MODULE: - rb_str_append(s, rb_inspect(v)); - break; - default: - bar: - rb_str_append(s, rb_any_to_s(v)); - break; -} -// These are two more labels: -function(call); -label2: label3: a = b + c; - -// Another label: -if (1) { - label4: a = b + c; -} - -// Not a label. -test( - a? - b: - c -)
\ No newline at end of file diff --git a/test/scanners/php/html+php_faulty.expected.raydebug b/test/scanners/php/html+php_faulty.expected.raydebug deleted file mode 100644 index 8bca346..0000000 --- a/test/scanners/php/html+php_faulty.expected.raydebug +++ /dev/null @@ -1 +0,0 @@ -inline_delimiter(<?php) diff --git a/test/scanners/php/html+php_faulty.in.php b/test/scanners/php/html+php_faulty.in.php deleted file mode 100644 index b3d9bbc..0000000 --- a/test/scanners/php/html+php_faulty.in.php +++ /dev/null @@ -1 +0,0 @@ -<?php diff --git a/test/scanners/php/labels.expected.raydebug b/test/scanners/php/labels.expected.raydebug deleted file mode 100644 index a469cd6..0000000 --- a/test/scanners/php/labels.expected.raydebug +++ /dev/null @@ -1,34 +0,0 @@ -inline_delimiter(<?php) -reserved(goto) ident(a)operator(;) -predefined(echo) string<delimiter(')content(Foo)delimiter(')>operator(;) - -label(a)operator(:) -predefined(echo) string<delimiter(')content(Bar)delimiter(')>operator(;) -inline_delimiter(?>) - -inline_delimiter(<?php) - -local_variable($headers) operator(=) predefined(Array)operator(()string<delimiter(')content(subject)delimiter(')>operator(,) string<delimiter(')content(bcc)delimiter(')>operator(,) string<delimiter(')content(to)delimiter(')>operator(,) string<delimiter(')content(cc)delimiter(')>operator(,) string<delimiter(')content(date)delimiter(')>operator(,) string<delimiter(')content(sender)delimiter(')>operator(\))operator(;) -local_variable($position) operator(=) integer(0)operator(;) - -label(hIterator)operator(:) operator({) - - local_variable($c) operator(=) integer(0)operator(;) - predefined(echo) local_variable($headers)operator([)local_variable($position)operator(]) operator(.) pre_constant(PHP_EOL)operator(;) - - label(cIterator)operator(:) operator({) - predefined(echo) string<delimiter(')content( )delimiter(')> operator(.) local_variable($headers)operator([)local_variable($position)operator(])operator([)local_variable($c)operator(]) operator(.) pre_constant(PHP_EOL)operator(;) - - reserved(if)operator(()operator(!)predefined(isset)operator(()local_variable($headers)operator([)local_variable($position)operator(])operator([)operator(++)local_variable($c)operator(])operator(\))operator(\)) operator({) - reserved(goto) ident(cIteratorExit)operator(;) - operator(}) - reserved(goto) ident(cIterator)operator(;) - operator(}) - - label(cIteratorExit)operator(:) operator({) - reserved(if)operator(()predefined(isset)operator(()local_variable($headers)operator([)operator(++)local_variable($position)operator(])operator(\))operator(\)) operator({) - reserved(goto) ident(hIterator)operator(;) - operator(}) - operator(}) -operator(}) -inline_delimiter(?>)
\ No newline at end of file diff --git a/test/scanners/php/labels.in.php b/test/scanners/php/labels.in.php deleted file mode 100644 index 2839ecd..0000000 --- a/test/scanners/php/labels.in.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php -goto a; -echo 'Foo'; - -a: -echo 'Bar'; -?> - -<?php - -$headers = Array('subject', 'bcc', 'to', 'cc', 'date', 'sender'); -$position = 0; - -hIterator: { - - $c = 0; - echo $headers[$position] . PHP_EOL; - - cIterator: { - echo ' ' . $headers[$position][$c] . PHP_EOL; - - if(!isset($headers[$position][++$c])) { - goto cIteratorExit; - } - goto cIterator; - } - - cIteratorExit: { - if(isset($headers[++$position])) { - goto hIterator; - } - } -} -?>
\ No newline at end of file diff --git a/test/scanners/php/namespaces.expected.raydebug b/test/scanners/php/namespaces.expected.raydebug deleted file mode 100644 index 47074a9..0000000 --- a/test/scanners/php/namespaces.expected.raydebug +++ /dev/null @@ -1,9 +0,0 @@ -inline_delimiter(<?php) -reserved(namespace) ident(foo)operator(;) -reserved(use) ident(blah)operator(\\)ident(blah) reserved(as) ident(foo)operator(;) - -local_variable($a) operator(=) reserved(new) ident(my)operator(\\)ident(name)operator(()operator(\))operator(;) comment(// instantiates "foo\\my\\name" class) -ident(foo)operator(\\)ident(bar)operator(::)ident(name)operator(()operator(\))operator(;) comment(// calls static method "name" in class "blah\\blah\\bar") -ident(my)operator(\\)ident(bar)operator(()operator(\))operator(;) comment(// calls function "foo\\my\\bar") -local_variable($a) operator(=) ident(my)operator(\\)constant(BAR)operator(;) comment(// sets $a to the value of constant "foo\\my\\BAR") -inline_delimiter(?>) diff --git a/test/scanners/php/namespaces.in.php b/test/scanners/php/namespaces.in.php deleted file mode 100644 index ad6a1a1..0000000 --- a/test/scanners/php/namespaces.in.php +++ /dev/null @@ -1,9 +0,0 @@ -<?php -namespace foo; -use blah\blah as foo; - -$a = new my\name(); // instantiates "foo\my\name" class -foo\bar::name(); // calls static method "name" in class "blah\blah\bar" -my\bar(); // calls function "foo\my\bar" -$a = my\BAR; // sets $a to the value of constant "foo\my\BAR" -?> diff --git a/test/scanners/php/pleac.expected.raydebug b/test/scanners/php/pleac.expected.raydebug deleted file mode 100644 index 1835cf5..0000000 --- a/test/scanners/php/pleac.expected.raydebug +++ /dev/null @@ -1,5516 +0,0 @@ -comment(# -*- php -*-) -comment(# The examples are taken from the Perl Cookbook) -comment(# By Tom Christiansen & Nathan Torkington) -comment(# see http://www.oreilly.com/catalog/cookbook for more) - -comment(# @@PLEAC@@_NAME) -comment(# @@SKIP@@ PHP) - -comment(# @@PLEAC@@_WEB) -comment(# @@SKIP@@ http://php.net/) - -comment(# @@PLEAC@@_1.0) -comment(#-----------------------------) -local_variable($string) operator(=) string<delimiter(')content(\\n)delimiter(')>operator(;) comment(# two characters, \\ and an n) -local_variable($string) operator(=) string<delimiter(')content(Jon )char(\\')content(Maddog)char(\\')content( Orwant)delimiter(')>operator(;) comment(# literal single quotes) -local_variable($string) operator(=) string<delimiter(')content(Jon "Maddog" Orwant)delimiter(')>operator(;) comment(# literal double quotes) -comment(#-----------------------------) -local_variable($string) operator(=) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(# a "newline" character) -local_variable($string) operator(=) string<delimiter(")content(Jon )char(\\")content(Maddog)char(\\")content( Orwant)delimiter(")>operator(;) comment(# literal double quotes) -local_variable($string) operator(=) string<delimiter(")content(Jon 'Maddog' Orwant)delimiter(")>operator(;) comment(# literal single quotes) -comment(#-----------------------------) -local_variable($a) operator(=) -string<delimiter(")content(This is a multiline -here document)delimiter(")>operator(;) - -local_variable($a) operator(=) string<delimiter(<<<EOF)content( -)content(This is a multiline here document)content( -)content(terminated by EOF on a line by itself)content( -)delimiter(EOF)>operator(;) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.1) -comment(#-----------------------------) -local_variable($value) operator(=) predefined(substr)operator(()local_variable($string)operator(,) local_variable($offset)operator(,) local_variable($count)operator(\))operator(;) -local_variable($value) operator(=) predefined(substr)operator(()local_variable($string)operator(,) local_variable($offset)operator(\))operator(;) - -local_variable($string) operator(=) predefined(substr_replace)operator(()local_variable($string)operator(,) local_variable($newstring)operator(,) local_variable($offset)operator(,) local_variable($count)operator(\))operator(;) -local_variable($string) operator(=) predefined(substr_replace)operator(()local_variable($string)operator(,) local_variable($newtail)operator(,) local_variable($offset)operator(\))operator(;) -comment(#-----------------------------) -comment(# get a 5-byte string, skip 3, then grab 2 8-byte strings, then the rest) -predefined(list)operator(()local_variable($leading)operator(,) local_variable($s1)operator(,) local_variable($s2)operator(,) local_variable($trailing)operator(\)) operator(=) - predefined(array_values)operator(()predefined(unpack)operator(()string<delimiter(")content(A5a/x3/A8b/A8c/A*d)delimiter(")>operator(,) local_variable($data)operator(\))operator(;) - -comment(# split at five byte boundaries) -predefined(preg_match_all) operator(()string<delimiter(")content(/.)string({)content(5}/)delimiter(")>operator(,) local_variable($data)operator(,) local_variable($f)operator(,) constant(PREG_PATTERN_ORDER)operator(\))operator(;) -local_variable($fivers) operator(=) local_variable($f)operator([)integer(0)operator(])operator(;) - -comment(# chop string into individual characters) -local_variable($chars) operator(=) local_variable($string)operator(;) -comment(#-----------------------------) -local_variable($string) operator(=) string<delimiter(")content(This is what you have)delimiter(")>operator(;) -comment(# +012345678901234567890 Indexing forwards (left to right\)) -comment(# 109876543210987654321- Indexing backwards (right to left\)) -comment(# note that 0 means 10 or 20, etc. above) - -local_variable($first) operator(=) predefined(substr)operator(()local_variable($string)operator(,) integer(0)operator(,) integer(1)operator(\))operator(;) comment(# "T") -local_variable($start) operator(=) predefined(substr)operator(()local_variable($string)operator(,) integer(5)operator(,) integer(2)operator(\))operator(;) comment(# "is") -local_variable($rest) operator(=) predefined(substr)operator(()local_variable($string)operator(,) integer(13)operator(\))operator(;) comment(# "you have") -local_variable($last) operator(=) predefined(substr)operator(()local_variable($string)operator(,) operator(-)integer(1)operator(\))operator(;) comment(# "e") -local_variable($end) operator(=) predefined(substr)operator(()local_variable($string)operator(,) operator(-)integer(4)operator(\))operator(;) comment(# "have") -local_variable($piece) operator(=) predefined(substr)operator(()local_variable($string)operator(,) operator(-)integer(8)operator(,) integer(3)operator(\))operator(;) comment(# "you") -comment(#-----------------------------) -local_variable($string) operator(=) string<delimiter(")content(This is what you have)delimiter(")>operator(;) -predefined(print) local_variable($string)operator(;) -comment(#This is what you have) - -local_variable($string) operator(=) predefined(substr_replace)operator(()local_variable($string)operator(,) string<delimiter(")content(wasn't)delimiter(")>operator(,) integer(5)operator(,) integer(2)operator(\))operator(;) comment(# change "is" to "wasn't") -comment(#This wasn't what you have) - -local_variable($string) operator(=) predefined(substr_replace)operator(()local_variable($string)operator(,) string<delimiter(")content(ondrous)delimiter(")>operator(,) operator(-)integer(12)operator(\))operator(;) comment(# "This wasn't wondrous") -comment(#This wasn't wondrous) - -local_variable($string) operator(=) predefined(substr_replace)operator(()local_variable($string)operator(,) string<delimiter(")delimiter(")>operator(,) integer(0)operator(,) integer(1)operator(\))operator(;) comment(# delete first character) -comment(#his wasn't wondrous) - -local_variable($string) operator(=) predefined(substr_replace)operator(()local_variable($string)operator(,) string<delimiter(")delimiter(")>operator(,) operator(-)integer(10)operator(\))operator(;) comment(# delete last 10 characters) -comment(#his wasn') -comment(#-----------------------------) -reserved(if) operator(()predefined(preg_match)operator(()string<delimiter(")content(/pattern/)delimiter(")>operator(,) predefined(substr)operator(()local_variable($string)operator(,) operator(-)integer(10)operator(\))operator(\)) operator({) - predefined(print) string<delimiter(")content(Pattern matches in last 10 characters)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(# substitute "at" for "is", restricted to first five characters) -local_variable($string)operator(=)operator(()predefined(substr_replace)operator(()predefined(preg_replace)operator(()string<delimiter(")content(/is/)delimiter(")>operator(,) string<delimiter(")content(at)delimiter(")>operator(,) predefined(substr)operator(()local_variable($string)operator(,)integer(0)operator(,)integer(5)operator(\))operator(\))operator(,)integer(0)operator(,)integer(5)operator(\))operator(;) -comment(#-----------------------------) -comment(# exchange the first and last letters in a string) -local_variable($a) operator(=) string<delimiter(")content(make a hat)delimiter(")>operator(;) -predefined(list)operator(()local_variable($a)operator([)integer(0)operator(])operator(,) local_variable($a)operator([)predefined(strlen)operator(()local_variable($a)operator(\))operator(-)integer(1)operator(])operator(\)) operator(=) predefined(Array)operator(()predefined(substr)operator(()local_variable($a)operator(,)operator(-)integer(1)operator(\))operator(,) predefined(substr)operator(()local_variable($a)operator(,)integer(0)operator(,)integer(1)operator(\))operator(\))operator(;) -predefined(print) local_variable($a)operator(;) - -comment(#-----------------------------) -comment(# extract column with unpack) -local_variable($a) operator(=) string<delimiter(")content(To be or not to be)delimiter(")>operator(;) -local_variable($b) operator(=) predefined(unpack)operator(()string<delimiter(")content(x6/A6a)delimiter(")>operator(,) local_variable($a)operator(\))operator(;) comment(# skip 6, grab 6) -predefined(print) local_variable($b)operator([)string<delimiter(')content(a)delimiter(')>operator(])operator(;) - - -local_variable($b) operator(=) predefined(unpack)operator(()string<delimiter(")content(x6/A2b/X5/A2c)delimiter(")>operator(,) local_variable($a)operator(\))operator(;) comment(# forward 6, grab 2; backward 5, grab 2) -predefined(print) local_variable($b)operator([)string<delimiter(')content(b)delimiter(')>operator(])operator(.)string<delimiter(")char(\\n)delimiter(")>operator(.)local_variable($b)operator([)string<delimiter(')content(c)delimiter(')>operator(])operator(.)string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(#-----------------------------) -reserved(function) function(cut2fmt)operator(()operator(\)) operator({) - local_variable($positions) operator(=) predefined(func_get_args)operator(()operator(\))operator(;) - local_variable($template) operator(=) string<delimiter(')delimiter(')>operator(;) - local_variable($lastpos) operator(=) integer(1)operator(;) - reserved(foreach)operator(()local_variable($positions) reserved(as) local_variable($place)operator(\)) operator({) - local_variable($template) operator(.=) string<delimiter(")content(A)delimiter(")> operator(.) operator(()local_variable($place) operator(-) local_variable($lastpos)operator(\)) operator(.) string<delimiter(")content( )delimiter(")>operator(;) - local_variable($lastpos) operator(=) local_variable($place)operator(;) - operator(}) - local_variable($template) operator(.=) string<delimiter(")content(A*)delimiter(")>operator(;) - reserved(return) local_variable($template)operator(;) -operator(}) - -local_variable($fmt) operator(=) ident(cut2fmt)operator(()integer(8)operator(,) integer(14)operator(,) integer(20)operator(,) integer(26)operator(,) integer(30)operator(\))operator(;) -predefined(print) string<delimiter(")local_variable($fmt)char(\\n)delimiter(")>operator(;) -comment(#A7 A6 A6 A6 A4 A*) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.2) -comment(#-----------------------------) -comment(# use $b if $b is true, else $c) -local_variable($a) operator(=) local_variable($b)operator(?)local_variable($b)operator(:)local_variable($c)operator(;) - -comment(# set $x to $y unless $x is already true) -local_variable($x) operator(||) local_variable($x)operator(=)local_variable($y)operator(;) -comment(#-----------------------------) -comment(# use $b if $b is defined, else $c) -local_variable($a) operator(=) predefined(defined)operator(()local_variable($b)operator(\)) operator(?) local_variable($b) operator(:) local_variable($c)operator(;) -comment(#-----------------------------) -local_variable($foo) operator(=) local_variable($bar) operator(||) local_variable($foo) operator(=) string<delimiter(")content(DEFAULT VALUE)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($dir) operator(=) predefined(array_shift)operator(()predefined($_SERVER)operator([)string<delimiter(')content(argv)delimiter(')>operator(])operator(\)) operator(||) local_variable($dir) operator(=) string<delimiter(")content(/tmp)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($dir) operator(=) predefined($_SERVER)operator([)string<delimiter(')content(argv)delimiter(')>operator(])operator([)integer(0)operator(]) operator(||) local_variable($dir) operator(=) string<delimiter(")content(/tmp)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($dir) operator(=) predefined(defined)operator(()predefined($_SERVER)operator([)string<delimiter(')content(argv)delimiter(')>operator(])operator([)integer(0)operator(])operator(\)) operator(?) predefined(array_shift)operator(()predefined($_SERVER)operator([)string<delimiter(')content(argv)delimiter(')>operator(])operator(\)) operator(:) string<delimiter(")content(/tmp)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($dir) operator(=) predefined(count)operator(()predefined($_SERVER)operator([)string<delimiter(')content(argv)delimiter(')>operator(])operator(\)) operator(?) predefined($_SERVER)operator([)string<delimiter(')content(argv)delimiter(')>operator(])operator([)integer(0)operator(]) operator(:) string<delimiter(")content(/tmp)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($count)operator([)local_variable($shell)operator(?)local_variable($shell)operator(:)string<delimiter(")content(/bin/sh)delimiter(")>operator(])operator(++)operator(;) -comment(#-----------------------------) -comment(# find the user name on Unix systems) -local_variable($user) operator(=) predefined($_ENV)operator([)string<delimiter(')content(USER)delimiter(')>operator(]) - operator(||) local_variable($user) operator(=) predefined($_ENV)operator([)string<delimiter(')content(LOGNAME)delimiter(')>operator(]) - operator(||) local_variable($user) operator(=) predefined(posix_getlogin)operator(()operator(\)) - operator(||) local_variable($user) operator(=) predefined(posix_getpwuid)operator(()predefined(posix_getuid)operator(()operator(\))operator(\))operator([)integer(0)operator(]) - operator(||) local_variable($user) operator(=) string<delimiter(")content(Unknown uid number )content($)content(<)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($starting_point) operator(||) local_variable($starting_point) operator(=) string<delimiter(")content(Greenwich)delimiter(")>operator(;) -comment(#-----------------------------) -predefined(count)operator(()local_variable($a)operator(\)) operator(||) local_variable($a) operator(=) local_variable($b)operator(;) comment(# copy only if empty) -local_variable($a) operator(=) predefined(count)operator(()local_variable($b)operator(\)) operator(?) local_variable($b) operator(:) local_variable($c)operator(;) comment(# assign @b if nonempty, else @c) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.3) -comment(#-----------------------------) -predefined(list)operator(()local_variable($VAR1)operator(,) local_variable($VAR2)operator(\)) operator(=) predefined(array)operator(()local_variable($VAR2)operator(,) local_variable($VAR1)operator(\))operator(;) -comment(#-----------------------------) -local_variable($temp) operator(=) local_variable($a)operator(;) -local_variable($a) operator(=) local_variable($b)operator(;) -local_variable($b) operator(=) local_variable($temp)operator(;) -comment(#-----------------------------) -local_variable($a) operator(=) string<delimiter(")content(alpha)delimiter(")>operator(;) -local_variable($b) operator(=) string<delimiter(")content(omega)delimiter(")>operator(;) -predefined(list)operator(()local_variable($a)operator(,) local_variable($b)operator(\)) operator(=) predefined(array)operator(()local_variable($b)operator(,) local_variable($a)operator(\))operator(;) comment(# the first shall be last -- and versa vice) -comment(#-----------------------------) -predefined(list)operator(()local_variable($alpha)operator(,) local_variable($beta)operator(,) local_variable($production)operator(\)) operator(=) predefined(Array)operator(()string<delimiter(")content(January)delimiter(")>operator(,)string<delimiter(")content(March)delimiter(")>operator(,)string<delimiter(")content(August)delimiter(")>operator(\))operator(;) -comment(# move beta to alpha,) -comment(# move production to beta,) -comment(# move alpha to production) -predefined(list)operator(()local_variable($alpha)operator(,) local_variable($beta)operator(,) local_variable($production)operator(\)) operator(=) predefined(array)operator(()local_variable($beta)operator(,) local_variable($production)operator(,) local_variable($alpha)operator(\))operator(;) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.4) -comment(#-----------------------------) -local_variable($num) operator(=) predefined(ord)operator(()local_variable($char)operator(\))operator(;) -local_variable($char) operator(=) predefined(chr)operator(()local_variable($num)operator(\))operator(;) -comment(#-----------------------------) -local_variable($char) operator(=) predefined(sprintf)operator(()string<delimiter(")content(%c)delimiter(")>operator(,) local_variable($num)operator(\))operator(;) comment(# slower than chr($num\)) -predefined(printf)operator(()string<delimiter(")content(Number %d is character %c)char(\\n)delimiter(")>operator(,) local_variable($num)operator(,) local_variable($num)operator(\))operator(;) -comment(#-----------------------------) -local_variable($ASCII) operator(=) predefined(unpack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) local_variable($string)operator(\))operator(;) -predefined(eval)operator(()string<delimiter(')content($STRING = pack("C*", )delimiter(')>operator(.)predefined(implode)operator(()string<delimiter(')content(,)delimiter(')>operator(,)local_variable($ASCII)operator(\))operator(.)string<delimiter(')content(\);)delimiter(')>operator(\))operator(;) -comment(#-----------------------------) -local_variable($ascii_value) operator(=) predefined(ord)operator(()string<delimiter(")content(e)delimiter(")>operator(\))operator(;) comment(# now 101) -local_variable($character) operator(=) predefined(chr)operator(()integer(101)operator(\))operator(;) comment(# now "e") -comment(#-----------------------------) -predefined(printf)operator(()string<delimiter(")content(Number %d is character %c)char(\\n)delimiter(")>operator(,) integer(101)operator(,) integer(101)operator(\))operator(;) -comment(#-----------------------------) -local_variable($ascii_character_numbers) operator(=) predefined(unpack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) string<delimiter(")content(sample)delimiter(")>operator(\))operator(;) -predefined(print) predefined(explode)operator(()string<delimiter(")content( )delimiter(")>operator(,)local_variable($ascii_character_numbers)operator(\))operator(.)string<delimiter(")char(\\n)delimiter(")>operator(;) - -predefined(eval)operator(()string<delimiter(')content($word = pack("C*", )delimiter(')>operator(.)predefined(implode)operator(()string<delimiter(')content(,)delimiter(')>operator(,)local_variable($ascii_character_numbers)operator(\))operator(.)string<delimiter(')content(\);)delimiter(')>operator(\))operator(;) -local_variable($word) operator(=) predefined(pack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) integer(115)operator(,) integer(97)operator(,) integer(109)operator(,) integer(112)operator(,) integer(108)operator(,) integer(101)operator(\))operator(;) comment(# same) -predefined(print) string<delimiter(")local_variable($word)char(\\n)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($hal) operator(=) string<delimiter(")content(HAL)delimiter(")>operator(;) -local_variable($ascii) operator(=) predefined(unpack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) local_variable($hal)operator(\))operator(;) -reserved(foreach) operator(()local_variable($ascii) reserved(as) local_variable($val)operator(\)) operator({) - local_variable($val)operator(++)operator(;) comment(# add one to each ASCII value) -operator(}) -predefined(eval)operator(()string<delimiter(')content($ibm = pack("C*", )delimiter(')>operator(.)predefined(implode)operator(()string<delimiter(')content(,)delimiter(')>operator(,)local_variable($ascii)operator(\))operator(.)string<delimiter(')content(\);)delimiter(')>operator(\))operator(;) -predefined(print) string<delimiter(")local_variable($ibm)char(\\n)delimiter(")>operator(;) comment(# prints "IBM") -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.5) -comment(#-----------------------------) -comment(// using perl regexp) -local_variable($array) operator(=) predefined(preg_split)operator(()string<delimiter(')content(//)delimiter(')>operator(,) local_variable($string) operator(,)operator(-)integer(1)operator(,) constant(PREG_SPLIT_NO_EMPTY)operator(\))operator(;) -comment(// using PHP function: $array = str_split($string\);) - -comment(// Cannot use unpack with a format of 'U*' in PHP.) -comment(#-----------------------------) -reserved(for) operator(()local_variable($offset) operator(=) integer(0)operator(;) predefined(preg_match)operator(()string<delimiter(')content(/(.\)/)delimiter(')>operator(,) local_variable($string)operator(,) local_variable($matches)operator(,) integer(0)operator(,) local_variable($offset)operator(\)) operator(>) integer(0)operator(;) local_variable($offset)operator(++)operator(\)) operator({) - comment(// $matches[1] has charcter, ord($matches[1]\) its number) -operator(}) -comment(#-----------------------------) -local_variable($seen) operator(=) predefined(array)operator(()operator(\))operator(;) -local_variable($string) operator(=) string<delimiter(")content(an apple a day)delimiter(")>operator(;) -reserved(foreach) operator(()predefined(str_split)operator(()local_variable($string)operator(\)) reserved(as) local_variable($char)operator(\)) operator({) - local_variable($seen)operator([)local_variable($char)operator(]) operator(=) integer(1)operator(;) -operator(}) -local_variable($keys) operator(=) predefined(array_keys)operator(()local_variable($seen)operator(\))operator(;) -predefined(sort)operator(()local_variable($keys)operator(\))operator(;) -predefined(print) string<delimiter(")content(unique chars are: )delimiter(")> operator(.) predefined(implode)operator(()string<delimiter(')delimiter(')>operator(,) local_variable($keys)operator(\))operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -ident(unique) ident(chars) ident(are)operator(:) ident(adelnpy) -comment(#-----------------------------) -local_variable($seen) operator(=) predefined(array)operator(()operator(\))operator(;) -local_variable($string) operator(=) string<delimiter(")content(an apple a day)delimiter(")>operator(;) -reserved(for) operator(()local_variable($offset) operator(=) integer(0)operator(;) predefined(preg_match)operator(()string<delimiter(')content(/(.\)/)delimiter(')>operator(,) local_variable($string)operator(,) local_variable($matches)operator(,) integer(0)operator(,) local_variable($offset)operator(\)) operator(>) integer(0)operator(;) local_variable($offset)operator(++)operator(\)) operator({) - local_variable($seen)operator([)local_variable($matches)operator([)integer(1)operator(])operator(]) operator(=) integer(1)operator(;) -operator(}) -local_variable($keys) operator(=) predefined(array_keys)operator(()local_variable($seen)operator(\))operator(;) -predefined(sort)operator(()local_variable($keys)operator(\))operator(;) -predefined(print) string<delimiter(")content(unique chars are: )delimiter(")> operator(.) predefined(implode)operator(()string<delimiter(')delimiter(')>operator(,) local_variable($keys)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -ident(unique) ident(chars) ident(are)operator(:) ident(adelnpy) -comment(#-----------------------------) -local_variable($sum) operator(=) integer(0)operator(;) -reserved(foreach) operator(()predefined(unpack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) local_variable($string)operator(\)) reserved(as) local_variable($byteval)operator(\)) operator({) - local_variable($sum) operator(+=) local_variable($byteval)operator(;) -operator(}) -predefined(print) string<delimiter(")content(sum is )local_variable($sum)char(\\n)delimiter(")>operator(;) -comment(// prints "1248" if $string was "an apple a day") -comment(#-----------------------------) -local_variable($sum) operator(=) predefined(array_sum)operator(()predefined(unpack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) local_variable($string)operator(\))operator(\))operator(;) -comment(#-----------------------------) - -comment(// sum - compute 16-bit checksum of all input files) -local_variable($handle) operator(=) exception(@)predefined(fopen)operator(()predefined($argv)operator([)integer(1)operator(])operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(;) -local_variable($checksum) operator(=) integer(0)operator(;) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($handle)operator(\))operator(\)) operator({) - local_variable($checksum) operator(+=) operator(()predefined(array_sum)operator(()predefined(unpack)operator(()string<delimiter(")content(C*)delimiter(")>operator(,) predefined(fgets)operator(()local_variable($handle)operator(\))operator(\))operator(\))operator(\))operator(;) -operator(}) -local_variable($checksum) operator(%=) predefined(pow)operator(()integer(2)operator(,)integer(16)operator(\)) operator(-) integer(1)operator(;) -predefined(print) string<delimiter(")local_variable($checksum)char(\\n)delimiter(")>operator(;) - -comment(# @@INCLUDE@@ include/php/slowcat.php) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.6) -comment(#-----------------------------) -local_variable($revchars) operator(=) predefined(strrev)operator(()local_variable($string)operator(\))operator(;) -comment(#-----------------------------) -local_variable($revwords) operator(=) predefined(implode)operator(()string<delimiter(")content( )delimiter(")>operator(,) predefined(array_reverse)operator(()predefined(explode)operator(()string<delimiter(")content( )delimiter(")>operator(,) local_variable($string)operator(\))operator(\))operator(\))operator(;) -comment(#-----------------------------) -comment(// reverse word order) -local_variable($string) operator(=) string<delimiter(')content(Yoda said, "can you see this?")delimiter(')>operator(;) -local_variable($allwords) operator(=) predefined(explode)operator(()string<delimiter(")content( )delimiter(")>operator(,) local_variable($string)operator(\))operator(;) -local_variable($revwords) operator(=) predefined(implode)operator(()string<delimiter(")content( )delimiter(")>operator(,) predefined(array_reverse)operator(()local_variable($allwords)operator(\))operator(\))operator(;) -predefined(print) local_variable($revwords) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -ident(this)operator(?)string<delimiter(")content( see you )delimiter(")>ident(can) ident(said)operator(,) constant(Yoda) -comment(#-----------------------------) -local_variable($revwords) operator(=) predefined(implode)operator(()string<delimiter(")content( )delimiter(")>operator(,) predefined(array_reverse)operator(()predefined(explode)operator(()string<delimiter(")content( )delimiter(")>operator(,) local_variable($string)operator(\))operator(\))operator(\))operator(;) -comment(#-----------------------------) -local_variable($revwords) operator(=) predefined(implode)operator(()string<delimiter(")content( )delimiter(")>operator(,) predefined(array_reverse)operator(()predefined(preg_split)operator(()string<delimiter(")content(/()content(\\s)content(+\)/)delimiter(")>operator(,) local_variable($string)operator(\))operator(\))operator(\))operator(;) -comment(#-----------------------------) -local_variable($word) operator(=) string<delimiter(")content(reviver)delimiter(")>operator(;) -local_variable($is_palindrome) operator(=) operator(()local_variable($word) operator(===) predefined(strrev)operator(()local_variable($word)operator(\))operator(\))operator(;) -comment(#-----------------------------) -comment(// quite a one-liner since "php" does not have a -n switch) -operator(%) ident(php) operator(-)ident(r) string<delimiter(')content(while (!feof(STDIN\)\) { $word = rtrim(fgets(STDIN\)\); if ($word == strrev($word\) && strlen($word\) > 5\) print $word; })delimiter(')> operator(<) operator(/)ident(usr)operator(/)ident(dict)operator(/)ident(words) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.8) -comment(#-----------------------------) -local_variable($text) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/)content(\\$)content(()content(\\w)content(+\)/e)delimiter(')>operator(,) string<delimiter(')content($$1)delimiter(')>operator(,) local_variable($text)operator(\))operator(;) -comment(#-----------------------------) -predefined(list)operator(()local_variable($rows)operator(,) local_variable($cols)operator(\)) operator(=) predefined(Array)operator(()integer(24)operator(,) integer(80)operator(\))operator(;) -local_variable($text) operator(=) string<delimiter(')content(I am $rows high and $cols long)delimiter(')>operator(;) -local_variable($text) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/)content(\\$)content(()content(\\w)content(+\)/e)delimiter(')>operator(,) string<delimiter(')content($$1)delimiter(')>operator(,) local_variable($text)operator(\))operator(;) -predefined(print) local_variable($text)operator(;) - -comment(#-----------------------------) -local_variable($text) operator(=) string<delimiter(")content(I am 17 years old)delimiter(")>operator(;) -local_variable($text) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/()content(\\d)content(+\)/e)delimiter(')>operator(,) string<delimiter(')content(2*$1)delimiter(')>operator(,) local_variable($text)operator(\))operator(;) -comment(#-----------------------------) -comment(# expand variables in $text, but put an error message in) -comment(# if the variable isn't defined) -local_variable($text) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/)content(\\$)content(()content(\\w)content(+\)/e)delimiter(')>operator(,)string<delimiter(')content(isset($$1\)?$$1:)char(\\')content([NO VARIABLE: $$1])char(\\')delimiter(')>operator(,) local_variable($text)operator(\))operator(;) -comment(#-----------------------------) - -comment(// As PHP arrays are used as hashes too, separation of section 4) -comment(// and section 5 makes little sense.) - -comment(# @@PLEAC@@_1.9) -comment(#-----------------------------) -local_variable($big) operator(=) predefined(strtoupper)operator(()local_variable($little)operator(\))operator(;) -local_variable($little) operator(=) predefined(strtolower)operator(()local_variable($big)operator(\))operator(;) -comment(// PHP does not have the\\L and\\U string escapes.) -comment(#-----------------------------) -local_variable($big) operator(=) predefined(ucfirst)operator(()local_variable($little)operator(\))operator(;) -local_variable($little) operator(=) predefined(strtolower)operator(()predefined(substr)operator(()local_variable($big)operator(,) integer(0)operator(,) integer(1)operator(\))operator(\)) operator(.) predefined(substr)operator(()local_variable($big)operator(,) integer(1)operator(\))operator(;) -comment(#-----------------------------) -local_variable($beast) operator(=) string<delimiter(")content(dromedary)delimiter(")>operator(;) -comment(// capitalize various parts of $beast) -local_variable($capit) operator(=) predefined(ucfirst)operator(()local_variable($beast)operator(\))operator(;) comment(// Dromedar) -comment(// PHP does not have the\\L and\\U string escapes.) -local_variable($capall) operator(=) predefined(strtoupper)operator(()local_variable($beast)operator(\))operator(;) comment(// DROMEDAR) -comment(// PHP does not have the\\L and\\U string escapes.) -local_variable($caprest) operator(=) predefined(strtolower)operator(()predefined(substr)operator(()local_variable($beast)operator(,) integer(0)operator(,) integer(1)operator(\))operator(\)) operator(.) predefined(substr)operator(()predefined(strtoupper)operator(()local_variable($beast)operator(\))operator(,) integer(1)operator(\))operator(;) comment(// dROMEDAR) -comment(// PHP does not have the\\L and\\U string escapes.) -comment(#-----------------------------) -comment(// titlecase each word's first character, lowercase the rest) -local_variable($text) operator(=) string<delimiter(")content(thIS is a loNG liNE)delimiter(")>operator(;) -local_variable($text) operator(=) predefined(ucwords)operator(()predefined(strtolower)operator(()local_variable($text)operator(\))operator(\))operator(;) -predefined(print) local_variable($text)operator(;) -constant(This) constant(Is) constant(A) constant(Long) constant(Line) -comment(#-----------------------------) -reserved(if) operator(()predefined(strtoupper)operator(()local_variable($a)operator(\)) operator(==) predefined(strtoupper)operator(()local_variable($b)operator(\))operator(\)) operator({) comment(// or strcasecmp($a, $b\) == 0) - predefined(print) string<delimiter(")content(a and b are the same)char(\\n)delimiter(")>operator(;) -operator(}) -comment(#-----------------------------) -comment(# @@INCLUDE@@ include/php/randcap.php) - -comment(// % php randcap.php < genesis | head -9) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.10) -comment(#-----------------------------) -predefined(echo) local_variable($var1) operator(.) ident(func)operator(()operator(\)) operator(.) local_variable($var2)operator(;) comment(// scalar only) -comment(#-----------------------------) -comment(// PHP can only handle variable expression without operators) -local_variable($answer) operator(=) string<delimiter(")content(STRING )content($)string({)content([ VAR EXPR ]} MORE STRING)delimiter(")>operator(;) -comment(#-----------------------------) -local_variable($phrase) operator(=) string<delimiter(")content(I have )delimiter(")> operator(.) operator(()local_variable($n) operator(+) integer(1)operator(\)) operator(.) string<delimiter(")content( guanacos.)delimiter(")>operator(;) -comment(// PHP cannot handle the complex exression: ${\\($n + 1\)}) -comment(#-----------------------------) -comment(// Rest of Discussion is not applicable to PHP) -comment(#-----------------------------) -comment(// Interpolating functions not available in PHP) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.11) -comment(# @@INCOMPLETE@@) -comment(# @@INCOMPLETE@@) - -comment(# @@PLEAC@@_1.12) -comment(#-----------------------------) -local_variable($output) operator(=) predefined(wordwrap)operator(()local_variable($str)operator(,) local_variable($width)operator(,) local_variable($break)operator(,) local_variable($cut)operator(\))operator(;) -comment(#-----------------------------) -comment(# @@INCLUDE@@ include/php/wrapdemo.php) -comment(#-----------------------------) -comment(// merge multiple lines into one, then wrap one long line) -predefined(print) predefined(wordwrap)operator(()predefined(str_replace)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) string<delimiter(")content( )delimiter(")>operator(,) predefined(file_get_contents)operator(()string<delimiter(')content(php://stdin)delimiter(')>operator(\))operator(\))operator(\))operator(;) -comment(#-----------------------------) -reserved(while)operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) operator({) - predefined(print) predefined(wordwrap)operator(()predefined(str_replace)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) string<delimiter(")content( )delimiter(")>operator(,) ident(stream_get_line)operator(()constant(STDIN)operator(,) integer(0)operator(,) string<delimiter(")char(\\n)char(\\n)delimiter(")>operator(\))operator(\))operator(\))operator(;) - predefined(print) string<delimiter(")char(\\n)char(\\n)delimiter(")>operator(;) -operator(}) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.13) -comment(#-----------------------------) -comment(//backslash) -local_variable($var) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/([CHARLIST]\)/)delimiter(')>operator(,) string<delimiter(')char(\\\\)content(\\$)content(1)delimiter(')>operator(,) local_variable($var)operator(\))operator(;) -comment(// double) -local_variable($var) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/([CHARLIST]\)/)delimiter(')>operator(,) string<delimiter(')content($1$1)delimiter(')>operator(,) local_variable($var)operator(\))operator(;) -comment(#-----------------------------) -local_variable($var) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/%/)delimiter(')>operator(,) string<delimiter(')content(%%)delimiter(')>operator(,) local_variable($var)operator(\))operator(;) -comment(#-----------------------------) -local_variable($string) operator(=) string<delimiter(')content(Mom said, "Don)char(\\')content(t do that.")delimiter(')>operator(;) -local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/([)char(\\')content("]\)/)delimiter(')>operator(,) string<delimiter(')char(\\\\)content(\\$)content(1)delimiter(')>operator(,) local_variable($string)operator(\))operator(;) -comment(// in PHP you can also use the addslashes(\) function) -comment(#-----------------------------) -local_variable($string) operator(=) string<delimiter(')content(Mom said, "Don)char(\\')content(t do that.")delimiter(')>operator(;) -local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/([)char(\\')content("]\)/)delimiter(')>operator(,) string<delimiter(')content($1$1)delimiter(')>operator(,) local_variable($string)operator(\))operator(;) -comment(#-----------------------------) -local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/([^A-Z]\)/)delimiter(')>operator(,) string<delimiter(')char(\\\\)content(\\$)content(1)delimiter(')>operator(,) local_variable($string)operator(\))operator(;) -comment(#-----------------------------) -comment(// PHP does not have the \\Q and \\E string metacharacters) -local_variable($string) operator(=) string<delimiter(")content(this is)char(\\\\)content( a)char(\\\\)content( test)char(\\\\)content(!)delimiter(")>operator(;) -comment(// PHP's quotemeta(\) function is not the same as perl's quotemeta(\) function) -local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/()content(\\W)content(\)/)delimiter(')>operator(,) string<delimiter(')char(\\\\)content(\\$)content(1)delimiter(')>operator(,) string<delimiter(')content(is a test!)delimiter(')>operator(\))operator(;) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.14) -comment(#-----------------------------) -local_variable($string) operator(=) predefined(trim)operator(()local_variable($string)operator(\))operator(;) -comment(#-----------------------------) -comment(// print what's typed, but surrounded by > < symbols) -reserved(while) operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) operator({) - predefined(print) string<delimiter(")content(>)delimiter(")> operator(.) predefined(substr)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(,) integer(0)operator(,) operator(-)integer(1)operator(\)) operator(.) string<delimiter(")content(<)char(\\n)delimiter(")>operator(;) -operator(}) -comment(#-----------------------------) -local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/)content(\\s)content(+/)delimiter(')>operator(,) string<delimiter(')content( )delimiter(')>operator(,) local_variable($string)operator(\))operator(;) comment(// finally, collapse middle) -comment(#-----------------------------) -local_variable($string) operator(=) predefined(trim)operator(()local_variable($string)operator(\))operator(;) -local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/)content(\\s)content(+/)delimiter(')>operator(,) string<delimiter(')content( )delimiter(')>operator(,) local_variable($string)operator(\))operator(;) -comment(#-----------------------------) -comment(// 1. trim leading and trailing white space) -comment(// 2. collapse internal whitespace to single space each) -reserved(function) function(sub_trim)operator(()local_variable($string)operator(\)) operator({) - local_variable($string) operator(=) predefined(trim)operator(()local_variable($string)operator(\))operator(;) - local_variable($string) operator(=) predefined(preg_replace)operator(()string<delimiter(')content(/)content(\\s)content(+/)delimiter(')>operator(,) string<delimiter(')content( )delimiter(')>operator(,) local_variable($string)operator(\))operator(;) - reserved(return) local_variable($string)operator(;) -operator(}) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.15) -comment(# @@INCOMPLETE@@) -comment(# @@INCOMPLETE@@) - -comment(# @@PLEAC@@_1.16) -comment(#-----------------------------) -local_variable($code) operator(=) predefined(soundex)operator(()local_variable($string)operator(\))operator(;) -comment(#-----------------------------) -local_variable($phoned_words) operator(=) predefined(metaphone)operator(()string<delimiter(")content(Schwern)delimiter(")>operator(\))operator(;) -comment(#-----------------------------) -comment(// substitution function for getpwent(\):) -comment(// returns an array of user entries,) -comment(// each entry contains the username and the full name) -reserved(function) function(getpwent)operator(()operator(\)) operator({) - local_variable($pwents) operator(=) predefined(array)operator(()operator(\))operator(;) - local_variable($handle) operator(=) predefined(fopen)operator(()string<delimiter(")content(passwd)delimiter(")>operator(,) string<delimiter(")content(r)delimiter(")>operator(\))operator(;) - reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($handle)operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($handle)operator(\))operator(;) - reserved(if) operator(()predefined(preg_match)operator(()string<delimiter(")content(/^#/)delimiter(")>operator(,) local_variable($line)operator(\))operator(\)) reserved(continue)operator(;) - local_variable($cols) operator(=) predefined(explode)operator(()string<delimiter(")content(:)delimiter(")>operator(,) local_variable($line)operator(\))operator(;) - local_variable($pwents)operator([)local_variable($cols)operator([)integer(0)operator(])operator(]) operator(=) local_variable($cols)operator([)integer(4)operator(])operator(;) - operator(}) - reserved(return) local_variable($pwents)operator(;) -operator(}) - -predefined(print) string<delimiter(")content(Lookup user: )delimiter(")>operator(;) -local_variable($user) operator(=) predefined(rtrim)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(\))operator(;) -reserved(if) operator(()predefined(empty)operator(()local_variable($user)operator(\))operator(\)) predefined(exit)operator(;) -local_variable($name_code) operator(=) predefined(soundex)operator(()local_variable($user)operator(\))operator(;) -local_variable($pwents) operator(=) ident(getpwent)operator(()operator(\))operator(;) -reserved(foreach)operator(()local_variable($pwents) reserved(as) local_variable($username) operator(=>) local_variable($fullname)operator(\)) operator({) - predefined(preg_match)operator(()string<delimiter(")content(/()content(\\w)content(+\)[^,]*)content(\\b)content(()content(\\w)content(+\)/)delimiter(")>operator(,) local_variable($fullname)operator(,) local_variable($matches)operator(\))operator(;) - predefined(list)operator(()operator(,) local_variable($firstname)operator(,) local_variable($lastname)operator(\)) operator(=) local_variable($matches)operator(;) - - reserved(if) operator(()local_variable($name_code) operator(==) predefined(soundex)operator(()local_variable($username)operator(\)) operator(||) - local_variable($name_code) operator(==) predefined(soundex)operator(()local_variable($lastname)operator(\)) operator(||) - local_variable($name_code) operator(==) predefined(soundex)operator(()local_variable($firstname)operator(\))operator(\)) - operator({) - predefined(printf)operator(()string<delimiter(")content(%s: %s %s)char(\\n)delimiter(")>operator(,) local_variable($username)operator(,) local_variable($firstname)operator(,) local_variable($lastname)operator(\))operator(;) - operator(}) -operator(}) -comment(#-----------------------------) - -comment(# @@PLEAC@@_1.17) -comment(#-----------------------------) -comment(# @@INCLUDE@@ include/php/fixstyle.php) -comment(#-----------------------------) -comment(# @@INCLUDE@@ include/php/fixstyle2.php) -comment(#-----------------------------) -comment(// very fast, but whitespace collapse) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($input)operator(\))operator(\)) operator({) - local_variable($i) operator(=) integer(0)operator(;) - predefined(preg_match)operator(()string<delimiter(")content(/^()content(\\s)content(*\)(.*\)/)delimiter(")>operator(,) predefined(fgets)operator(()local_variable($input)operator(\))operator(,) local_variable($matches)operator(\))operator(;) comment(// emit leading whitespace) - predefined(fwrite)operator(()local_variable($output)operator(,) local_variable($matches)operator([)integer(1)operator(])operator(\))operator(;) - reserved(foreach) operator(()predefined(preg_split)operator(()string<delimiter(")content(/()content(\\s)content(+\)/)delimiter(")>operator(,) local_variable($matches)operator([)integer(2)operator(])operator(\)) reserved(as) local_variable($token)operator(\)) operator({) comment(// preserve trailing whitespace) - predefined(fwrite)operator(()local_variable($output)operator(,) operator(()predefined(array_key_exists)operator(()local_variable($token)operator(,) local_variable($config)operator(\)) operator(?) local_variable($config)operator([)local_variable($token)operator(]) operator(:) local_variable($token)operator(\)) operator(.) string<delimiter(")content( )delimiter(")>operator(\))operator(;) - operator(}) - predefined(fwrite)operator(()local_variable($output)operator(,) string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) -operator(}) -comment(#-----------------------------) - -comment(// @@PLEAC@@_2.0) -comment(// As is the case under so many other languages floating point use under PHP is fraught) -comment(// with dangers. Although the basic techniques shown below are valid, please refer to) -comment(// the official PHP documentation for known issues, bugs, and alternate approaches ) - -comment(// @@PLEAC@@_2.1) -comment(// Two basic approaches to numeric validation:) -comment(// * Built-in functions like 'is_numeric', 'is_int', 'is_float' etc) -comment(// * Regexes, as shown below) - -local_variable($s) operator(=) string<delimiter(')content(12.345)delimiter(')>operator(;) - -predefined(preg_match)operator(()string<delimiter(')content(/)content(\\D)content(/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(&&) predefined(die)operator(()string<delimiter(")content(has nondigits)char(\\n)delimiter(")>operator(\))operator(;) -predefined(preg_match)operator(()string<delimiter(')content(/^)content(\\d)content(+$/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(not a natural number)char(\\n)delimiter(")>operator(\))operator(;) -predefined(preg_match)operator(()string<delimiter(')content(/^-?)content(\\d)content(+$/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(not an integer)char(\\n)delimiter(")>operator(\))operator(;) -predefined(preg_match)operator(()string<delimiter(')content(/^[+-]?)content(\\d)content(+$/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(not an integer)char(\\n)delimiter(")>operator(\))operator(;) -predefined(preg_match)operator(()string<delimiter(')content(/^-?)content(\\d)content(+)content(\\.)content(?)content(\\d)content(*$/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(not a decimal)char(\\n)delimiter(")>operator(\))operator(;) -predefined(preg_match)operator(()string<delimiter(')content(/^-?(?:)content(\\d)content(+(?:)content(\\.)content(\\d)content(*\)?|)content(\\.)content(\\d)content(+\)$/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(not a decimal)char(\\n)delimiter(")>operator(\))operator(;) -predefined(preg_match)operator(()string<delimiter(')content(/^([+-]?\)(?=)content(\\d)content(|)content(\\.)content(\\d)content(\))content(\\d)content(*()content(\\.)content(\\d)content(*\)?([Ee]([+-]?)content(\\d)content(+\)\)?$/)delimiter(')>operator(,) local_variable($s)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(not a C float)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(getnum)operator(()local_variable($s)operator(\)) -operator({) - predefined(sscanf)operator(()local_variable($s)operator(,) string<delimiter(")content(%D)delimiter(")>operator(,) local_variable($number)operator(\))operator(;) reserved(return) predefined(isset)operator(()local_variable($number)operator(\)) operator(?) local_variable($number) operator(:) integer(0)operator(;) -operator(}) - -predefined(echo) ident(getnum)operator(()integer(123)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// ok) -predefined(echo) ident(getnum)operator(()hex(0xff)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// ..) -predefined(echo) ident(getnum)operator(()integer(044)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// ..) - -predefined(echo) ident(getnum)operator(()string<delimiter(')content(x)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// fail) - -comment(// @@PLEAC@@_2.2) -comment(// In PHP floating point comparisions are 'safe' [meaning the '==' comparison operator) -comment(// can be used] as long as the value consists of 14 digits or less [total digits, either) -comment(// side of the decimal point e.g. xxxxxxx.xxxxxxx, xxxxxxxxxxxxxx., .xxxxxxxxxxxxxx]. If) -comment(// values with more digits must be compared, then:) -comment(//) -comment(// * Represent as strings, and take care to avoid any implicit conversions e.g. don't pass) -comment(// a float as a float to a function and expect all digits to be retained - they won't be -) -comment(// then use 'strcmp' to compare the strings) -comment(//) -comment(// * Avoid float use; perhaps use arbitrary precision arithmetic. In this case, the) -comment(// 'bccomp' function is relevant) - -comment(// Will work as long as each floating point value is 14 digits or less) -reserved(if) operator(()local_variable($float_1) operator(==) local_variable($float_2)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// Compare as strings) -local_variable($cmp) operator(=) predefined(strcmp)operator(()string<delimiter(')content(123456789.123456789123456789)delimiter(')>operator(,) string<delimiter(')content(123456789.123456789123456788)delimiter(')>operator(\))operator(;) - -comment(// Use 'bccomp') -local_variable($precision) operator(=) integer(5)operator(;) comment(// Number of significant comparison digits after decimal point) -reserved(if) operator(()predefined(bccomp)operator(()string<delimiter(')content(1.111117)delimiter(')>operator(,) string<delimiter(')content(1.111116)delimiter(')>operator(,) local_variable($precision)operator(\))operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -local_variable($precision) operator(=) integer(6)operator(;) -reserved(if) operator(()predefined(bccomp)operator(()string<delimiter(')content(1.111117)delimiter(')>operator(,) string<delimiter(')content(1.111116)delimiter(')>operator(,) local_variable($precision)operator(\))operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// ----------------------------) - -local_variable($wage) operator(=) integer(536)operator(;) -local_variable($week) operator(=) local_variable($wage) operator(*) integer(40)operator(;) -predefined(printf)operator(()string<delimiter(")content(One week's wage is: )content($)content(%.2f)char(\\n)delimiter(")>operator(,) local_variable($week) operator(/) integer(100)operator(\))operator(;) - -comment(// @@PLEAC@@_2.3) -comment(// Preferred approach) -local_variable($rounded) operator(=) predefined(round)operator(()local_variable($unrounded)operator(,) local_variable($precision)operator(\))operator(;) - -comment(// Possible alternate approach) -local_variable($format) operator(=) string<delimiter(')content(%[width].[prec]f)delimiter(')>operator(;) -local_variable($rounded) operator(=) predefined(sprintf)operator(()local_variable($format)operator(,) local_variable($unrounded)operator(\))operator(;) - -comment(// ------------) - -local_variable($a) operator(=) float(0.255)operator(;) local_variable($b) operator(=) predefined(round)operator(()local_variable($a)operator(,) integer(2)operator(\))operator(;) -predefined(echo) string<delimiter(")content(Unrounded: )inline<delimiter({)local_variable($a)delimiter(})>char(\\n)content(Rounded: )inline<delimiter({)local_variable($b)delimiter(})>char(\\n)delimiter(")>operator(;) - -local_variable($a) operator(=) float(0.255)operator(;) local_variable($b) operator(=) predefined(sprintf)operator(()string<delimiter(')content(%.2f)delimiter(')>operator(,) local_variable($a)operator(\))operator(;) -predefined(echo) string<delimiter(")content(Unrounded: )inline<delimiter({)local_variable($a)delimiter(})>char(\\n)content(Rounded: )inline<delimiter({)local_variable($b)delimiter(})>char(\\n)delimiter(")>operator(;) - -local_variable($a) operator(=) float(0.255)operator(;) -predefined(printf)operator(()string<delimiter(")content(Unrounded: %.f)char(\\n)content(Rounded: %.2f)char(\\n)delimiter(")>operator(,) local_variable($a)operator(,) local_variable($a)operator(\))operator(;) - -comment(// ----------------------------) - -predefined(echo) string<delimiter(")content(number)char(\\t)content(int)char(\\t)content(floor)char(\\t)content(ceil)char(\\n)delimiter(")>operator(;) - -reserved(foreach)operator(()predefined(array)operator(()float(3.3)operator(,) float(3.5)operator(,) float(3.7)operator(,) operator(-)float(3.3)operator(\)) reserved(as) local_variable($number)operator(\)) -operator({) - predefined(printf)operator(()string<delimiter(")content(%.1f)char(\\t)content(%.1f)char(\\t)content(%.1f)char(\\t)content(%.1f)char(\\n)delimiter(")>operator(,) local_variable($number)operator(,) operator(()pre_type(int)operator(\)) local_variable($number)operator(,) predefined(floor)operator(()local_variable($number)operator(\))operator(,) predefined(ceil)operator(()local_variable($number)operator(\))operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_2.4) -comment(// PHP offers the 'bindec' and 'decbin' functions to converting between binary and decimal) - -local_variable($num) operator(=) predefined(bindec)operator(()string<delimiter(')content(0110110)delimiter(')>operator(\))operator(;) - -local_variable($binstr) operator(=) predefined(decbin)operator(()integer(54)operator(\))operator(;) - -comment(// @@PLEAC@@_2.5) -reserved(foreach) operator(()predefined(range)operator(()local_variable($X)operator(,) local_variable($Y)operator(\)) reserved(as) local_variable($i)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(foreach) operator(()predefined(range)operator(()local_variable($X)operator(,) local_variable($Y)operator(,) integer(7)operator(\)) reserved(as) local_variable($i)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(for) operator(()local_variable($i) operator(=) local_variable($X)operator(;) local_variable($i) operator(<=) local_variable($Y)operator(;) local_variable($i)operator(++)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(for) operator(()local_variable($i) operator(=) local_variable($X)operator(;) local_variable($i) operator(<=) local_variable($Y)operator(;) local_variable($i) operator(+=) integer(7)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// ----------------------------) - -predefined(echo) string<delimiter(')content(Infancy is:)delimiter(')>operator(;) reserved(foreach)operator(()predefined(range)operator(()integer(0)operator(,) integer(2)operator(\)) reserved(as) local_variable($i)operator(\)) predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($i)delimiter(})>char(\\n)delimiter(")>operator(;) -predefined(echo) string<delimiter(')content(Toddling is:)delimiter(')>operator(;) reserved(foreach)operator(()predefined(range)operator(()integer(3)operator(,) integer(4)operator(\)) reserved(as) local_variable($i)operator(\)) predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($i)delimiter(})>char(\\n)delimiter(")>operator(;) -predefined(echo) string<delimiter(')content(Childhood is:)delimiter(')>operator(;) reserved(foreach)operator(()predefined(range)operator(()integer(5)operator(,) integer(12)operator(\)) reserved(as) local_variable($i)operator(\)) predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($i)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_2.6) -comment(// PHP offers no native support for Roman Numerals. However, a 'Numbers_Roman' class) -comment(// is available for download from PEAR: [http://pear.php.net/package/Numbers_Roman].) -comment(// Note the following 'include' directives are required:) -comment(//) -comment(// include_once('Numbers/Roman.php'\);) - -local_variable($roman) operator(=) constant(Numbers_Roman)operator(::)ident(toNumeral)operator(()local_variable($arabic)operator(\))operator(;) -local_variable($arabic) operator(=) constant(Numbers_Roman)operator(::)ident(toNumber)operator(()local_variable($roman)operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($roman_fifteen) operator(=) constant(Numbers_Roman)operator(::)ident(toNumeral)operator(()integer(15)operator(\))operator(;) - -local_variable($arabic_fifteen) operator(=) constant(Numbers_Roman)operator(::)ident(toNumber)operator(()local_variable($roman_fifteen)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(Roman for fifteen is: %s)char(\\n)delimiter(")>operator(,) local_variable($roman_fifteen)operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(Arabic for fifteen is: %d)char(\\n)delimiter(")>operator(,) local_variable($arabic_fifteen)operator(\))operator(;) - -comment(// @@PLEAC@@_2.7) -comment(// Techniques used here simply mirror Perl examples, and are not an endorsement) -comment(// of any particular RNG technique) - -comment(// In PHP do this ...) -local_variable($random) operator(=) predefined(rand)operator(()local_variable($lowerbound)operator(,) local_variable($upperbound)operator(\))operator(;) -local_variable($random) operator(=) predefined(rand)operator(()local_variable($x)operator(,) local_variable($y)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(make_password)operator(()local_variable($chars)operator(,) local_variable($reqlen)operator(\)) -operator({) - local_variable($len) operator(=) predefined(strlen)operator(()local_variable($chars)operator(\))operator(;) - reserved(for) operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) local_variable($reqlen)operator(;) local_variable($i)operator(++)operator(\)) local_variable($password) operator(.=) predefined(substr)operator(()local_variable($chars)operator(,) predefined(rand)operator(()integer(0)operator(,) local_variable($len)operator(\))operator(,) integer(1)operator(\))operator(;) - reserved(return) local_variable($password)operator(;) -operator(}) - -local_variable($chars) operator(=) string<delimiter(')content(ABCDEfghijKLMNOpqrstUVWXYz)delimiter(')>operator(;) local_variable($reqlen) operator(=) integer(8)operator(;) - -local_variable($password) operator(=) ident(make_password)operator(()local_variable($chars)operator(,) local_variable($reqlen)operator(\))operator(;) - -comment(// @@PLEAC@@_2.8) -comment(// PHP sports a large number of C Standard Library routines including the 'srand') -comment(// function, used to re-seed the RNG used with calls to the 'rand' function. Thus,) -comment(// as per Perl example:) - -reserved(while) operator(()pre_constant(TRUE)operator(\)) -operator({) - local_variable($seed) operator(=) operator(()pre_type(int)operator(\)) predefined(fgets)operator(()constant(STDIN)operator(\))operator(;) - reserved(if) operator(()operator(!)predefined(empty)operator(()local_variable($seed)operator(\))operator(\)) reserved(break)operator(;) -operator(}) - -predefined(srand)operator(()local_variable($seed)operator(\))operator(;) - -comment(// @@PLEAC@@_2.9) -comment(// The above is considered - for many reasons - a poor way of seeding the RNG. PHP) -comment(// also offers alternate versions of the functions, 'mt_srand' and 'mt_rand',) -comment(// which are described as faster, and more 'random', though key to obtaining a) -comment(// more 'random' distribution of generated numbers seems to be through using) -comment(// a combination of a previously saved random value in combination with an) -comment(// unrepeatable value [like the current time in microseconds] that is multiplied) -comment(// by a large prime number, or perhaps as part of a hash [examples available in) -comment(// PHP documentation for 'srand' and 'mt_srand']) - -ident(mt_srand)operator(()local_variable($saved_random_value) operator(+) predefined(microtime)operator(()operator(\)) operator(*) integer(1000003)operator(\))operator(;) - -comment(// or) - -ident(mt_srand)operator(()operator(()local_variable($saved_random_value) operator(+) predefined(hexdec)operator(()predefined(substr)operator(()predefined(md5)operator(()predefined(microtime)operator(()operator(\))operator(\))operator(,) operator(-)integer(8)operator(\))operator(\))operator(\)) operator(&) hex(0x7fffffff)operator(\))operator(;) - -comment(// Use of 'mt_rand' together with an appropriate seeding approach should help better) -comment(// approximate the generation of a 'truly random value') -local_variable($truly_random_value) operator(=) ident(mt_rand)operator(()operator(\))operator(;) - -comment(// @@PLEAC@@_2.10) -reserved(function) function(random)operator(()operator(\)) operator({) reserved(return) operator(()pre_type(float)operator(\)) predefined(rand)operator(()operator(\)) operator(/) operator(()pre_type(float)operator(\)) predefined(getrandmax)operator(()operator(\))operator(;) operator(}) - -reserved(function) function(gaussian_rand)operator(()operator(\)) -operator({) - local_variable($u1) operator(=) float(0.0)operator(;) local_variable($u2) operator(=) float(0.0)operator(;) local_variable($g1) operator(=) float(0.0)operator(;) local_variable($g2) operator(=) float(0.0)operator(;) local_variable($w) operator(=) float(0.0)operator(;) - - reserved(do) - operator({) - local_variable($u1) operator(=) float(2.0) operator(*) ident(random)operator(()operator(\)) operator(-) float(1.0)operator(;) local_variable($u2) operator(=) float(2.0) operator(*) ident(random)operator(()operator(\)) operator(-) float(1.0)operator(;) - local_variable($w) operator(=) local_variable($u1) operator(*) local_variable($u1) operator(+) local_variable($u2) operator(*) local_variable($u2)operator(;) - operator(}) reserved(while) operator(()local_variable($w) operator(>) float(1.0)operator(\))operator(;) - - local_variable($w) operator(=) predefined(sqrt)operator(()operator(()operator(-)float(2.0) operator(*) predefined(log)operator(()local_variable($w)operator(\))operator(\)) operator(/) local_variable($w)operator(\))operator(;) local_variable($g2) operator(=) local_variable($u1) operator(*) local_variable($w)operator(;) local_variable($g1) operator(=) local_variable($u2) operator(*) local_variable($w)operator(;) - - reserved(return) local_variable($g1)operator(;) -operator(}) - -comment(// ------------) - -local_variable($mean) operator(=) float(25.0)operator(;) local_variable($sdev) operator(=) float(2.0)operator(;) -local_variable($salary) operator(=) ident(gaussian_rand)operator(()operator(\)) operator(*) local_variable($mean) operator(+) local_variable($sdev)operator(;) - -predefined(printf)operator(()string<delimiter(")content(You have been hired at: %.2f)char(\\n)delimiter(")>operator(,) local_variable($salary)operator(\))operator(;) - -comment(// @@PLEAC@@_2.11) -comment(// 'deg2rad' and 'rad2deg' are actually PHP built-ins, but here is how you might implement) -operator(/) ident(them) reserved(if) ident(needed) -reserved(function) function(deg2rad_)operator(()local_variable($deg)operator(\)) operator({) reserved(return) operator(()local_variable($deg) operator(/) float(180.0)operator(\)) operator(*) pre_constant(M_PI)operator(;) operator(}) -reserved(function) function(rad2deg_)operator(()local_variable($rad)operator(\)) operator({) reserved(return) operator(()local_variable($rad) operator(/) pre_constant(M_PI)operator(\)) operator(*) float(180.0)operator(;) operator(}) - -comment(// ------------) - -predefined(printf)operator(()string<delimiter(")content(%f)char(\\n)delimiter(")>operator(,) ident(deg2rad_)operator(()float(180.0)operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(%f)char(\\n)delimiter(")>operator(,) predefined(deg2rad)operator(()float(180.0)operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(degree_sin)operator(()local_variable($deg)operator(\)) operator({) reserved(return) predefined(sin)operator(()predefined(deg2rad)operator(()local_variable($deg)operator(\))operator(\))operator(;) operator(}) - -comment(// ------------) - -local_variable($rad) operator(=) predefined(deg2rad)operator(()float(380.0)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(%f)char(\\n)delimiter(")>operator(,) predefined(sin)operator(()local_variable($rad)operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(%f)char(\\n)delimiter(")>operator(,) ident(degree_sin)operator(()float(380.0)operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_2.12) -reserved(function) function(my_tan)operator(()local_variable($theta)operator(\)) operator({) reserved(return) predefined(sin)operator(()local_variable($theta)operator(\)) operator(/) predefined(cos)operator(()local_variable($theta)operator(\))operator(;) operator(}) - -comment(// ------------) - -local_variable($theta) operator(=) float(3.7)operator(;) - -predefined(printf)operator(()string<delimiter(")content(%f)char(\\n)delimiter(")>operator(,) ident(my_tan)operator(()local_variable($theta)operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(%f)char(\\n)delimiter(")>operator(,) predefined(tan)operator(()local_variable($theta)operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_2.13) -local_variable($value) operator(=) float(100.0)operator(;) - -local_variable($log_e) operator(=) predefined(log)operator(()local_variable($value)operator(\))operator(;) -local_variable($log_10) operator(=) predefined(log10)operator(()local_variable($value)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(log_base)operator(()local_variable($base)operator(,) local_variable($value)operator(\)) operator({) reserved(return) predefined(log)operator(()local_variable($value)operator(\)) operator(/) predefined(log)operator(()local_variable($base)operator(\))operator(;) operator(}) - -comment(// ------------) - -local_variable($answer) operator(=) ident(log_base)operator(()float(10.0)operator(,) float(10000.0)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(log(10, 10,000\) = %f)char(\\n)delimiter(")>operator(,) local_variable($answer)operator(\))operator(;) - -comment(// @@PLEAC@@_2.14) -comment(// PHP offers no native support for matrices. However, a 'Math_Matrix' class) -comment(// is available for download from PEAR: [http://pear.php.net/package/Math_Matrix].) -comment(// Note the following 'include' directives are required:) -comment(//) -comment(// include_once('Math/Matrix.php'\);) - -local_variable($a) operator(=) reserved(new) constant(Math_Matrix)operator(()predefined(array)operator(()predefined(array)operator(()integer(3)operator(,) integer(2)operator(,) integer(3)operator(\))operator(,) predefined(array)operator(()integer(5)operator(,) integer(9)operator(,) integer(8)operator(\))operator(\))operator(\))operator(;) -local_variable($b) operator(=) reserved(new) constant(Math_Matrix)operator(()predefined(array)operator(()predefined(array)operator(()integer(4)operator(,) integer(7)operator(\))operator(,) predefined(array)operator(()integer(9)operator(,) integer(3)operator(\))operator(,) predefined(array)operator(()integer(8)operator(,) integer(1)operator(\))operator(\))operator(\))operator(;) - -predefined(echo) local_variable($a)operator(->)ident(toString)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) local_variable($b)operator(->)ident(toString)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// NOTE: When I installed this package I had to rename the 'clone' method else) -comment(// it would not load, so I chose to rename it to 'clone_', and this usage is) -comment(// shown below. This bug may well be fixed by the time you obtain this package) - -local_variable($c) operator(=) local_variable($a)operator(->)ident(clone_)operator(()operator(\))operator(;) -local_variable($c)operator(->)ident(multiply)operator(()local_variable($b)operator(\))operator(;) - -predefined(echo) local_variable($c)operator(->)ident(toString)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_2.15) -comment(// PHP offers no native support for complex numbers. However, a 'Math_Complex' class) -comment(// is available for download from PEAR: [http://pear.php.net/package/Math_Complex].) -comment(// Note the following 'include' directives are required:) -comment(//) -comment(// include_once('Math/Complex.php'\);) -comment(// include_once('Math/TrigOp.php'\);) -comment(// include_once('Math/ComplexOp.php'\);) - -local_variable($a) operator(=) reserved(new) constant(Math_Complex)operator(()integer(3)operator(,) integer(5)operator(\))operator(;) -local_variable($b) operator(=) reserved(new) constant(Math_Complex)operator(()integer(2)operator(,) operator(-)integer(2)operator(\))operator(;) - -local_variable($c) operator(=) constant(Math_ComplexOp)operator(::)ident(mult)operator(()local_variable($a)operator(,) local_variable($b)operator(\))operator(;) - -predefined(echo) local_variable($c)operator(->)ident(toString)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -local_variable($d) operator(=) reserved(new) constant(Math_Complex)operator(()integer(3)operator(,) integer(4)operator(\))operator(;) -local_variable($r) operator(=) constant(Math_ComplexOp)operator(::)predefined(sqrt)operator(()local_variable($d)operator(\))operator(;) - -predefined(echo) local_variable($r)operator(->)ident(toString)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_2.16) -comment(// Like C, PHP supports decimal-alternate notations. Thus, for example, the integer) -comment(// value, 867, is expressable in literal form as:) -comment(//) -comment(// Hexadecimal -> 0x363) -comment(// Octal -> 01543) -comment(//) -comment(// For effecting such conversions using strings there is 'sprintf' and 'sscanf'.) - -local_variable($dec) operator(=) integer(867)operator(;) -local_variable($hex) operator(=) predefined(sprintf)operator(()string<delimiter(')content(%x)delimiter(')>operator(,) local_variable($dec)operator(\))operator(;) -local_variable($oct) operator(=) predefined(sprintf)operator(()string<delimiter(')content(%o)delimiter(')>operator(,) local_variable($dec)operator(\))operator(;) - -comment(// ------------) - -local_variable($dec) operator(=) integer(0)operator(;) -local_variable($hex) operator(=) string<delimiter(')content(363)delimiter(')>operator(;) - -predefined(sscanf)operator(()local_variable($hex)operator(,) string<delimiter(')content(%x)delimiter(')>operator(,) local_variable($dec)operator(\))operator(;) - -comment(// ------------) - -local_variable($dec) operator(=) integer(0)operator(;) -local_variable($oct) operator(=) string<delimiter(')content(1543)delimiter(')>operator(;) - -predefined(sscanf)operator(()local_variable($oct)operator(,) string<delimiter(')content(%o)delimiter(')>operator(,) local_variable($dec)operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($number) operator(=) integer(0)operator(;) - -predefined(printf)operator(()string<delimiter(')content(Gimme a number in decimal, octal, or hex: )delimiter(')>operator(\))operator(;) -predefined(sscanf)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(,) string<delimiter(')content(%D)delimiter(')>operator(,) local_variable($number)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(%d %x %o)char(\\n)delimiter(")>operator(,) local_variable($number)operator(,) local_variable($number)operator(,) local_variable($number)operator(\))operator(;) - -comment(// @@PLEAC@@_2.17) -comment(// PHP offers the 'number_format' built-in function to, among many other format tasks, ) -comment(// commify numbers. Perl-compatible [as well as extended] regexes are also available) - -reserved(function) function(commify_series)operator(()local_variable($s)operator(\)) operator({) reserved(return) predefined(number_format)operator(()local_variable($s)operator(,) integer(0)operator(,) string<delimiter(')delimiter(')>operator(,) string<delimiter(')content(,)delimiter(')>operator(\))operator(;) operator(}) - -comment(// ------------) - -local_variable($hits) operator(=) integer(3456789)operator(;) - -predefined(printf)operator(()string<delimiter(")content(Your website received %s accesses last month)char(\\n)delimiter(")>operator(,) ident(commify_series)operator(()local_variable($hits)operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(commify)operator(()local_variable($s)operator(\)) -operator({) - reserved(return) predefined(strrev)operator(()predefined(preg_replace)operator(()string<delimiter(')content(/()content(\\d)content(\\d)content(\\d)content(\)(?=)content(\\d)content(\)(?!)content(\\d)content(*)content(\\.)content(\)/)delimiter(')>operator(,) string<delimiter(')content(${1},)delimiter(')>operator(,) predefined(strrev)operator(()local_variable($s)operator(\))operator(\))operator(\))operator(;) -operator(}) - -comment(// ------------) - -local_variable($hits) operator(=) integer(3456789)operator(;) - -predefined(echo) ident(commify)operator(()predefined(sprintf)operator(()string<delimiter(")content(Your website received %d accesses last month)char(\\n)delimiter(")>operator(,) local_variable($hits)operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_2.18) -reserved(function) function(pluralise)operator(()local_variable($value)operator(,) local_variable($root)operator(,) local_variable($singular)operator(=)string<delimiter(')delimiter(')> operator(,) local_variable($plural)operator(=)string<delimiter(')content(s)delimiter(')>operator(\)) -operator({) - reserved(return) local_variable($root) operator(.) operator(()operator(()local_variable($value) operator(>) integer(1)operator(\)) operator(?) local_variable($plural) operator(:) local_variable($singular)operator(\))operator(;) -operator(}) - -comment(// ------------) - -local_variable($duration) operator(=) integer(1)operator(;) -predefined(printf)operator(()string<delimiter(")content(It took %d %s)char(\\n)delimiter(")>operator(,) local_variable($duration)operator(,) ident(pluralise)operator(()local_variable($duration)operator(,) string<delimiter(')content(hour)delimiter(')>operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(%d %s %s enough.)char(\\n)delimiter(")>operator(,) local_variable($duration)operator(,) ident(pluralise)operator(()local_variable($duration)operator(,) string<delimiter(')content(hour)delimiter(')>operator(\))operator(,) - ident(pluralise)operator(()local_variable($duration)operator(,) string<delimiter(')delimiter(')>operator(,) string<delimiter(')content(is)delimiter(')>operator(,) string<delimiter(')content(are)delimiter(')>operator(\))operator(\))operator(;) - -local_variable($duration) operator(=) integer(5)operator(;) -predefined(printf)operator(()string<delimiter(")content(It took %d %s)char(\\n)delimiter(")>operator(,) local_variable($duration)operator(,) ident(pluralise)operator(()local_variable($duration)operator(,) string<delimiter(')content(hour)delimiter(')>operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(%d %s %s enough.)char(\\n)delimiter(")>operator(,) local_variable($duration)operator(,) ident(pluralise)operator(()local_variable($duration)operator(,) string<delimiter(')content(hour)delimiter(')>operator(\))operator(,) - ident(pluralise)operator(()local_variable($duration)operator(,) string<delimiter(')delimiter(')>operator(,) string<delimiter(')content(is)delimiter(')>operator(,) string<delimiter(')content(are)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(plural)operator(()local_variable($singular)operator(\)) -operator({) - local_variable($s2p) operator(=) predefined(array)operator(()string<delimiter(')content(/ss$/)delimiter(')> operator(=>) string<delimiter(')content(sses)delimiter(')>operator(,) string<delimiter(')content(/([psc]h\)$/)delimiter(')> operator(=>) string<delimiter(')content(${1}es)delimiter(')>operator(,) string<delimiter(')content(/z$/)delimiter(')> operator(=>) string<delimiter(')content(zes)delimiter(')>operator(,) - string<delimiter(')content(/ff$/)delimiter(')> operator(=>) string<delimiter(')content(ffs)delimiter(')>operator(,) string<delimiter(')content(/f$/)delimiter(')> operator(=>) string<delimiter(')content(ves)delimiter(')>operator(,) string<delimiter(')content(/ey$/)delimiter(')> operator(=>) string<delimiter(')content(eys)delimiter(')>operator(,) - string<delimiter(')content(/y$/)delimiter(')> operator(=>) string<delimiter(')content(ies)delimiter(')>operator(,) string<delimiter(')content(/ix$/)delimiter(')> operator(=>) string<delimiter(')content(ices)delimiter(')>operator(,) string<delimiter(')content(/([sx]\)$/)delimiter(')> operator(=>) string<delimiter(')content($1es)delimiter(')>operator(,) - string<delimiter(')content($)delimiter(')> operator(=>) string<delimiter(')content(s)delimiter(')>operator(\))operator(;) - - reserved(foreach)operator(()local_variable($s2p) reserved(as) local_variable($s) operator(=>) local_variable($p)operator(\)) - operator({) - reserved(if) operator(()predefined(preg_match)operator(()local_variable($s)operator(,) local_variable($singular)operator(\))operator(\)) reserved(return) predefined(preg_replace)operator(()local_variable($s)operator(,) local_variable($p)operator(,) local_variable($singular)operator(\))operator(;) - operator(}) -operator(}) - -comment(// ------------) - -reserved(foreach)operator(()predefined(array)operator(()string<delimiter(')content(mess)delimiter(')>operator(,) string<delimiter(')content(index)delimiter(')>operator(,) string<delimiter(')content(leaf)delimiter(')>operator(,) string<delimiter(')content(puppy)delimiter(')>operator(\)) reserved(as) local_variable($word)operator(\)) -operator({) - predefined(printf)operator(()string<delimiter(")content(%6s -> %s)char(\\n)delimiter(")>operator(,) local_variable($word)operator(,) ident(plural)operator(()local_variable($word)operator(\))operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_2.19) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_3.0) -comment(// PHP's date / time suport is quite extensive, and appears grouped into three areas of) -comment(// functionality:) -comment(//) -comment(// * UNIX / C Library [libc]-based routines, which include [among others]:) -comment(// - localtime, gmtime) -comment(// - strftime, strptime, mktime) -comment(// - time, getdate, gettimeofday, ) -comment(//) -comment(// * PHP 'native' functions, those date / time routines released in earlier versions,) -comment(// and which otherwise provide 'convenience' functionality; these include:) -comment(// - date) -comment(// - strtotime) -comment(//) -comment(// * 'DateTime' class-based. This facility appears [according to the PHP documentation]) -comment(// to be extremely new / experimental, so whilst usage examples will be provided, they) -comment(// should not be taken to be 'official' examples, and obviously, subject to change.) -comment(// My own impression is that this facility is currently only partially implemented,) -comment(// so there is limited use for these functions. The functions included in this group) -comment(// are some of the 'date_'-prefixed functions; they are, however, not used standalone,) -comment(// but as methods in conjunction with an object. Typical usage:) -comment(//) -comment(// $today = new DateTime(\); // actually calls: date_create($today, ...\);) -comment(// echo $today->format('U'\) . "\\n"; // actually calls: date_format($today, ...\);) -comment(//) -comment(// Also worth mentioning is the PEAR [PHP Extension and Repository] package, 'Calendar',) -comment(// which offers a rich set of date / time manipulation facilities. However, since it is) -comment(// not currently shipped with PHP, no examples appear) - -comment(// Helper functions for performing date arithmetic ) - -reserved(function) function(dateOffset)operator(()operator(\)) -operator({) - reserved(static) local_variable($tbl) operator(=) predefined(array)operator(()string<delimiter(')content(sec)delimiter(')> operator(=>) integer(1)operator(,) string<delimiter(')content(min)delimiter(')> operator(=>) integer(60)operator(,) string<delimiter(')content(hou)delimiter(')> operator(=>) integer(3600)operator(,) string<delimiter(')content(day)delimiter(')> operator(=>) integer(86400)operator(,) string<delimiter(')content(wee)delimiter(')> operator(=>) integer(604800)operator(\))operator(;) - local_variable($delta) operator(=) integer(0)operator(;) - - reserved(foreach) operator(()predefined(func_get_args)operator(()operator(\)) reserved(as) local_variable($arg)operator(\)) - operator({) - local_variable($kv) operator(=) predefined(explode)operator(()string<delimiter(')content(=)delimiter(')>operator(,) local_variable($arg)operator(\))operator(;) - local_variable($delta) operator(+=) local_variable($kv)operator([)integer(1)operator(]) operator(*) local_variable($tbl)operator([)predefined(strtolower)operator(()predefined(substr)operator(()local_variable($kv)operator([)integer(0)operator(])operator(,) integer(0)operator(,) integer(3)operator(\))operator(\))operator(])operator(;) - operator(}) - - reserved(return) local_variable($delta)operator(;) -operator(}) - -reserved(function) function(dateInterval)operator(()local_variable($intvltype)operator(,) local_variable($timevalue)operator(\)) -operator({) - reserved(static) local_variable($tbl) operator(=) predefined(array)operator(()string<delimiter(')content(sec)delimiter(')> operator(=>) integer(1)operator(,) string<delimiter(')content(min)delimiter(')> operator(=>) integer(60)operator(,) string<delimiter(')content(hou)delimiter(')> operator(=>) integer(3600)operator(,) string<delimiter(')content(day)delimiter(')> operator(=>) integer(86400)operator(,) string<delimiter(')content(wee)delimiter(')> operator(=>) integer(604800)operator(\))operator(;) - reserved(return) operator(()pre_type(int)operator(\)) predefined(round)operator(()local_variable($timevalue) operator(/) local_variable($tbl)operator([)predefined(strtolower)operator(()predefined(substr)operator(()local_variable($intvltype)operator(,) integer(0)operator(,) integer(3)operator(\))operator(\))operator(])operator(\))operator(;) -operator(}) - -comment(// ----------------------------) - -comment(// Extract indexed array from 'getdate') -local_variable($today) operator(=) predefined(getdate)operator(()operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(Today is day %d of the current year)char(\\n)delimiter(")>operator(,) local_variable($today)operator([)string<delimiter(')content(yday)delimiter(')>operator(])operator(\))operator(;) - -comment(// Extract indexed, and associative arrays, respectively, from 'localtime') -local_variable($today) operator(=) predefined(localtime)operator(()operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(Today is day %d of the current year)char(\\n)delimiter(")>operator(,) local_variable($today)operator([)integer(7)operator(])operator(\))operator(;) - -local_variable($today) operator(=) predefined(localtime)operator(()predefined(time)operator(()operator(\))operator(,) pre_constant(TRUE)operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(Today is day %d of the current year)char(\\n)delimiter(")>operator(,) local_variable($today)operator([)string<delimiter(')content(tm_yday)delimiter(')>operator(])operator(\))operator(;) - -comment(// @@PLEAC@@_3.1) -predefined(define)operator(()constant(SEP)operator(,) string<delimiter(')content(-)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($today) operator(=) predefined(getdate)operator(()operator(\))operator(;) - -local_variable($day) operator(=) local_variable($today)operator([)string<delimiter(')content(mday)delimiter(')>operator(])operator(;) -local_variable($month) operator(=) local_variable($today)operator([)string<delimiter(')content(mon)delimiter(')>operator(])operator(;) -local_variable($year) operator(=) local_variable($today)operator([)string<delimiter(')content(year)delimiter(')>operator(])operator(;) - -comment(// Either do this to use interpolation:) -local_variable($sep) operator(=) constant(SEP)operator(;) -predefined(echo) string<delimiter(")content(Current date is: )inline<delimiter({)local_variable($year)delimiter(})>inline<delimiter({)local_variable($sep)delimiter(})>inline<delimiter({)local_variable($month)delimiter(})>inline<delimiter({)local_variable($sep)delimiter(})>inline<delimiter({)local_variable($day)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// or simply concatenate:) -predefined(echo) string<delimiter(')content(Current date is: )delimiter(')> operator(.) local_variable($year) operator(.) constant(SEP) operator(.) local_variable($month) operator(.) constant(SEP) operator(.) local_variable($day) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// ------------) - -local_variable($today) operator(=) predefined(localtime)operator(()predefined(time)operator(()operator(\))operator(,) pre_constant(TRUE)operator(\))operator(;) - -local_variable($day) operator(=) local_variable($today)operator([)string<delimiter(')content(tm_mday)delimiter(')>operator(])operator(;) -local_variable($month) operator(=) local_variable($today)operator([)string<delimiter(')content(tm_mon)delimiter(')>operator(]) operator(+) integer(1)operator(;) -local_variable($year) operator(=) local_variable($today)operator([)string<delimiter(')content(tm_year)delimiter(')>operator(]) operator(+) integer(1900)operator(;) - -predefined(printf)operator(()string<delimiter(")content(Current date is: %4d%s%2d%s%2d)char(\\n)delimiter(")>operator(,) local_variable($year)operator(,) constant(SEP)operator(,) local_variable($month)operator(,) constant(SEP)operator(,) local_variable($day)operator(\))operator(;) - -comment(// ------------) - -local_variable($format) operator(=) string<delimiter(')content(Y)delimiter(')> operator(.) constant(SEP) operator(.) string<delimiter(')content(n)delimiter(')> operator(.) constant(SEP) operator(.) string<delimiter(')content(d)delimiter(')>operator(;) - -local_variable($today) operator(=) predefined(date)operator(()local_variable($format)operator(\))operator(;) - -predefined(echo) string<delimiter(")content(Current date is: )inline<delimiter({)local_variable($today)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ------------) - -local_variable($sep) operator(=) constant(SEP)operator(;) - -local_variable($today) operator(=) predefined(strftime)operator(()string<delimiter(")content(%Y)local_variable($sep)content(%m)local_variable($sep)content(%d)delimiter(")>operator(\))operator(;) - -predefined(echo) string<delimiter(")content(Current date is: )inline<delimiter({)local_variable($today)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_3.2) -local_variable($timestamp) operator(=) predefined(mktime)operator(()local_variable($hour)operator(,) local_variable($min)operator(,) local_variable($sec)operator(,) local_variable($month)operator(,) local_variable($day)operator(,) local_variable($year)operator(\))operator(;) - -local_variable($timestamp) operator(=) predefined(gmmktime)operator(()local_variable($hour)operator(,) local_variable($min)operator(,) local_variable($sec)operator(,) local_variable($month)operator(,) local_variable($day)operator(,) local_variable($year)operator(\))operator(;) - -comment(// @@PLEAC@@_3.3) -local_variable($dmyhms) operator(=) predefined(getdate)operator(()operator(\))operator(;) comment(// timestamp: current date / time) - -local_variable($dmyhms) operator(=) predefined(getdate)operator(()local_variable($timestamp)operator(\))operator(;) comment(// timestamp: arbitrary) - -local_variable($day) operator(=) local_variable($dmyhms)operator([)string<delimiter(')content(mday)delimiter(')>operator(])operator(;) -local_variable($month) operator(=) local_variable($dmyhms)operator([)string<delimiter(')content(mon)delimiter(')>operator(])operator(;) -local_variable($year) operator(=) local_variable($dmyhms)operator([)string<delimiter(')content(year)delimiter(')>operator(])operator(;) - -local_variable($hours) operator(=) local_variable($dmyhms)operator([)string<delimiter(')content(hours)delimiter(')>operator(])operator(;) -local_variable($minutes) operator(=) local_variable($dmyhms)operator([)string<delimiter(')content(minutes)delimiter(')>operator(])operator(;) -local_variable($seconds) operator(=) local_variable($dmyhms)operator([)string<delimiter(')content(seconds)delimiter(')>operator(])operator(;) - -comment(// @@PLEAC@@_3.4) -comment(// Date arithmetic is probably most easily performed using timestamps [i.e. *NIX Epoch) -comment(// Seconds]. Dates - in whatever form - are converted to timestamps, these are) -comment(// arithmetically manipulated, and the result converted to whatever form required.) -comment(// Note: use 'mktime' to create timestamps properly adjusted for daylight saving; whilst) -comment(// 'strtotime' is more convenient to use, it does not, AFAIK, include this adjustment) - -local_variable($when) operator(=) local_variable($now) operator(+) local_variable($difference)operator(;) -local_variable($then) operator(=) local_variable($now) operator(-) local_variable($difference)operator(;) - -comment(// ------------) - -local_variable($now) operator(=) predefined(mktime)operator(()integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) integer(8)operator(,) integer(6)operator(,) integer(2003)operator(\))operator(;) - -local_variable($diff1) operator(=) ident(dateOffset)operator(()string<delimiter(')content(day=1)delimiter(')>operator(\))operator(;) local_variable($diff2) operator(=) ident(dateOffset)operator(()string<delimiter(')content(weeks=2)delimiter(')>operator(\))operator(;) - -predefined(echo) string<delimiter(')content(Today is: )delimiter(')> operator(.) predefined(date)operator(()string<delimiter(')content(Y-m-d)delimiter(')>operator(,) local_variable($now)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) string<delimiter(')content(One day in the future is: )delimiter(')> operator(.) predefined(date)operator(()string<delimiter(')content(Y-m-d)delimiter(')>operator(,) local_variable($now) operator(+) local_variable($diff1)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) string<delimiter(')content(Two weeks in the past is: )delimiter(')> operator(.) predefined(date)operator(()string<delimiter(')content(Y-m-d)delimiter(')>operator(,) local_variable($now) operator(-) local_variable($diff2)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -comment(// Date arithmetic performed using a custom function, 'dateOffset'. Internally, offset may) -comment(// be computed in one of several ways:) -comment(// * Direct timestamp manipulation - fastest, but no daylight saving adjustment ) -comment(// * Via 'date' built-in function - slower [?], needs a base time from which to) -comment(// compute values, but has daylight saving adjustment ) -comment(// * Via 'strtotime' built-in function - as for 'date') -comment(// * Via 'DateTime' class) -comment(//) -comment(// Approach used here is to utilise direct timestamp manipulation in 'dateOffset' [it's) -comment(// performance can also be improved by replacing $tbl with a global definition etc],) -comment(// and to illustrate how the other approaches might be used ) - -comment(// 1. 'dateOffset') - -local_variable($birthtime) operator(=) predefined(mktime)operator(()integer(3)operator(,) integer(45)operator(,) integer(50)operator(,) integer(1)operator(,) integer(18)operator(,) integer(1973)operator(\))operator(;) - -local_variable($interval) operator(=) ident(dateOffset)operator(()string<delimiter(')content(day=55)delimiter(')>operator(,) string<delimiter(')content(hours=2)delimiter(')>operator(,) string<delimiter(')content(min=17)delimiter(')>operator(,) string<delimiter(')content(sec=5)delimiter(')>operator(\))operator(;) - -local_variable($then) operator(=) local_variable($birthtime) operator(+) local_variable($interval)operator(;) - -predefined(printf)operator(()string<delimiter(")content(Birthtime is: %s)char(\\n)content(then is: %s)char(\\n)delimiter(")>operator(,) predefined(date)operator(()constant(DATE_RFC1123)operator(,) local_variable($birthtime)operator(\))operator(,) predefined(date)operator(()constant(DATE_RFC1123)operator(,) local_variable($then)operator(\))operator(\))operator(;) - -comment(// ------------) - -comment(// 2. 'date') - -comment(// Base values, and offsets, respectively) -local_variable($hr) operator(=) integer(3)operator(;) local_variable($min) operator(=) integer(45)operator(;) local_variable($sec) operator(=) integer(50)operator(;) local_variable($mon) operator(=) integer(1)operator(;) local_variable($day) operator(=) integer(18)operator(;) local_variable($year) operator(=) integer(1973)operator(;) - -local_variable($yroff) operator(=) integer(0)operator(;) local_variable($monoff) operator(=) integer(0)operator(;) local_variable($dayoff) operator(=) integer(55)operator(;) local_variable($hroff) operator(=) integer(2)operator(;) local_variable($minoff) operator(=) integer(17)operator(;) local_variable($secoff) operator(=) integer(5)operator(;) - -comment(// Base date) -local_variable($birthtime) operator(=) predefined(mktime)operator(()local_variable($hr)operator(,) local_variable($min)operator(,) local_variable($sec)operator(,) local_variable($mon)operator(,) local_variable($day)operator(,) local_variable($year)operator(,) pre_constant(TRUE)operator(\))operator(;) - -local_variable($year) operator(=) predefined(date)operator(()string<delimiter(')content(Y)delimiter(')>operator(,) local_variable($birthtime)operator(\)) operator(+) local_variable($yroff)operator(;) -local_variable($mon) operator(=) predefined(date)operator(()string<delimiter(')content(m)delimiter(')>operator(,) local_variable($birthtime)operator(\)) operator(+) local_variable($monoff)operator(;) -local_variable($day) operator(=) predefined(date)operator(()string<delimiter(')content(d)delimiter(')>operator(,) local_variable($birthtime)operator(\)) operator(+) local_variable($dayoff)operator(;) - -local_variable($hr) operator(=) predefined(date)operator(()string<delimiter(')content(H)delimiter(')>operator(,) local_variable($birthtime)operator(\)) operator(+) local_variable($hroff)operator(;) -local_variable($min) operator(=) predefined(date)operator(()string<delimiter(')content(i)delimiter(')>operator(,) local_variable($birthtime)operator(\)) operator(+) local_variable($minoff)operator(;) -local_variable($sec) operator(=) predefined(date)operator(()string<delimiter(')content(s)delimiter(')>operator(,) local_variable($birthtime)operator(\)) operator(+) local_variable($secoff)operator(;) - -comment(// Offset date) -local_variable($then) operator(=) predefined(mktime)operator(()local_variable($hr)operator(,) local_variable($min)operator(,) local_variable($sec)operator(,) local_variable($mon)operator(,) local_variable($day)operator(,) local_variable($year)operator(,) pre_constant(TRUE)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(Birthtime is: %s)char(\\n)content(then is: %s)char(\\n)delimiter(")>operator(,) predefined(date)operator(()constant(DATE_RFC1123)operator(,) local_variable($birthtime)operator(\))operator(,) predefined(date)operator(()constant(DATE_RFC1123)operator(,) local_variable($then)operator(\))operator(\))operator(;) - -comment(// ------------) - -comment(// 3. 'strtotime') - -comment(// Generate timestamp whatever way is preferable) -local_variable($birthtime) operator(=) predefined(mktime)operator(()integer(3)operator(,) integer(45)operator(,) integer(50)operator(,) integer(1)operator(,) integer(18)operator(,) integer(1973)operator(\))operator(;) -local_variable($birthtime) operator(=) predefined(strtotime)operator(()string<delimiter(')content(1/18/1973 03:45:50)delimiter(')>operator(\))operator(;) - -local_variable($then) operator(=) predefined(strtotime)operator(()string<delimiter(')content(+55 days 2 hours 17 minutes 2 seconds)delimiter(')>operator(,) local_variable($birthtime)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(Birthtime is: %s)char(\\n)content(then is: %s)char(\\n)delimiter(")>operator(,) predefined(date)operator(()constant(DATE_RFC1123)operator(,) local_variable($birthtime)operator(\))operator(,) predefined(date)operator(()constant(DATE_RFC1123)operator(,) local_variable($then)operator(\))operator(\))operator(;) - -comment(// ------------) - -comment(// 4. 'DateTime' class) - -local_variable($birthtime) operator(=) reserved(new) constant(DateTime)operator(()string<delimiter(')content(1/18/1973 03:45:50)delimiter(')>operator(\))operator(;) -local_variable($then) operator(=) reserved(new) constant(DateTime)operator(()string<delimiter(')content(1/18/1973 03:45:50)delimiter(')>operator(\))operator(;) -local_variable($then)operator(->)ident(modify)operator(()string<delimiter(')content(+55 days 2 hours 17 minutes 2 seconds)delimiter(')>operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(Birthtime is: %s)char(\\n)content(then is: %s)char(\\n)delimiter(")>operator(,) local_variable($birthtime)operator(->)ident(format)operator(()constant(DATE_RFC1123)operator(\))operator(,) local_variable($then)operator(->)ident(format)operator(()constant(DATE_RFC1123)operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_3.5) -comment(// Date intervals are most easily computed using timestamps [i.e. *NIX Epoch) -comment(// Seconds] which, of course, gives the interval result is seconds from which) -comment(// all other interval measures [days, weeks, months, years] may be derived.) -comment(// Refer to previous section for discussion of daylight saving and other related) -comment(// problems) - -local_variable($interval_seconds) operator(=) local_variable($recent) operator(-) local_variable($earlier)operator(;) - -comment(// ----------------------------) - -comment(// Conventional approach ...) -local_variable($bree) operator(=) predefined(strtotime)operator(()string<delimiter(')content(16 Jun 1981, 4:35:25)delimiter(')>operator(\))operator(;) -local_variable($nat) operator(=) predefined(strtotime)operator(()string<delimiter(')content(18 Jan 1973, 3:45:50)delimiter(')>operator(\))operator(;) - -comment(// ... or, with daylight saving adjustment) -local_variable($bree) operator(=) predefined(mktime)operator(()integer(4)operator(,) integer(35)operator(,) integer(25)operator(,) integer(6)operator(,) integer(16)operator(,) integer(1981)operator(,) pre_constant(TRUE)operator(\))operator(;) -local_variable($nat) operator(=) predefined(mktime)operator(()integer(3)operator(,) integer(45)operator(,) integer(50)operator(,) integer(1)operator(,) integer(18)operator(,) integer(1973)operator(,) pre_constant(TRUE)operator(\))operator(;) - -local_variable($difference) operator(=) local_variable($bree) operator(-) local_variable($nat)operator(;) - -comment(// 'dateInterval' custom function computes intervals in several measures given an) -comment(// interval in seconds. Note, 'month' and 'year' measures not provided) -predefined(printf)operator(()string<delimiter(")content(There were %d seconds between Nat and Bree)char(\\n)delimiter(")>operator(,) local_variable($difference)operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(There were %d weeks between Nat and Bree)char(\\n)delimiter(")>operator(,) ident(dateInterval)operator(()string<delimiter(')content(weeks)delimiter(')>operator(,) local_variable($difference)operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(There were %d days between Nat and Bree)char(\\n)delimiter(")>operator(,) ident(dateInterval)operator(()string<delimiter(')content(days)delimiter(')>operator(,) local_variable($difference)operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(There were %d hours between Nat and Bree)char(\\n)delimiter(")>operator(,) ident(dateInterval)operator(()string<delimiter(')content(hours)delimiter(')>operator(,) local_variable($difference)operator(\))operator(\))operator(;) -predefined(printf)operator(()string<delimiter(")content(There were %d minutes between Nat and Bree)char(\\n)delimiter(")>operator(,) ident(dateInterval)operator(()string<delimiter(')content(mins)delimiter(')>operator(,) local_variable($difference)operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_3.6) -comment(// 'getdate' accepts a timestamp [or implicitly calls 'time'] and returns an array of) -comment(// date components. It returns much the same information as 'strptime' except that) -comment(// the component names are different) - -local_variable($today) operator(=) predefined(getdate)operator(()operator(\))operator(;) - -local_variable($weekday) operator(=) local_variable($today)operator([)string<delimiter(')content(wday)delimiter(')>operator(])operator(;) -local_variable($monthday) operator(=) local_variable($today)operator([)string<delimiter(')content(mday)delimiter(')>operator(])operator(;) -local_variable($yearday) operator(=) local_variable($today)operator([)string<delimiter(')content(yday)delimiter(')>operator(])operator(;) - -local_variable($weeknumber) operator(=) operator(()pre_type(int)operator(\)) predefined(round)operator(()local_variable($yearday) operator(/) float(7.0)operator(\))operator(;) - -comment(// Safter method of obtaining week number) -local_variable($weeknumber) operator(=) predefined(strftime)operator(()string<delimiter(')content(%U)delimiter(')>operator(\)) operator(+) integer(1)operator(;) - -comment(// ----------------------------) - -predefined(define)operator(()constant(SEP)operator(,) string<delimiter(')content(/)delimiter(')>operator(\))operator(;) - -local_variable($day) operator(=) integer(16)operator(;) -local_variable($month) operator(=) integer(6)operator(;) -local_variable($year) operator(=) integer(1981)operator(;) - -local_variable($timestamp) operator(=) predefined(mktime)operator(()integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) local_variable($month)operator(,) local_variable($day)operator(,) local_variable($year)operator(\))operator(;) - -local_variable($date) operator(=) predefined(getdate)operator(()local_variable($timestamp)operator(\))operator(;) - -local_variable($weekday) operator(=) local_variable($date)operator([)string<delimiter(')content(wday)delimiter(')>operator(])operator(;) -local_variable($monthday) operator(=) local_variable($date)operator([)string<delimiter(')content(mday)delimiter(')>operator(])operator(;) -local_variable($yearday) operator(=) local_variable($date)operator([)string<delimiter(')content(yday)delimiter(')>operator(])operator(;) - -local_variable($weeknumber) operator(=) operator(()pre_type(int)operator(\)) predefined(round)operator(()local_variable($yearday) operator(/) float(7.0)operator(\))operator(;) - -local_variable($weeknumber) operator(=) predefined(strftime)operator(()string<delimiter(')content(%U)delimiter(')>operator(,) local_variable($timestamp)operator(\)) operator(+) integer(1)operator(;) - -comment(// Interpolate ...) -local_variable($sep) operator(=) constant(SEP)operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($month)delimiter(})>inline<delimiter({)local_variable($sep)delimiter(})>inline<delimiter({)local_variable($day)delimiter(})>inline<delimiter({)local_variable($sep)delimiter(})>inline<delimiter({)local_variable($year)delimiter(})>content( was a )inline<delimiter({)local_variable($date)operator([)string<delimiter(')content(weekday)delimiter(')>operator(])delimiter(})>content( in week )inline<delimiter({)local_variable($weeknumber)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ... or, concatenate) -predefined(echo) local_variable($month) operator(.) constant(SEP) operator(.) local_variable($day) operator(.) constant(SEP) operator(.) local_variable($year) operator(.) string<delimiter(')content( was a )delimiter(')> operator(.) local_variable($date)operator([)string<delimiter(')content(weekday)delimiter(')>operator(]) - operator(.) string<delimiter(')content( in week )delimiter(')> operator(.) local_variable($weeknumber) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_3.7) -comment(// 'strtotime' parses a textual date expression by attempting a 'best guess' at) -comment(// the format, and either fails, or generates a timestamp. Timestamp could be fed) -comment(// into any one of the various functions; example:) -local_variable($timestamp) operator(=) predefined(strtotime)operator(()string<delimiter(')content(1998-06-03)delimiter(')>operator(\))operator(;) predefined(echo) predefined(strftime)operator(()string<delimiter(')content(%Y-%m-%d)delimiter(')>operator(,) local_variable($timestamp)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// 'strptime' parses a textual date expression according to a specified format,) -comment(// and returns an array of date components; components can be easily dumped) -predefined(print_r)operator(()predefined(strptime)operator(()string<delimiter(')content(1998-06-03)delimiter(')>operator(,) string<delimiter(')content(%Y-%m-%d)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Parse date string according to format) -local_variable($darr) operator(=) predefined(strptime)operator(()string<delimiter(')content(1998-06-03)delimiter(')>operator(,) string<delimiter(')content(%Y-%m-%d)delimiter(')>operator(\))operator(;) - -reserved(if) operator(()operator(!)predefined(empty)operator(()local_variable($darr)operator(\))operator(\)) -operator({) - comment(// Show date components in 'debug' form) - predefined(print_r)operator(()local_variable($darr)operator(\))operator(;) - - comment(// Check whether there was a parse error i.e. one or more components could not) - comment(// be extracted from the string) - reserved(if) operator(()predefined(empty)operator(()local_variable($darr)operator([)string<delimiter(')content(unparsed)delimiter(')>operator(])operator(\))operator(\)) - operator({) - comment(// Properly parsed date, so validate required components using, 'checkdate') - reserved(if) operator(()predefined(checkdate)operator(()local_variable($darr)operator([)string<delimiter(')content(tm_mon)delimiter(')>operator(]) operator(+) integer(1)operator(,) local_variable($darr)operator([)string<delimiter(')content(tm_mday)delimiter(')>operator(])operator(,) local_variable($darr)operator([)string<delimiter(')content(tm_year)delimiter(')>operator(]) operator(+) integer(1900)operator(\))operator(\)) - predefined(echo) string<delimiter(")content(Parsed date verified as correct)char(\\n)delimiter(")>operator(;) - reserved(else) - predefined(echo) string<delimiter(")content(Parsed date failed verification)char(\\n)delimiter(")>operator(;) - operator(}) - reserved(else) - operator({) - predefined(echo) string<delimiter(")content(Date string parse not complete; failed components: )inline<delimiter({)local_variable($darr)operator([)string<delimiter(')content(unparsed)delimiter(')>operator(])delimiter(})>char(\\n)delimiter(")>operator(;) - operator(}) -operator(}) -reserved(else) -operator({) - predefined(echo) string<delimiter(")content(Date string could not be parsed)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_3.8) -comment(// 'date' and 'strftime' both print a date string based on:) -comment(// * Format String, describing layout of date components) -comment(// * Timestamp [*NIX Epoch Seconds], either given explicitly, or implictly) -comment(// via a call to 'time' which retrieves current time value) - -local_variable($ts) operator(=) integer(1234567890)operator(;) - -predefined(date)operator(()string<delimiter(')content(Y/m/d)delimiter(')>operator(,) local_variable($ts)operator(\))operator(;) -predefined(date)operator(()string<delimiter(')content(Y/m/d)delimiter(')>operator(,) predefined(mktime)operator(()local_variable($h)operator(,) local_variable($m)operator(,) local_variable($s)operator(,) local_variable($mth)operator(,) local_variable($d)operator(,) local_variable($y)operator(,) local_variable($is_dst)operator(\))operator(\))operator(;) - -predefined(date)operator(()string<delimiter(')content(Y/m/d)delimiter(')>operator(\))operator(;) comment(// same as: date('Y/m/d', time(\)\);) - -comment(// ------------) - -local_variable($ts) operator(=) integer(1234567890)operator(;) - -predefined(strftime)operator(()string<delimiter(')content(%Y/%m/%d)delimiter(')>operator(,) local_variable($ts)operator(\))operator(;) -predefined(strftime)operator(()string<delimiter(')content(%Y/%m/%d)delimiter(')>operator(,) predefined(mktime)operator(()local_variable($h)operator(,) local_variable($m)operator(,) local_variable($s)operator(,) local_variable($mth)operator(,) local_variable($d)operator(,) local_variable($y)operator(,) local_variable($is_dst)operator(\))operator(\))operator(;) - -predefined(strftime)operator(()string<delimiter(')content(%Y/%m/%d)delimiter(')>operator(\))operator(;) comment(// same as: strftime('%Y/%m/%d', time(\)\);) - -comment(// ----------------------------) - -comment(// 'mktime' creates a local time timestamp) -local_variable($t) operator(=) predefined(strftime)operator(()string<delimiter(')content(%a %b %e %H:%M:%S %z %Y)delimiter(')>operator(,) predefined(mktime)operator(()integer(3)operator(,) integer(45)operator(,) integer(50)operator(,) integer(1)operator(,) integer(18)operator(,) integer(73)operator(,) pre_constant(TRUE)operator(\))operator(\))operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($t)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// 'gmmktime' creates a GMT time timestamp) -local_variable($t) operator(=) predefined(strftime)operator(()string<delimiter(')content(%a %b %e %H:%M:%S %z %Y)delimiter(')>operator(,) predefined(gmmktime)operator(()integer(3)operator(,) integer(45)operator(,) integer(50)operator(,) integer(1)operator(,) integer(18)operator(,) integer(73)operator(\))operator(\))operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($t)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -comment(// 'strtotime' parses a textual date expression, and generates a timestamp ) -local_variable($t) operator(=) predefined(strftime)operator(()string<delimiter(')content(%A %D)delimiter(')>operator(,) predefined(strtotime)operator(()string<delimiter(')content(18 Jan 1973, 3:45:50)delimiter(')>operator(\))operator(\))operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($t)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// This should generate output identical to previous example) -local_variable($t) operator(=) predefined(strftime)operator(()string<delimiter(')content(%A %D)delimiter(')>operator(,) predefined(mktime)operator(()integer(3)operator(,) integer(45)operator(,) integer(50)operator(,) integer(1)operator(,) integer(18)operator(,) integer(73)operator(,) pre_constant(TRUE)operator(\))operator(\))operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($t)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_3.9) -comment(// PHP 5 and above can use the built-in, 'microtime'. Crude implementation for ealier versions:) -comment(// function microtime(\) { $t = gettimeofday(\); return (float\) ($t['sec'] + $t['usec'] / 1000000.0\); } ) - -comment(// ------------) - -local_variable($before) operator(=) predefined(microtime)operator(()operator(\))operator(;) - -local_variable($line) operator(=) predefined(fgets)operator(()constant(STDIN)operator(\))operator(;) - -local_variable($elapsed) operator(=) predefined(microtime)operator(()operator(\)) operator(-) local_variable($before)operator(;) - -predefined(printf)operator(()string<delimiter(")content(You took %.3f seconds)char(\\n)delimiter(")>operator(,) local_variable($elapsed)operator(\))operator(;) - -comment(// ------------) - -predefined(define)operator(()constant(NUMBER_OF_TIMES)operator(,) integer(100)operator(\))operator(;) -predefined(define)operator(()constant(SIZE)operator(,) integer(500)operator(\))operator(;) - -reserved(for)operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) constant(NUMBER_OF_TIMES)operator(;) local_variable($i)operator(++)operator(\)) -operator({) - local_variable($arr) operator(=) predefined(array)operator(()operator(\))operator(;) - reserved(for)operator(()local_variable($j) operator(=) integer(0)operator(;) local_variable($j) operator(<) constant(SIZE)operator(;) local_variable($j)operator(++)operator(\)) local_variable($arr)operator([)operator(]) operator(=) predefined(rand)operator(()operator(\))operator(;) - - local_variable($begin) operator(=) predefined(microtime)operator(()operator(\))operator(;) - predefined(sort)operator(()local_variable($arr)operator(\))operator(;) - local_variable($elapsed) operator(=) predefined(microtime)operator(()operator(\)) operator(-) local_variable($begin)operator(;) - - local_variable($total_time) operator(+=) local_variable($elapsed)operator(;) -operator(}) - -predefined(printf)operator(()string<delimiter(")content(On average, sorting %d random numbers takes %.5f seconds)char(\\n)delimiter(")>operator(,) constant(SIZE)operator(,) local_variable($total_time) operator(/) operator(()pre_type(float)operator(\)) constant(NUMBER_OF_TIMES)operator(\))operator(;) - -comment(// @@PLEAC@@_3.10) -comment(// Low-resolution: sleep time specified in seconds) -predefined(sleep)operator(()integer(1)operator(\))operator(;) - -comment(// High-resolution: sleep time specified in microseconds [not reliable under Windows]) -predefined(usleep)operator(()integer(250000)operator(\))operator(;) - -comment(// @@PLEAC@@_3.11) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_4.0) -comment(// Nested arrays are supported, and may be easily printed using 'print_r') - -local_variable($nested) operator(=) predefined(array)operator(()string<delimiter(')content(this)delimiter(')>operator(,) string<delimiter(')content(that)delimiter(')>operator(,) string<delimiter(')content(the)delimiter(')>operator(,) string<delimiter(')content(other)delimiter(')>operator(\))operator(;) - -local_variable($nested) operator(=) predefined(array)operator(()string<delimiter(')content(this)delimiter(')>operator(,) string<delimiter(')content(that)delimiter(')>operator(,) predefined(array)operator(()string<delimiter(')content(the)delimiter(')>operator(,) string<delimiter(')content(other)delimiter(')>operator(\))operator(\))operator(;) predefined(print_r)operator(()local_variable($nested)operator(\))operator(;) - -local_variable($tune) operator(=) predefined(array)operator(()string<delimiter(')content(The)delimiter(')>operator(,) string<delimiter(')content(Star-Spangled)delimiter(')>operator(,) string<delimiter(')content(Banner)delimiter(')>operator(\))operator(;) - -comment(// @@PLEAC@@_4.1) -comment(// PHP offers only the 'array' type which is actually an associative array, though) -comment(// may be numerically indexed, to mimic vectors and matrices; there is no separate) -comment(// 'list' type) - -local_variable($a) operator(=) predefined(array)operator(()string<delimiter(')content(quick)delimiter(')>operator(,) string<delimiter(')content(brown)delimiter(')>operator(,) string<delimiter(')content(fox)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($a) operator(=) predefined(escapeshellarg)operator(()string<delimiter(')content(Why are you teasing me?)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($lines) operator(=) string<delimiter(<<<END_OF_HERE_DOC)content( -)content( The boy stood on the burning deck,)content( -)content( it was as hot as glass.)content( -)delimiter(END_OF_HERE_DOC)>operator(;) - -comment(// ------------) - -local_variable($bigarray) operator(=) predefined(array_map)operator(()string<delimiter(')content(rtrim)delimiter(')>operator(,) predefined(file)operator(()string<delimiter(')content(mydatafile)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($banner) operator(=) string<delimiter(')content(The mines of Moria)delimiter(')>operator(;) - -local_variable($banner) operator(=) predefined(escapeshellarg)operator(()string<delimiter(')content(The mines of Moria)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($name) operator(=) string<delimiter(')content(Gandalf)delimiter(')>operator(;) - -local_variable($banner) operator(=) string<delimiter(")content(Speak )inline<delimiter({)local_variable($name)delimiter(})>content(, and enter!)delimiter(")>operator(;) - -local_variable($banner) operator(=) string<delimiter(')content(Speak )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($name)operator(\)) operator(.) string<delimiter(')content( and welcome!)delimiter(')>operator(;) - -comment(// ------------) - -local_variable($his_host) operator(=) string<delimiter(')content(www.perl.com)delimiter(')>operator(;) - -local_variable($host_info) operator(=) string<delimiter(`)content(nslookup )local_variable($his_host)delimiter(`)>operator(;) - -local_variable($cmd) operator(=) string<delimiter(')content(ps )delimiter(')> operator(.) predefined(posix_getpid)operator(()operator(\))operator(;) local_variable($perl_info) operator(=) string<delimiter(`)local_variable($cmd)delimiter(`)>operator(;) - -local_variable($shell_info) operator(=) string<delimiter(`)content(ps )content($)content($)delimiter(`)>operator(;) - -comment(// ------------) - -local_variable($banner) operator(=) predefined(array)operator(()string<delimiter(')content(Costs)delimiter(')>operator(,) string<delimiter(')content(only)delimiter(')>operator(,) string<delimiter(')content($4.95)delimiter(')>operator(\))operator(;) - -local_variable($banner) operator(=) predefined(array_map)operator(()string<delimiter(')content(escapeshellarg)delimiter(')>operator(,) predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(Costs only $4.95)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -comment(// AFAIK PHP doesn't support non-quoted strings ala Perl's 'q', 'qq' and 'qw', so arrays) -comment(// created from strings must use quoted strings, and make use of 'split' [or equivalent].) -comment(// A slew of functions exist for performing string quoting, including 'escapeshellarg',) -comment(// 'quotemeta', and 'preg_quote') - -local_variable($brax) operator(=) predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(( \) < > { } [ ])delimiter(')>operator(\))operator(;) - -comment(// Do this to quote each element within '..') -comment(// $brax = array_map('escapeshellarg', split(' ', '( \) < > { } [ ]'\)\);) - -local_variable($rings) operator(=) predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(Nenya Narya Vilya)delimiter(')>operator(\))operator(;) - -local_variable($tags) operator(=) predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(LI TABLE TR TD A IMG H1 P)delimiter(')>operator(\))operator(;) - -local_variable($sample) operator(=) predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(The vertical bar | looks and behaves like a pipe.)delimiter(')>operator(\))operator(;) - -comment(// @@PLEAC@@_4.2) -reserved(function) function(commify_series)operator(()local_variable($list)operator(\)) -operator({) - local_variable($n) operator(=) predefined(str_word_count)operator(()local_variable($list)operator(\))operator(;) local_variable($series) operator(=) predefined(str_word_count)operator(()local_variable($list)operator(,) integer(1)operator(\))operator(;) - - reserved(if) operator(()local_variable($n) operator(==) integer(0)operator(\)) reserved(return) pre_constant(NULL)operator(;) - reserved(if) operator(()local_variable($n) operator(==) integer(1)operator(\)) reserved(return) local_variable($series)operator([)integer(0)operator(])operator(;) - reserved(if) operator(()local_variable($n) operator(==) integer(2)operator(\)) reserved(return) local_variable($series)operator([)integer(0)operator(]) operator(.) string<delimiter(')content( and )delimiter(')> operator(.) local_variable($series)operator([)integer(1)operator(])operator(;) - - reserved(return) predefined(join)operator(()string<delimiter(')content(, )delimiter(')>operator(,) predefined(array_slice)operator(()local_variable($series)operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(\)) operator(.) string<delimiter(')content(, and )delimiter(')> operator(.) local_variable($series)operator([)local_variable($n) operator(-) integer(1)operator(])operator(;) -operator(}) - -comment(// ------------) - -predefined(echo) ident(commify_series)operator(()string<delimiter(')content(red)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) ident(commify_series)operator(()string<delimiter(')content(red yellow)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) ident(commify_series)operator(()string<delimiter(')content(red yellow green)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -local_variable($mylist) operator(=) string<delimiter(')content(red yellow green)delimiter(')>operator(;) -predefined(echo) string<delimiter(')content(I have )delimiter(')> operator(.) ident(commify_series)operator(()local_variable($mylist)operator(\)) operator(.) string<delimiter(")content( marbles.)char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -reserved(function) function(commify_series)operator(()local_variable($arr)operator(\)) -operator({) - local_variable($n) operator(=) predefined(count)operator(()local_variable($arr)operator(\))operator(;) local_variable($sepchar) operator(=) string<delimiter(')content(,)delimiter(')>operator(;) - - reserved(foreach)operator(()local_variable($arr) reserved(as) local_variable($str)operator(\)) - operator({) - reserved(if) operator(()predefined(strpos)operator(()local_variable($str)operator(,) string<delimiter(')content(,)delimiter(')>operator(\)) operator(===) pre_constant(false)operator(\)) reserved(continue)operator(;) - local_variable($sepchar) operator(=) string<delimiter(')content(;)delimiter(')>operator(;) reserved(break)operator(;) - operator(}) - - reserved(if) operator(()local_variable($n) operator(==) integer(0)operator(\)) reserved(return) pre_constant(NULL)operator(;) - reserved(if) operator(()local_variable($n) operator(==) integer(1)operator(\)) reserved(return) local_variable($arr)operator([)integer(0)operator(])operator(;) - reserved(if) operator(()local_variable($n) operator(==) integer(2)operator(\)) reserved(return) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(')content( and )delimiter(')> operator(.) local_variable($arr)operator([)integer(1)operator(])operator(;) - - reserved(return) predefined(join)operator(()string<delimiter(")inline<delimiter({)local_variable($sepchar)delimiter(})>content( )delimiter(")>operator(,) predefined(array_slice)operator(()local_variable($arr)operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(\)) operator(.) string<delimiter(")inline<delimiter({)local_variable($sepchar)delimiter(})>content( and )delimiter(")> operator(.) local_variable($arr)operator([)local_variable($n) operator(-) integer(1)operator(])operator(;) -operator(}) - -comment(// ------------) - -local_variable($lists) operator(=) predefined(array)operator(() - predefined(array)operator(()string<delimiter(')content(just one thing)delimiter(')>operator(\))operator(,) - predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(Mutt Jeff)delimiter(')>operator(\))operator(,) - predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(Peter Paul Mary)delimiter(')>operator(\))operator(,) - predefined(array)operator(()string<delimiter(')content(To our parents)delimiter(')>operator(,) string<delimiter(')content(Mother Theresa)delimiter(')>operator(,) string<delimiter(')content(God)delimiter(')>operator(\))operator(,) - predefined(array)operator(()string<delimiter(')content(pastrami)delimiter(')>operator(,) string<delimiter(')content(ham and cheese)delimiter(')>operator(,) string<delimiter(')content(peanut butter and jelly)delimiter(')>operator(,) string<delimiter(')content(tuna)delimiter(')>operator(\))operator(,) - predefined(array)operator(()string<delimiter(')content(recycle tired, old phrases)delimiter(')>operator(,) string<delimiter(')content(ponder big, happy thoughts)delimiter(')>operator(\))operator(,) - predefined(array)operator(()string<delimiter(')content(recycle tired, old phrases)delimiter(')>operator(,) string<delimiter(')content(ponder big, happy thoughts)delimiter(')>operator(,) string<delimiter(')content(sleep and dream peacefully)delimiter(')>operator(\))operator(\))operator(;) - -reserved(foreach)operator(()local_variable($lists) reserved(as) local_variable($arr)operator(\)) -operator({) - predefined(echo) string<delimiter(')content(The list is: )delimiter(')> operator(.) ident(commify_series)operator(()local_variable($arr)operator(\)) operator(.) string<delimiter(")content(.)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_4.3) -comment(// AFAICT you cannot grow / shrink an array to an arbitrary size. However, you can:) -comment(// * Grow an array by appending an element using subscrip notation, or using) -comment(// either 'array_unshift' or 'array_push' to add one or more elements) - -local_variable($arr)operator([)operator(]) operator(=) string<delimiter(')content(one)delimiter(')>operator(;) -predefined(array_unshift)operator(()local_variable($arr)operator(,) string<delimiter(')content(one)delimiter(')>operator(,) string<delimiter(')content(two)delimiter(')>operator(,) string<delimiter(')content(three)delimiter(')>operator(\))operator(;) -predefined(array_push)operator(()local_variable($arr)operator(,) string<delimiter(')content(one)delimiter(')>operator(,) string<delimiter(')content(two)delimiter(')>operator(,) string<delimiter(')content(three)delimiter(')>operator(\))operator(;) - -comment(// * Shrink an array by using 'unset' to remove one or more specific elements, or) -comment(// either 'array_shift' or 'array_pop' to remove an element from the ends) - -predefined(unset)operator(()local_variable($arr)operator([)local_variable($idx1)operator(])operator(,) local_variable($arr)operator([)local_variable($idx2)operator(])operator(,) local_variable($arr)operator([)local_variable($idx3)operator(])operator(\))operator(;) -local_variable($item) operator(=) predefined(array_shift)operator(()local_variable($arr)operator(\))operator(;) -local_variable($item) operator(=) predefined(array_pop)operator(()local_variable($arr)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(what_about_the_array)operator(()operator(\)) -operator({) - reserved(global) local_variable($people)operator(;) - - predefined(echo) string<delimiter(')content(The array now has )delimiter(')> operator(.) predefined(count)operator(()local_variable($people)operator(\)) operator(.) string<delimiter(")content( elements)char(\\n)delimiter(")>operator(;) - predefined(echo) string<delimiter(')content(The index value of the last element is )delimiter(')> operator(.) operator(()predefined(count)operator(()local_variable($people)operator(\)) operator(-) integer(1)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - predefined(echo) string<delimiter(')content(Element #3 is )delimiter(')> operator(.) local_variable($people)operator([)integer(3)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -local_variable($people) operator(=) predefined(array)operator(()string<delimiter(')content(Crosby)delimiter(')>operator(,) string<delimiter(')content(Stills)delimiter(')>operator(,) string<delimiter(')content(Nash)delimiter(')>operator(,) string<delimiter(')content(Young)delimiter(')>operator(\))operator(;) -ident(what_about_the_array)operator(()operator(\))operator(;) - -predefined(array_pop)operator(()local_variable($people)operator(\))operator(;) -ident(what_about_the_array)operator(()operator(\))operator(;) - -comment(// Cannot, AFAICT, resize the array to an arbitrary size) - -comment(# @@PLEAC@@_4.4) -reserved(foreach) operator(()local_variable($list) reserved(as) local_variable($item)operator(\)) operator({) - comment(// do something with $item) -operator(}) - -comment(// Environment listing example) - -comment(// PHP defines a superglobal $_ENV to provide access to environment) -comment(// variables.) - -comment(// Beware, array assignment means copying in PHP. You need to use) -comment(// the reference operator to avoid copying. But we want a copy here.) -local_variable($env) operator(=) predefined($_ENV)operator(;) - -comment(// PHP can sort an array by key, so you don't need to get keys,) -comment(// and then sort.) -predefined(ksort)operator(()local_variable($env)operator(\))operator(;) - -reserved(foreach) operator(()local_variable($env) reserved(as) local_variable($key) operator(=>) local_variable($value)operator(\)) operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($key)delimiter(})>content(=)inline<delimiter({)local_variable($value)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// Literal translation of Perl example would be:) -local_variable($keys) operator(=) predefined(array_keys)operator(()predefined($_ENV)operator(\))operator(;) -predefined(sort)operator(()local_variable($keys)operator(\))operator(;) -reserved(foreach) operator(()local_variable($keys) reserved(as) local_variable($key)operator(\)) operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($key)delimiter(})>content(=)inline<delimiter({)predefined($_ENV)operator([)local_variable($key)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// This assumes that MAX_QUOTA is a named constant.) -reserved(foreach) operator(()local_variable($all_users) reserved(as) local_variable($user)operator(\)) operator({) - local_variable($disk_space) operator(=) ident(get_usage)operator(()local_variable($user)operator(\))operator(;) - reserved(if) operator(()local_variable($disk_space) operator(>) constant(MAX_QUOTA)operator(\)) operator({) - ident(complain)operator(()local_variable($user)operator(\))operator(;) - operator(}) -operator(}) - -comment(// You can't modify array's elements in-place.) -local_variable($array) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(\))operator(;) -local_variable($newarray) operator(=) predefined(array)operator(()operator(\))operator(;) -reserved(foreach) operator(()local_variable($array) reserved(as) local_variable($item)operator(\)) operator({) - local_variable($newarray)operator([)operator(]) operator(=) local_variable($item) operator(-) integer(1)operator(;) -operator(}) -predefined(print_r)operator(()local_variable($newarray)operator(\))operator(;) - -comment(// Before PHP 5, that is. You can precede the reference operator) -comment(// before $item to get reference instead of copy.) -local_variable($array) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(\))operator(;) -reserved(foreach) operator(()local_variable($array) reserved(as) operator(&)local_variable($item)operator(\)) operator({) - local_variable($item)operator(--)operator(;) -operator(}) -predefined(print_r)operator(()local_variable($array)operator(\))operator(;) - -comment(// TODO: explain the old each(\) and list(\) iteration construct.) -comment(// foreach is new in PHP 4, and there are subtle differences.) - -comment(// @@PLEAC@@_4.5) -comment(// Conventional 'read-only' access) -reserved(foreach)operator(()local_variable($array) reserved(as) local_variable($item)operator(\)) -operator({) - operator(;) comment(// Can access, but not update, array element referred to by '$item') -operator(}) - -comment(// ----) - -comment(// '&' makes '$item' a reference) -reserved(foreach)operator(()local_variable($array) reserved(as) operator(&)local_variable($item)operator(\)) -operator({) - operator(;) comment(// Update array element referred to by '$item') -operator(}) - -comment(// ------------) - -local_variable($arraylen) operator(=) predefined(count)operator(()local_variable($array)operator(\))operator(;) - -reserved(for)operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) local_variable($arraylen)operator(;) local_variable($i)operator(++)operator(\)) -operator({) - operator(;) comment(// '$array' is updateable via subscript notation) -operator(}) - -comment(// ----------------------------) - -local_variable($fruits) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')>operator(,) string<delimiter(')content(Raspberry)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($fruits) reserved(as) operator(&)local_variable($fruit)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($fruit)delimiter(})>content( tastes good in a pie.)char(\\n)delimiter(")>operator(;) -operator(}) - -local_variable($fruitlen) operator(=) predefined(count)operator(()local_variable($fruits)operator(\))operator(;) - -reserved(for)operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) local_variable($fruitlen)operator(;) local_variable($i)operator(++)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($fruits)operator([)local_variable($i)operator(])delimiter(})>content( tastes good in a pie.)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ----------------------------) - -local_variable($rogue_cats) operator(=) predefined(array)operator(()string<delimiter(')content(Blackie)delimiter(')>operator(,) string<delimiter(')content(Goldie)delimiter(')>operator(,) string<delimiter(')content(Silkie)delimiter(')>operator(\))operator(;) - -comment(// Take care to assign reference to '$rogue_cats' array via '=&') -local_variable($namelist)operator([)string<delimiter(')content(felines)delimiter(')>operator(]) operator(=&) local_variable($rogue_cats)operator(;) - -comment(// Take care to make '$cat' a reference via '&$' to allow updating) -reserved(foreach)operator(()local_variable($namelist)operator([)string<delimiter(')content(felines)delimiter(')>operator(]) reserved(as) operator(&)local_variable($cat)operator(\)) -operator({) - local_variable($cat) operator(.=) string<delimiter(')content( [meow])delimiter(')>operator(;) -operator(}) - -comment(// Via array reference) -reserved(foreach)operator(()local_variable($namelist)operator([)string<delimiter(')content(felines)delimiter(')>operator(]) reserved(as) local_variable($cat)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($cat)delimiter(})>content( purrs hypnotically.)char(\\n)delimiter(")>operator(;) -operator(}) - -predefined(echo) string<delimiter(")content(---)char(\\n)delimiter(")>operator(;) - -comment(// Original array) -reserved(foreach)operator(()local_variable($rogue_cats) reserved(as) local_variable($cat)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($cat)delimiter(})>content( purrs hypnotically.)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_4.6) -comment(// PHP offers the 'array_unique' function to perform this task. It works with both keyed,) -comment(// and numerically-indexed arrays; keys / indexes are preserved; use of 'array_values' ) -comment(// is recommended to reindex numerically-indexed arrays since there will likely be missing) -comment(// indexes) - -comment(// Remove duplicate values) -local_variable($unique) operator(=) predefined(array_unique)operator(()local_variable($array)operator(\))operator(;) - -comment(// Remove duplicates, and reindex [for numerically-indexed arrays only]) -local_variable($unique) operator(=) predefined(array_values)operator(()predefined(array_unique)operator(()local_variable($array)operator(\))operator(\))operator(;) - -comment(// or use:) -local_variable($unique) operator(=) predefined(array_keys)operator(()predefined(array_flip)operator(()local_variable($array)operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Selected Perl 'seen' examples) -reserved(foreach)operator(()local_variable($list) reserved(as) local_variable($item)operator(\)) -operator({) - reserved(if) operator(()operator(!)predefined(isset)operator(()local_variable($seen)operator([)local_variable($item)operator(])operator(\))operator(\)) - operator({) - local_variable($seen)operator([)local_variable($item)operator(]) operator(=) pre_constant(TRUE)operator(;) - local_variable($unique)operator([)operator(]) operator(=) local_variable($item)operator(;) - operator(}) -operator(}) - -comment(// ------------) - -reserved(foreach)operator(()local_variable($list) reserved(as) local_variable($item)operator(\)) -operator({) - local_variable($seen)operator([)local_variable($item)operator(]) operator(||) operator(()operator(++)local_variable($seen)operator([)local_variable($item)operator(]) operator(&&) operator(()local_variable($unique)operator([)operator(]) operator(=) local_variable($item)operator(\))operator(\))operator(;) -operator(}) - -comment(// ------------) - -reserved(function) function(some_func)operator(()local_variable($item)operator(\)) -operator({) - operator(;) comment(// Do something with '$item') -operator(}) - -reserved(foreach)operator(()local_variable($list) reserved(as) local_variable($item)operator(\)) -operator({) - local_variable($seen)operator([)local_variable($item)operator(]) operator(||) operator(()operator(++)local_variable($seen)operator([)local_variable($item)operator(]) operator(&&) ident(some_func)operator(()local_variable($item)operator(\))operator(\))operator(;) -operator(}) - -comment(// ----------------------------) - -reserved(foreach)operator(()predefined(array_slice)operator(()predefined(preg_split)operator(()string<delimiter(')content(/)content(\\n)content(/)delimiter(')>operator(,) string<delimiter(`)content(who)delimiter(`)>operator(\))operator(,) integer(0)operator(,) operator(-)integer(1)operator(\)) reserved(as) local_variable($user_entry)operator(\)) -operator({) - local_variable($user) operator(=) predefined(preg_split)operator(()string<delimiter(')content(/)content(\\s)content(/)delimiter(')>operator(,) local_variable($user_entry)operator(\))operator(;) - local_variable($ucnt)operator([)local_variable($user)operator([)integer(0)operator(])operator(])operator(++)operator(;) -operator(}) - -predefined(ksort)operator(()local_variable($ucnt)operator(\))operator(;) - -predefined(echo) string<delimiter(")content(users logged in:)char(\\n)delimiter(")>operator(;) - -reserved(foreach)operator(()local_variable($ucnt) reserved(as) local_variable($user) operator(=>) local_variable($cnt)operator(\)) -operator({) - predefined(echo) string<delimiter(")char(\\t)inline<delimiter({)local_variable($user)delimiter(})>content( => )inline<delimiter({)local_variable($cnt)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_4.7) -comment(// PHP offers the 'array_diff' and 'array_diff_assoc' functions to perform this task. Same) -comment(// points as made about 'array_unique' apply here also) - -local_variable($a) operator(=) predefined(array)operator(()string<delimiter(')content(c)delimiter(')>operator(,) string<delimiter(')content(a)delimiter(')>operator(,) string<delimiter(')content(b)delimiter(')>operator(,) string<delimiter(')content(d)delimiter(')>operator(\))operator(;) -local_variable($b) operator(=) predefined(array)operator(()string<delimiter(')content(c)delimiter(')>operator(,) string<delimiter(')content(a)delimiter(')>operator(,) string<delimiter(')content(b)delimiter(')>operator(,) string<delimiter(')content(e)delimiter(')>operator(\))operator(;) - -local_variable($diff) operator(=) predefined(array_diff)operator(()local_variable($a)operator(,) local_variable($b)operator(\))operator(;) comment(// $diff -> [3] 'd') -local_variable($diff) operator(=) predefined(array_diff)operator(()local_variable($b)operator(,) local_variable($a)operator(\))operator(;) comment(// $diff -> [3] 'e') - -comment(// Numerically-indexed array, reindexed) -local_variable($diff) operator(=) predefined(array_values)operator(()predefined(array_diff)operator(()local_variable($a)operator(,) local_variable($b)operator(\))operator(\))operator(;) comment(// $diff -> [0] 'd') -local_variable($diff) operator(=) predefined(array_values)operator(()predefined(array_diff)operator(()local_variable($b)operator(,) local_variable($a)operator(\))operator(\))operator(;) comment(// $diff -> [0] 'e') - -comment(// ----------------------------) - -comment(// 1st Perl 'seen' example only) - -local_variable($a) operator(=) predefined(array)operator(()string<delimiter(')content(k1)delimiter(')> operator(=>) integer(11)operator(,) string<delimiter(')content(k2)delimiter(')> operator(=>) integer(12)operator(,) string<delimiter(')content(k4)delimiter(')> operator(=>) integer(14)operator(\))operator(;) -local_variable($b) operator(=) predefined(array)operator(()string<delimiter(')content(k1)delimiter(')> operator(=>) integer(11)operator(,) string<delimiter(')content(k2)delimiter(')> operator(=>) integer(12)operator(,) string<delimiter(')content(k3)delimiter(')> operator(=>) integer(13)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($b) reserved(as) local_variable($item) operator(=>) local_variable($value)operator(\)) operator({) local_variable($seen)operator([)local_variable($item)operator(]) operator(=) integer(1)operator(;) operator(}) - -comment(// Stores key only e.g. $aonly[0] contains 'k4', same as Perl example) -reserved(foreach)operator(()local_variable($a) reserved(as) local_variable($item) operator(=>) local_variable($value)operator(\)) operator({) reserved(if) operator(()operator(!)local_variable($seen)operator([)local_variable($item)operator(])operator(\)) local_variable($aonly)operator([)operator(]) operator(=) local_variable($item)operator(;) operator(}) - -comment(// Stores key and value e.g. $aonly['k4'] contains 14, same entry as in $a) -reserved(foreach)operator(()local_variable($a) reserved(as) local_variable($item) operator(=>) local_variable($value)operator(\)) operator({) reserved(if) operator(()operator(!)local_variable($seen)operator([)local_variable($item)operator(])operator(\)) local_variable($aonly)operator([)local_variable($item)operator(]) operator(=) local_variable($value)operator(;) operator(}) - -comment(// ----------------------------) - -comment(// Conventional way: $hash = array('key1' => 1, 'key2' => 2\);) - -local_variable($hash)operator([)string<delimiter(')content(key1)delimiter(')>operator(]) operator(=) integer(1)operator(;) -local_variable($hash)operator([)string<delimiter(')content(key2)delimiter(')>operator(]) operator(=) integer(2)operator(;) - -local_variable($hash) operator(=) predefined(array_combine)operator(()predefined(array)operator(()string<delimiter(')content(key1)delimiter(')>operator(,) string<delimiter(')content(key2)delimiter(')>operator(\))operator(,) predefined(array)operator(()integer(1)operator(,) integer(2)operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($seen) operator(=) predefined(array_slice)operator(()local_variable($b)operator(,) integer(0)operator(\))operator(;) - -local_variable($seen) operator(=) predefined(array_combine)operator(()predefined(array_keys)operator(()local_variable($b)operator(\))operator(,) predefined(array_fill)operator(()integer(0)operator(,) predefined(count)operator(()local_variable($b)operator(\))operator(,) integer(1)operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_4.8) -comment(// PHP offers a number of array-based 'set operation' functions:) -comment(// * union: array_unique(array_merge(...\)\)) -comment(// * intersection: array_intersect and family) -comment(// * difference: array_diff and family) -comment(// which may be used for this type of task. Also, if returned arrays need to be) -comment(// reindexed, 'array_slice($array, 0\)', or 'array_values($array\)' are useful) - -local_variable($a) operator(=) predefined(array)operator(()integer(1)operator(,) integer(3)operator(,) integer(5)operator(,) integer(6)operator(,) integer(7)operator(,) integer(8)operator(\))operator(;) -local_variable($b) operator(=) predefined(array)operator(()integer(2)operator(,) integer(3)operator(,) integer(5)operator(,) integer(7)operator(,) integer(9)operator(\))operator(;) - -local_variable($union) operator(=) predefined(array_values)operator(()predefined(array_unique)operator(()predefined(array_merge)operator(()local_variable($a)operator(,) local_variable($b)operator(\))operator(\))operator(\))operator(;) comment(// 1, 2, 3, 5, 6, 7, 8, 9) -local_variable($isect) operator(=) predefined(array_values)operator(()predefined(array_intersect)operator(()local_variable($a)operator(,) local_variable($b)operator(\))operator(\))operator(;) comment(// 3, 5, 7) -local_variable($diff) operator(=) predefined(array_values)operator(()predefined(array_diff)operator(()local_variable($a)operator(,) local_variable($b)operator(\))operator(\))operator(;) comment(// 1, 8) - -comment(// @@PLEAC@@_4.9) -comment(// PHP offers the 'array_merge' function to perform this task. Duplicate values are retained,) -comment(// but if arrays are numerically-indexed, resulting array is reindexed) - -local_variable($arr1) operator(=) predefined(array)operator(()string<delimiter(')content(c)delimiter(')>operator(,) string<delimiter(')content(a)delimiter(')>operator(,) string<delimiter(')content(b)delimiter(')>operator(,) string<delimiter(')content(d)delimiter(')>operator(\))operator(;) -local_variable($arr2) operator(=) predefined(array)operator(()string<delimiter(')content(c)delimiter(')>operator(,) string<delimiter(')content(a)delimiter(')>operator(,) string<delimiter(')content(b)delimiter(')>operator(,) string<delimiter(')content(e)delimiter(')>operator(\))operator(;) - -local_variable($new) operator(=) predefined(array_merge)operator(()local_variable($arr1)operator(,) local_variable($arr2)operator(\))operator(;) comment(// $new -> 'c', 'a', 'b', 'd', 'c', 'a', 'b', 'd') - -comment(// ----------------------------) - -local_variable($members) operator(=) predefined(array)operator(()string<delimiter(')content(Time)delimiter(')>operator(,) string<delimiter(')content(Flies)delimiter(')>operator(\))operator(;) -local_variable($initiates) operator(=) predefined(array)operator(()string<delimiter(')content(An)delimiter(')>operator(,) string<delimiter(')content(Arrow)delimiter(')>operator(\))operator(;) - -local_variable($members) operator(=) predefined(array_merge)operator(()local_variable($members)operator(,) local_variable($initiates)operator(\))operator(;) - -comment(// ------------) - -local_variable($members) operator(=) predefined(array)operator(()string<delimiter(')content(Time)delimiter(')>operator(,) string<delimiter(')content(Flies)delimiter(')>operator(\))operator(;) -local_variable($initiates) operator(=) predefined(array)operator(()string<delimiter(')content(An)delimiter(')>operator(,) string<delimiter(')content(Arrow)delimiter(')>operator(\))operator(;) - -comment(// 'array_splice' is the PHP equivalent to Perl's 'splice') -predefined(array_splice)operator(()local_variable($members)operator(,) integer(2)operator(,) integer(0)operator(,) predefined(array_merge)operator(()predefined(array)operator(()string<delimiter(')content(Like)delimiter(')>operator(\))operator(,) local_variable($initiates)operator(\))operator(\))operator(;) -predefined(echo) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($members)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -predefined(array_splice)operator(()local_variable($members)operator(,) integer(0)operator(,) integer(1)operator(,) predefined(array)operator(()string<delimiter(')content(Fruit)delimiter(')>operator(\))operator(\))operator(;) -predefined(array_splice)operator(()local_variable($members)operator(,) operator(-)integer(2)operator(,) integer(2)operator(,) predefined(array)operator(()string<delimiter(')content(A)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')>operator(\))operator(\))operator(;) -predefined(echo) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($members)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_4.10) -local_variable($reversed) operator(=) predefined(array_reverse)operator(()local_variable($array)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(foreach)operator(()predefined(array_reverse)operator(()local_variable($array)operator(\)) reserved(as) local_variable($item)operator(\)) -operator({) - operator(;) comment(// ... do something with '$item' ...) -operator(}) - -comment(// ------------) - -reserved(for)operator(()local_variable($i) operator(=) predefined(count)operator(()local_variable($array)operator(\)) operator(-) integer(1)operator(;) local_variable($i) operator(>=) integer(0)operator(;) local_variable($i)operator(--)operator(\)) -operator({) - operator(;) comment(// ... do something with '$array[$i]' ...) -operator(}) - -comment(// ----------------------------) - -predefined(sort)operator(()local_variable($array)operator(\))operator(;) -local_variable($array) operator(=) predefined(array_reverse)operator(()local_variable($array)operator(\))operator(;) - -comment(// ------------) - -predefined(rsort)operator(()local_variable($array)operator(\))operator(;) - -comment(// @@PLEAC@@_4.11) -comment(// Array elements can be deleted using 'unset'; removing several elements would require applying) -comment(// 'unset' several times, probably in a loop. However, they would most likely also need to be) -comment(// reindexed, so a better approach would be to use 'array_slice' which avoids explicit looping.) -comment(// Where elements need to be removed, and those elements also returned, it is probably best to) -comment(// combine both operations in a function. This is the approach taken here in implementing both) -comment(// 'shiftN' and 'popN', and it is these functions that are used in the examples) - -reserved(function) function(popN)operator(()operator(&)local_variable($arr)operator(,) local_variable($n)operator(\)) -operator({) - local_variable($ret) operator(=) predefined(array_slice)operator(()local_variable($arr)operator(,) operator(-)operator(()local_variable($n)operator(\))operator(,) local_variable($n)operator(\))operator(;) - local_variable($arr) operator(=) predefined(array_slice)operator(()local_variable($arr)operator(,) integer(0)operator(,) predefined(count)operator(()local_variable($arr)operator(\)) operator(-) local_variable($n)operator(\))operator(;) - reserved(return) local_variable($ret)operator(;) -operator(}) - -reserved(function) function(shiftN)operator(()operator(&)local_variable($arr)operator(,) local_variable($n)operator(\)) -operator({) - local_variable($ret) operator(=) predefined(array_slice)operator(()local_variable($arr)operator(,) integer(0)operator(,) local_variable($n)operator(\))operator(;) - local_variable($arr) operator(=) predefined(array_slice)operator(()local_variable($arr)operator(,) local_variable($n)operator(\))operator(;) - reserved(return) local_variable($ret)operator(;) -operator(}) - -comment(// ------------) - -comment(// Remove $n elements from the front of $array; return them in $fron) -local_variable($front) operator(=) ident(shiftN)operator(()local_variable($array)operator(,) local_variable($n)operator(\))operator(;) - -comment(// Remove $n elements from the end of $array; return them in $end) -local_variable($end) operator(=) ident(popN)operator(()local_variable($array)operator(,) local_variable($n)operator(\))operator(;) - -comment(// ------------) - -local_variable($friends) operator(=) predefined(array)operator(()string<delimiter(')content(Peter)delimiter(')>operator(,) string<delimiter(')content(Paul)delimiter(')>operator(,) string<delimiter(')content(Mary)delimiter(')>operator(,) string<delimiter(')content(Jim)delimiter(')>operator(,) string<delimiter(')content(Tim)delimiter(')>operator(\))operator(;) - -predefined(list)operator(()local_variable($this_)operator(,) local_variable($that)operator(\)) operator(=) ident(shiftN)operator(()local_variable($friends)operator(,) integer(2)operator(\))operator(;) - -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($this_)delimiter(})>content( )inline<delimiter({)local_variable($that)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ------------) - -local_variable($beverages) operator(=) predefined(array)operator(()string<delimiter(')content(Dew)delimiter(')>operator(,) string<delimiter(')content(Jolt)delimiter(')>operator(,) string<delimiter(')content(Cola)delimiter(')>operator(,) string<delimiter(')content(Sprite)delimiter(')>operator(,) string<delimiter(')content(Fresca)delimiter(')>operator(\))operator(;) - -local_variable($pair) operator(=) ident(popN)operator(()local_variable($beverages)operator(,) integer(2)operator(\))operator(;) - -predefined(echo) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($pair)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_4.12) -comment(// This section illustrates various 'find first' techniques. The Perl examples all use an) -comment(// explicit loop and condition testing [also repeated here]. This is the simplest, and) -comment(// [potentially] most efficient approach because the search can be terminated as soon as a) -comment(// match is found. However, it is worth mentioning a few alternatives:) -comment(// * 'array_search' performs a 'find first' using the element value rather than a condition) -comment(// check, so isn't really applicable here) -comment(// * 'array_filter', whilst using a condition check, actually performs a 'find all', though) -comment(// all but the first returned element can be discarded. This approach is actually less error) -comment(// prone than using a loop, but the disadvantage is that each element is visited: there is no) -comment(// means of terminating the search once a match has been found. It would be nice if this) -comment(// function were to have a third parameter, a Boolean flag indicating whether to traverse) -comment(// the whole array, or quit after first match [next version, maybe :\) ?]) - -local_variable($found) operator(=) pre_constant(FALSE)operator(;) - -reserved(foreach)operator(()local_variable($array) reserved(as) local_variable($item)operator(\)) -operator({) - comment(// Not found - skip to next item) - reserved(if) operator(()operator(!)local_variable($criterion)operator(\)) reserved(continue)operator(;) - - comment(// Found - save and leave) - local_variable($match) operator(=) local_variable($item)operator(;) - local_variable($found) operator(=) pre_constant(TRUE)operator(;) - reserved(break)operator(;) -operator(}) - -reserved(if) operator(()local_variable($found)operator(\)) -operator({) - operator(;) comment(// do something with $match) -operator(}) -reserved(else) -operator({) - operator(;) comment(// not found) -operator(}) - -comment(// ------------) - -reserved(function) function(predicate)operator(()local_variable($element)operator(\)) -operator({) - reserved(if) operator(()ident(criterion)operator(\)) reserved(return) pre_constant(TRUE)operator(;) - reserved(return) pre_constant(FALSE)operator(;) -operator(}) - -local_variable($match) operator(=) predefined(array_slice)operator(()predefined(array_filter)operator(()local_variable($array)operator(,) string<delimiter(')content(predicate)delimiter(')>operator(\))operator(,) integer(0)operator(,) integer(1)operator(\))operator(;) - -reserved(if) operator(()local_variable($match)operator(\)) -operator({) - operator(;) comment(// do something with $match[0]) -operator(}) -reserved(else) -operator({) - operator(;) comment(// $match is empty - not found) -operator(}) - -comment(// ----------------------------) - -reserved(class) class(Employee) -operator({) - reserved(public) local_variable($name)operator(,) local_variable($age)operator(,) local_variable($ssn)operator(,) local_variable($salary)operator(;) - - reserved(public) reserved(function) function(__construct)operator(()local_variable($name)operator(,) local_variable($age)operator(,) local_variable($ssn)operator(,) local_variable($salary)operator(,) local_variable($category)operator(\)) - operator({) - local_variable($this)operator(->)ident(name) operator(=) local_variable($name)operator(;) - local_variable($this)operator(->)ident(age) operator(=) local_variable($age)operator(;) - local_variable($this)operator(->)ident(ssn) operator(=) local_variable($ssn)operator(;) - local_variable($this)operator(->)ident(salary) operator(=) local_variable($salary)operator(;) - local_variable($this)operator(->)ident(category) operator(=) local_variable($category)operator(;) - operator(}) -operator(}) - -comment(// ------------) - -local_variable($employees) operator(=) predefined(array)operator(() - reserved(new) constant(Employee)operator(()string<delimiter(')content(sdf)delimiter(')>operator(,) integer(27)operator(,) integer(12345)operator(,) integer(47000)operator(,) string<delimiter(')content(Engineer)delimiter(')>operator(\))operator(,) - reserved(new) constant(Employee)operator(()string<delimiter(')content(ajb)delimiter(')>operator(,) integer(32)operator(,) integer(12376)operator(,) integer(51000)operator(,) string<delimiter(')content(Programmer)delimiter(')>operator(\))operator(,) - reserved(new) constant(Employee)operator(()string<delimiter(')content(dgh)delimiter(')>operator(,) integer(31)operator(,) integer(12355)operator(,) integer(45000)operator(,) string<delimiter(')content(Engineer)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -reserved(function) function(array_update)operator(()local_variable($arr)operator(,) local_variable($lambda)operator(,) local_variable($updarr)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($arr) reserved(as) local_variable($key)operator(\)) local_variable($lambda)operator(()local_variable($updarr)operator(,) local_variable($key)operator(\))operator(;) - reserved(return) local_variable($updarr)operator(;) -operator(}) - -reserved(function) function(highest_salaried_engineer)operator(()operator(&)local_variable($arr)operator(,) local_variable($employee)operator(\)) -operator({) - reserved(static) local_variable($highest_salary) operator(=) integer(0)operator(;) - - reserved(if) operator(()local_variable($employee)operator(->)ident(category) operator(==) string<delimiter(')content(Engineer)delimiter(')>operator(\)) - operator({) - reserved(if) operator(()local_variable($employee)operator(->)ident(salary) operator(>) local_variable($highest_salary)operator(\)) - operator({) - local_variable($highest_salary) operator(=) local_variable($employee)operator(->)ident(salary)operator(;) - local_variable($arr)operator([)integer(0)operator(]) operator(=) local_variable($employee)operator(;) - operator(}) - operator(}) -operator(}) - -comment(// ------------) - -comment(// 'array_update' custom function is modelled on 'array_reduce' except that it allows the) -comment(// return of an array, contents and length of which are entirely dependant on what the) -comment(// callback function does. Here, it is logically working in a 'find first' capacity) -local_variable($highest_salaried_engineer) operator(=) ident(array_update)operator(()local_variable($employees)operator(,) string<delimiter(')content(highest_salaried_engineer)delimiter(')>operator(,) predefined(array)operator(()operator(\))operator(\))operator(;) - -predefined(echo) string<delimiter(')content(Highest paid engineer is: )delimiter(')> operator(.) local_variable($highest_salaried_engineer)operator([)integer(0)operator(])operator(->)ident(name) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_4.13) -comment(// PHP implements 'grep' functionality [as embodied in the current section] in the 'array_filter') -comment(// function) - -reserved(function) function(predicate)operator(()local_variable($element)operator(\)) -operator({) - reserved(if) operator(()ident(criterion)operator(\)) reserved(return) pre_constant(TRUE)operator(;) - reserved(return) pre_constant(FALSE)operator(;) -operator(}) - -local_variable($matching) operator(=) predefined(array_filter)operator(()local_variable($list)operator(,) string<delimiter(')content(predicate)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($bigs) operator(=) predefined(array_filter)operator(()local_variable($nums)operator(,) predefined(create_function)operator(()string<delimiter(')content($n)delimiter(')>operator(,) string<delimiter(')content(return $n > 1000000;)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -reserved(function) function(is_pig)operator(()local_variable($user)operator(\)) -operator({) - local_variable($user_details) operator(=) predefined(preg_split)operator(()string<delimiter(')content(/()content(\\s)content(\)+/)delimiter(')>operator(,) local_variable($user)operator(\))operator(;) - comment(// Assuming field 5 is the resource being compared) - reserved(return) local_variable($user_details)operator([)integer(5)operator(]) operator(>) float(1e7)operator(;) -operator(}) - -local_variable($pigs) operator(=) predefined(array_filter)operator(()predefined(array_slice)operator(()predefined(preg_split)operator(()string<delimiter(')content(/)content(\\n)content(/)delimiter(')>operator(,) string<delimiter(`)content(who -u)delimiter(`)>operator(\))operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(,) string<delimiter(')content(is_pig)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($matching) operator(=) predefined(array_filter)operator(()predefined(array_slice)operator(()predefined(preg_split)operator(()string<delimiter(')content(/)content(\\n)content(/)delimiter(')>operator(,) string<delimiter(`)content(who)delimiter(`)>operator(\))operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(,) - predefined(create_function)operator(()string<delimiter(')content($user)delimiter(')>operator(,) string<delimiter(')content(return preg_match()char(\\')content(/^gnat /)char(\\')content(, $user\);)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -reserved(class) class(Employee) -operator({) - reserved(public) local_variable($name)operator(,) local_variable($age)operator(,) local_variable($ssn)operator(,) local_variable($salary)operator(;) - - reserved(public) reserved(function) function(__construct)operator(()local_variable($name)operator(,) local_variable($age)operator(,) local_variable($ssn)operator(,) local_variable($salary)operator(,) local_variable($category)operator(\)) - operator({) - local_variable($this)operator(->)ident(name) operator(=) local_variable($name)operator(;) - local_variable($this)operator(->)ident(age) operator(=) local_variable($age)operator(;) - local_variable($this)operator(->)ident(ssn) operator(=) local_variable($ssn)operator(;) - local_variable($this)operator(->)ident(salary) operator(=) local_variable($salary)operator(;) - local_variable($this)operator(->)ident(category) operator(=) local_variable($category)operator(;) - operator(}) -operator(}) - -comment(// ------------) - -local_variable($employees) operator(=) predefined(array)operator(() - reserved(new) constant(Employee)operator(()string<delimiter(')content(sdf)delimiter(')>operator(,) integer(27)operator(,) integer(12345)operator(,) integer(47000)operator(,) string<delimiter(')content(Engineer)delimiter(')>operator(\))operator(,) - reserved(new) constant(Employee)operator(()string<delimiter(')content(ajb)delimiter(')>operator(,) integer(32)operator(,) integer(12376)operator(,) integer(51000)operator(,) string<delimiter(')content(Programmer)delimiter(')>operator(\))operator(,) - reserved(new) constant(Employee)operator(()string<delimiter(')content(dgh)delimiter(')>operator(,) integer(31)operator(,) integer(12355)operator(,) integer(45000)operator(,) string<delimiter(')content(Engineer)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($engineers) operator(=) predefined(array_filter)operator(()local_variable($employees)operator(,) - predefined(create_function)operator(()string<delimiter(')content($employee)delimiter(')>operator(,) string<delimiter(')content(return $employee->category == "Engineer";)delimiter(')>operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_4.14) -comment(// PHP offers a rich set of sorting functions. Key features:) -comment(// * Inplace sorts; the original array, not a a copy, is sorted) -comment(// * Separate functions exist for sorting [both ascending and descending order]:) -comment(// - By value, assign new keys / indices [sort, rsort]) -comment(// - By key [ksort, krsort] (for non-numerically indexed arrays\)) -comment(// - By value [asort, arsort]) -comment(// - As above, but using a user-defined comparator [i.e. callback function]) -comment(// [usort, uasort, uksort]) -comment(// - Natural order sort [natsort]) -comment(// * Significantly, if sorting digit-only elements, whether strings or numbers,) -comment(// 'natural order' [i.e. 1 before 10 before 100 (ascending\)] is retained. If) -comment(// the elements are alphanumeric e.g. 'z1', 'z10' then 'natsort' should be) -comment(// used [note: beware of 'natsort' with negative numbers; prefer 'sort' or 'asort']) - -local_variable($unsorted) operator(=) predefined(array)operator(()integer(7)operator(,) integer(12)operator(,) operator(-)integer(13)operator(,) integer(2)operator(,) integer(100)operator(,) integer(5)operator(,) integer(1)operator(,) operator(-)integer(2)operator(,) integer(23)operator(,) integer(3)operator(,) integer(6)operator(,) integer(4)operator(\))operator(;) - -predefined(sort)operator(()local_variable($unsorted)operator(\))operator(;) comment(// -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100) -predefined(rsort)operator(()local_variable($unsorted)operator(\))operator(;) comment(// 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13) - -predefined(asort)operator(()local_variable($unsorted)operator(\))operator(;) comment(// -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100) -predefined(arsort)operator(()local_variable($unsorted)operator(\))operator(;) comment(// 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13) - -predefined(natsort)operator(()local_variable($unsorted)operator(\))operator(;) comment(// -2, -13, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100) - -comment(// ------------) - -reserved(function) function(ascend)operator(()local_variable($left)operator(,) local_variable($right)operator(\)) operator({) reserved(return) local_variable($left) operator(>) local_variable($right)operator(;) operator(}) -reserved(function) function(descend)operator(()local_variable($left)operator(,) local_variable($right)operator(\)) operator({) reserved(return) local_variable($left) operator(<) local_variable($right)operator(;) operator(}) - -comment(// ------------) - -predefined(usort)operator(()local_variable($unsorted)operator(,) string<delimiter(')content(ascend)delimiter(')>operator(\))operator(;) comment(// -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100) -predefined(usort)operator(()local_variable($unsorted)operator(,) string<delimiter(')content(descend)delimiter(')>operator(\))operator(;) comment(// 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13) - -predefined(uasort)operator(()local_variable($unsorted)operator(,) string<delimiter(')content(ascend)delimiter(')>operator(\))operator(;) comment(// -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100) -predefined(uasort)operator(()local_variable($unsorted)operator(,) string<delimiter(')content(descend)delimiter(')>operator(\))operator(;) comment(// 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13) - -comment(// ----------------------------) - -reserved(function) function(kill_process)operator(()local_variable($pid)operator(\)) -operator({) - comment(// Is 'killable' ?) - reserved(if) operator(()operator(!)predefined(posix_kill)operator(()local_variable($pid)operator(,) integer(0)operator(\))operator(\)) reserved(return)operator(;) - - comment(// Ok, so kill in two stages) - predefined(posix_kill)operator(()local_variable($pid)operator(,) integer(15)operator(\))operator(;) comment(// SIGTERM) - predefined(sleep)operator(()integer(1)operator(\))operator(;) - predefined(posix_kill)operator(()local_variable($pid)operator(,) integer(9)operator(\))operator(;) comment(// SIGKILL) -operator(}) - -reserved(function) function(pid)operator(()local_variable($pentry)operator(\)) -operator({) - local_variable($p) operator(=) predefined(preg_split)operator(()string<delimiter(')content(/)content(\\s)content(/)delimiter(')>operator(,) predefined(trim)operator(()local_variable($pentry)operator(\))operator(\))operator(;) - reserved(return) local_variable($p)operator([)integer(0)operator(])operator(;) -operator(}) - -local_variable($processes) operator(=) predefined(array_map)operator(()string<delimiter(')content(pid)delimiter(')>operator(,) predefined(array_slice)operator(()predefined(preg_split)operator(()string<delimiter(')content(/)content(\\n)content(/)delimiter(')>operator(,) string<delimiter(`)content(ps ax)delimiter(`)>operator(\))operator(,) integer(1)operator(,) operator(-)integer(1)operator(\))operator(\))operator(;) -predefined(sort)operator(()local_variable($processes)operator(\))operator(;) - -predefined(echo) predefined(join)operator(()string<delimiter(')content( ,)delimiter(')>operator(,) local_variable($processes)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -predefined(echo) string<delimiter(')content(Enter a pid to kill: )delimiter(')>operator(;) -reserved(if) operator(()operator(()local_variable($pid) operator(=) predefined(trim)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(\))operator(\))operator(\)) - ident(kill_process)operator(()local_variable($pid)operator(\))operator(;) - -comment(// @@PLEAC@@_4.15) -comment(// Tasks in this section would typically use the PHP 'usort' family of functions) -comment(// which are used with a comparator function so as to perform custom comparisions.) -comment(// A significant difference from the Perl examples is that these functions are) -comment(// inplace sorters, so it is the original array that is modified. Where this must) -comment(// be prevented a copy of the array can be made and sorted) - -reserved(function) function(comparator)operator(()local_variable($left)operator(,) local_variable($right)operator(\)) -operator({) - operator(;) comment(// Compare '$left' with '$right' returning result) -operator(}) - -comment(// ------------) - -local_variable($ordered) operator(=) predefined(array_slice)operator(()local_variable($unordered)operator(\))operator(;) -predefined(usort)operator(()local_variable($ordered)operator(,) string<delimiter(')content(comparator)delimiter(')>operator(\))operator(;) - -comment(// ----------------------------) - -comment(// The Perl example looks like it is creating a hash using computed values as the key,) -comment(// array values as the value, sorting on the computed key, then extracting the sorted) -comment(// values and placing them back into an array) - -reserved(function) function(compute)operator(()local_variable($value)operator(\)) -operator({) - operator(;) comment(// Return computed value utilising '$value') -operator(}) - -comment(// ------------) - -comment(// Original numerically-indexed array [sample data used]) -local_variable($unordered) operator(=) predefined(array)operator(()integer(5)operator(,) integer(3)operator(,) integer(7)operator(,) integer(1)operator(,) integer(4)operator(,) integer(2)operator(,) integer(6)operator(\))operator(;) - -comment(// Create hash using 'compute' function to generate the keys. This example assumes that) -comment(// each value in the '$unordered' array is used in generating the corresponding '$key') -reserved(foreach)operator(()local_variable($unordered) reserved(as) local_variable($value)operator(\)) -operator({) - local_variable($precomputed)operator([)ident(compute)operator(()local_variable($value)operator(\))operator(]) operator(=) local_variable($value)operator(;) -operator(}) - -comment(// Copy the hash, and sort it by key) -local_variable($ordered_precomputed) operator(=) predefined(array_slice)operator(()local_variable($precomputed)operator(,) integer(0)operator(\))operator(;) predefined(ksort)operator(()local_variable($ordered_precomputed)operator(\))operator(;) - -comment(// Extract the values of the hash in current order placing them in a new numerically-indexed) -comment(// array) -local_variable($ordered) operator(=) predefined(array_values)operator(()local_variable($ordered_precomputed)operator(\))operator(;) - -comment(// ----------------------------) - -comment(// As above, except uses 'array_update' and 'accum' to help create hash) - -reserved(function) function(array_update)operator(()local_variable($arr)operator(,) local_variable($lambda)operator(,) local_variable($updarr)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($arr) reserved(as) local_variable($key)operator(\)) local_variable($lambda)operator(()local_variable($updarr)operator(,) local_variable($key)operator(\))operator(;) - reserved(return) local_variable($updarr)operator(;) -operator(}) - -reserved(function) function(accum)operator(()operator(&)local_variable($arr)operator(,) local_variable($value)operator(\)) -operator({) - local_variable($arr)operator([)ident(compute)operator(()local_variable($value)operator(\))operator(]) operator(=) local_variable($value)operator(;) -operator(}) - -comment(// ------------) - -reserved(function) function(compute)operator(()local_variable($value)operator(\)) -operator({) - operator(;) comment(// Return computed value utilising '$value') -operator(}) - -comment(// ------------) - -comment(// Original numerically-indexed array [sample data used]) -local_variable($unordered) operator(=) predefined(array)operator(()integer(5)operator(,) integer(3)operator(,) integer(7)operator(,) integer(1)operator(,) integer(4)operator(,) integer(2)operator(,) integer(6)operator(\))operator(;) - -comment(// Create hash) -local_variable($precomputed) operator(=) ident(array_update)operator(()local_variable($unordered)operator(,) string<delimiter(')content(accum)delimiter(')>operator(,) predefined(array)operator(()operator(\))operator(\))operator(;) - -comment(// Copy the hash, and sort it by key) -local_variable($ordered_precomputed) operator(=) predefined(array_slice)operator(()local_variable($precomputed)operator(,) integer(0)operator(\))operator(;) predefined(ksort)operator(()local_variable($ordered_precomputed)operator(\))operator(;) - -comment(// Extract the values of the hash in current order placing them in a new numerically-indexed) -comment(// array) -local_variable($ordered) operator(=) predefined(array_values)operator(()local_variable($ordered_precomputed)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(class) class(Employee) -operator({) - reserved(public) local_variable($name)operator(,) local_variable($age)operator(,) local_variable($ssn)operator(,) local_variable($salary)operator(;) - - reserved(public) reserved(function) function(__construct)operator(()local_variable($name)operator(,) local_variable($age)operator(,) local_variable($ssn)operator(,) local_variable($salary)operator(\)) - operator({) - local_variable($this)operator(->)ident(name) operator(=) local_variable($name)operator(;) - local_variable($this)operator(->)ident(age) operator(=) local_variable($age)operator(;) - local_variable($this)operator(->)ident(ssn) operator(=) local_variable($ssn)operator(;) - local_variable($this)operator(->)ident(salary) operator(=) local_variable($salary)operator(;) - operator(}) -operator(}) - -comment(// ------------) - -local_variable($employees) operator(=) predefined(array)operator(() - reserved(new) constant(Employee)operator(()string<delimiter(')content(sdf)delimiter(')>operator(,) integer(27)operator(,) integer(12345)operator(,) integer(47000)operator(\))operator(,) - reserved(new) constant(Employee)operator(()string<delimiter(')content(ajb)delimiter(')>operator(,) integer(32)operator(,) integer(12376)operator(,) integer(51000)operator(\))operator(,) - reserved(new) constant(Employee)operator(()string<delimiter(')content(dgh)delimiter(')>operator(,) integer(31)operator(,) integer(12355)operator(,) integer(45000)operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($ordered) operator(=) predefined(array_slice)operator(()local_variable($employees)operator(,) integer(0)operator(\))operator(;) -predefined(usort)operator(()local_variable($ordered)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return $left->name > $right->name;)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($sorted_employees) operator(=) predefined(array_slice)operator(()local_variable($employees)operator(,) integer(0)operator(\))operator(;) -predefined(usort)operator(()local_variable($sorted_employees)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return $left->name > $right->name;)delimiter(')>operator(\))operator(\))operator(;) - -local_variable($bonus) operator(=) predefined(array)operator(()integer(12376) operator(=>) integer(5000)operator(,) integer(12345) operator(=>) integer(6000)operator(,) integer(12355) operator(=>) integer(0)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($sorted_employees) reserved(as) local_variable($employee)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($employee)operator(->)ident(name)delimiter(})>content( earns )char(\\$)inline<delimiter({)local_variable($employee)operator(->)ident(salary)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -reserved(foreach)operator(()local_variable($sorted_employees) reserved(as) local_variable($employee)operator(\)) -operator({) - reserved(if) operator(()operator(()local_variable($amount) operator(=) local_variable($bonus)operator([)local_variable($employee)operator(->)ident(ssn)operator(])operator(\))operator(\)) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($employee)operator(->)ident(name)delimiter(})>content( got a bonus of: )char(\\$)inline<delimiter({)local_variable($amount)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -local_variable($sorted) operator(=) predefined(array_slice)operator(()local_variable($employees)operator(,) integer(0)operator(\))operator(;) -predefined(usort)operator(()local_variable($sorted)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return $left->name > $right->name || $left->age != $right->age;)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -comment(// PHP offers a swag of POSIX functions for obtaining user information [i.e. they all read) -comment(// the '/etc/passwd' file for the relevant infroamtion], and it is these that should rightly) -comment(// be used for this purpose. However, since the intent of this section is to illustrate array) -comment(// manipulation, these functions won't be used. Instead a custom function mimicing Perl's) -comment(// 'getpwent' function will be implemented so the code presented here can more faithfully match) -comment(// the Perl code) - -reserved(function) function(get_pw_entries)operator(()operator(\)) -operator({) - reserved(function) function(normal_users_only)operator(()local_variable($e)operator(\)) - operator({) - local_variable($entry) operator(=) predefined(split)operator(()string<delimiter(')content(:)delimiter(')>operator(,) local_variable($e)operator(\))operator(;) reserved(return) local_variable($entry)operator([)integer(2)operator(]) operator(>) integer(100) operator(&&) local_variable($entry)operator([)integer(2)operator(]) operator(<) integer(32768)operator(;) - operator(}) - - reserved(foreach)operator(()predefined(array_filter)operator(()predefined(file)operator(()string<delimiter(')content(/etc/passwd)delimiter(')>operator(\))operator(,) string<delimiter(')content(normal_users_only)delimiter(')>operator(\)) reserved(as) local_variable($entry)operator(\)) - local_variable($users)operator([)operator(]) operator(=) predefined(split)operator(()string<delimiter(')content(:)delimiter(')>operator(,) predefined(trim)operator(()local_variable($entry)operator(\))operator(\))operator(;) - - reserved(return) local_variable($users)operator(;) -operator(}) - -comment(// ------------) - -local_variable($users) operator(=) ident(get_pw_entries)operator(()operator(\))operator(;) - -predefined(usort)operator(()local_variable($users)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return $left[0] > $right[0];)delimiter(')>operator(\))operator(\))operator(;) -reserved(foreach)operator(()local_variable($users) reserved(as) local_variable($user)operator(\)) predefined(echo) string<delimiter(")inline<delimiter({)local_variable($user)operator([)integer(0)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -local_variable($names) operator(=) predefined(array)operator(()string<delimiter(')content(sdf)delimiter(')>operator(,) string<delimiter(')content(ajb)delimiter(')>operator(,) string<delimiter(')content(dgh)delimiter(')>operator(\))operator(;) - -local_variable($sorted) operator(=) predefined(array_slice)operator(()local_variable($names)operator(,) integer(0)operator(\))operator(;) -predefined(usort)operator(()local_variable($sorted)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return substr($left, 1, 1\) > substr($right, 1, 1\);)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($strings) operator(=) predefined(array)operator(()string<delimiter(')content(bbb)delimiter(')>operator(,) string<delimiter(')content(aa)delimiter(')>operator(,) string<delimiter(')content(c)delimiter(')>operator(\))operator(;) - -local_variable($sorted) operator(=) predefined(array_slice)operator(()local_variable($strings)operator(,) integer(0)operator(\))operator(;) -predefined(usort)operator(()local_variable($sorted)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return strlen($left\) > strlen($right\);)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(array_update)operator(()local_variable($arr)operator(,) local_variable($lambda)operator(,) local_variable($updarr)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($arr) reserved(as) local_variable($key)operator(\)) local_variable($lambda)operator(()local_variable($updarr)operator(,) local_variable($key)operator(\))operator(;) - reserved(return) local_variable($updarr)operator(;) -operator(}) - -reserved(function) function(accum)operator(()operator(&)local_variable($arr)operator(,) local_variable($value)operator(\)) -operator({) - local_variable($arr)operator([)predefined(strlen)operator(()local_variable($value)operator(\))operator(]) operator(=) local_variable($value)operator(;) -operator(}) - -comment(// ----) - -local_variable($strings) operator(=) predefined(array)operator(()string<delimiter(')content(bbb)delimiter(')>operator(,) string<delimiter(')content(aa)delimiter(')>operator(,) string<delimiter(')content(c)delimiter(')>operator(\))operator(;) - -local_variable($temp) operator(=) ident(array_update)operator(()local_variable($strings)operator(,) string<delimiter(')content(accum)delimiter(')>operator(,) predefined(array)operator(()operator(\))operator(\))operator(;) -predefined(ksort)operator(()local_variable($temp)operator(\))operator(;) -local_variable($sorted) operator(=) predefined(array_values)operator(()local_variable($temp)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(array_update)operator(()local_variable($arr)operator(,) local_variable($lambda)operator(,) local_variable($updarr)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($arr) reserved(as) local_variable($key)operator(\)) local_variable($lambda)operator(()local_variable($updarr)operator(,) local_variable($key)operator(\))operator(;) - reserved(return) local_variable($updarr)operator(;) -operator(}) - -reserved(function) function(accum)operator(()operator(&)local_variable($arr)operator(,) local_variable($value)operator(\)) -operator({) - reserved(if) operator(()predefined(preg_match)operator(()string<delimiter(')content(/()content(\\d)content(+\)/)delimiter(')>operator(,) local_variable($value)operator(,) local_variable($matches)operator(\))operator(\)) - local_variable($arr)operator([)local_variable($matches)operator([)integer(1)operator(])operator(]) operator(=) local_variable($value)operator(;) -operator(}) - -comment(// ----) - -local_variable($fields) operator(=) predefined(array)operator(()string<delimiter(')content(b1b2b)delimiter(')>operator(,) string<delimiter(')content(a4a)delimiter(')>operator(,) string<delimiter(')content(c9)delimiter(')>operator(,) string<delimiter(')content(ddd)delimiter(')>operator(,) string<delimiter(')content(a)delimiter(')>operator(\))operator(;) - -local_variable($temp) operator(=) ident(array_update)operator(()local_variable($fields)operator(,) string<delimiter(')content(accum)delimiter(')>operator(,) predefined(array)operator(()operator(\))operator(\))operator(;) -predefined(ksort)operator(()local_variable($temp)operator(\))operator(;) -local_variable($sorted_fields) operator(=) predefined(array_values)operator(()local_variable($temp)operator(\))operator(;) - -comment(// @@PLEAC@@_4.16) -predefined(array_unshift)operator(()local_variable($a1)operator(,) predefined(array_pop)operator(()local_variable($a1)operator(\))operator(\))operator(;) comment(// last -> first) -predefined(array_push)operator(()local_variable($a1)operator(,) predefined(array_shift)operator(()local_variable($a1)operator(\))operator(\))operator(;) comment(// first -> last) - -comment(// ----------------------------) - -reserved(function) function(grab_and_rotate)operator(()operator(&)local_variable($arr)operator(\)) -operator({) - local_variable($item) operator(=) local_variable($arr)operator([)integer(0)operator(])operator(;) - predefined(array_push)operator(()local_variable($arr)operator(,) predefined(array_shift)operator(()local_variable($arr)operator(\))operator(\))operator(;) - reserved(return) local_variable($item)operator(;) -operator(}) - -comment(// ------------) - -local_variable($processes) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(\))operator(;) - -reserved(while) operator(()pre_constant(TRUE)operator(\)) -operator({) - local_variable($process) operator(=) ident(grab_and_rotate)operator(()local_variable($processes)operator(\))operator(;) - predefined(echo) string<delimiter(")content(Handling process )inline<delimiter({)local_variable($process)delimiter(})>char(\\n)delimiter(")>operator(;) - predefined(sleep)operator(()integer(1)operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_4.17) -comment(// PHP offers the 'shuffle' function to perform this task) - -local_variable($arr) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(,) integer(6)operator(,) integer(7)operator(,) integer(8)operator(,) integer(9)operator(\))operator(;) - -predefined(shuffle)operator(()local_variable($arr)operator(\))operator(;) - -predefined(echo) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($arr)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -comment(// Perl example equivalents) -reserved(function) function(fisher_yates_shuffle)operator(()operator(&)local_variable($a)operator(\)) -operator({) - local_variable($size) operator(=) predefined(count)operator(()local_variable($a)operator(\)) operator(-) integer(1)operator(;) - - reserved(for)operator(()local_variable($i) operator(=) local_variable($size)operator(;) local_variable($i) operator(>=) integer(0)operator(;) local_variable($i)operator(--)operator(\)) - operator({) - reserved(if) operator(()operator(()local_variable($j) operator(=) predefined(rand)operator(()integer(0)operator(,) local_variable($i)operator(\))operator(\)) operator(!=) local_variable($i)operator(\)) - predefined(list)operator(()local_variable($a)operator([)local_variable($i)operator(])operator(,) local_variable($a)operator([)local_variable($j)operator(])operator(\)) operator(=) predefined(array)operator(()local_variable($a)operator([)local_variable($j)operator(])operator(,) local_variable($a)operator([)local_variable($i)operator(])operator(\))operator(;) - operator(}) -operator(}) - -reserved(function) function(naive_shuffle)operator(()operator(&)local_variable($a)operator(\)) -operator({) - local_variable($size) operator(=) predefined(count)operator(()local_variable($a)operator(\))operator(;) - - reserved(for)operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) local_variable($size)operator(;) local_variable($i)operator(++)operator(\)) - operator({) - local_variable($j) operator(=) predefined(rand)operator(()integer(0)operator(,) local_variable($size) operator(-) integer(1)operator(\))operator(;) - predefined(list)operator(()local_variable($a)operator([)local_variable($i)operator(])operator(,) local_variable($a)operator([)local_variable($j)operator(])operator(\)) operator(=) predefined(array)operator(()local_variable($a)operator([)local_variable($j)operator(])operator(,) local_variable($a)operator([)local_variable($i)operator(])operator(\))operator(;) - operator(}) -operator(}) - -comment(// ------------) - -local_variable($arr) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(,) integer(6)operator(,) integer(7)operator(,) integer(8)operator(,) integer(9)operator(\))operator(;) - -ident(fisher_yates_shuffle)operator(()local_variable($arr)operator(\))operator(;) -predefined(echo) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($arr)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -ident(naive_shuffle)operator(()local_variable($arr)operator(\))operator(;) -predefined(echo) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($arr)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_4.18) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_4.19) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_5.0) -comment(// PHP uses the term 'array' to refer to associative arrays - referred to in Perl) -comment(// as 'hashes' - and for the sake of avoiding confusion, the Perl terminology will) -comment(// be used. As a matter of interest, PHP does not sport a vector, matrix, or list) -comment(// type: the 'array' [Perl 'hash'] serves all these roles) - -local_variable($age) operator(=) predefined(array)operator(()string<delimiter(')content(Nat)delimiter(')> operator(=>) integer(24)operator(,) string<delimiter(')content(Jules)delimiter(')> operator(=>) integer(25)operator(,) string<delimiter(')content(Josh)delimiter(')> operator(=>) integer(17)operator(\))operator(;) - -local_variable($age)operator([)string<delimiter(')content(Nat)delimiter(')>operator(]) operator(=) integer(24)operator(;) -local_variable($age)operator([)string<delimiter(')content(Jules)delimiter(')>operator(]) operator(=) integer(25)operator(;) -local_variable($age)operator([)string<delimiter(')content(Josh)delimiter(')>operator(]) operator(=) integer(17)operator(;) - -local_variable($age) operator(=) predefined(array_combine)operator(()predefined(array)operator(()string<delimiter(')content(Nat)delimiter(')>operator(,) string<delimiter(')content(Jules)delimiter(')>operator(,) string<delimiter(')content(Josh)delimiter(')>operator(\))operator(,) predefined(array)operator(()integer(24)operator(,) integer(25)operator(,) integer(17)operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -local_variable($food_colour)operator([)string<delimiter(')content(Apple)delimiter(')>operator(]) operator(=) string<delimiter(')content(red)delimiter(')>operator(;) local_variable($food_colour)operator([)string<delimiter(')content(Banana)delimiter(')>operator(]) operator(=) string<delimiter(')content(yellow)delimiter(')>operator(;) -local_variable($food_colour)operator([)string<delimiter(')content(Lemon)delimiter(')>operator(]) operator(=) string<delimiter(')content(yellow)delimiter(')>operator(;) local_variable($food_colour)operator([)string<delimiter(')content(Carrot)delimiter(')>operator(]) operator(=) string<delimiter(')content(orange)delimiter(')>operator(;) - -local_variable($food_colour) operator(=) predefined(array_combine)operator(()predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')>operator(,) string<delimiter(')content(Lemon)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')>operator(\))operator(,) - predefined(array)operator(()string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(orange)delimiter(')>operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_5.1) -local_variable($hash)operator([)local_variable($key)operator(]) operator(=) local_variable($value)operator(;) - -comment(// ------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -local_variable($food_colour)operator([)string<delimiter(')content(Raspberry)delimiter(')>operator(]) operator(=) string<delimiter(')content(pink)delimiter(')>operator(;) - -predefined(echo) string<delimiter(")content(Known foods:)char(\\n)delimiter(")>operator(;) -reserved(foreach)operator(()local_variable($food_colour) reserved(as) local_variable($food) operator(=>) local_variable($colour)operator(\)) predefined(echo) string<delimiter(")inline<delimiter({)local_variable($food)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_5.2) -comment(// Returns TRUE on all existing entries with non-NULL values) -reserved(if) operator(()predefined(isset)operator(()local_variable($hash)operator([)local_variable($key)operator(])operator(\))operator(\)) - operator(;) comment(// entry exists ) -reserved(else) - operator(;) comment(// no such entry ) - -comment(// ------------) - -comment(// Returns TRUE on all existing entries regardless of attached value) -reserved(if) operator(()predefined(array_key_exists)operator(()local_variable($key)operator(,) local_variable($hash)operator(\))operator(\)) - operator(;) comment(// entry exists ) -reserved(else) - operator(;) comment(// no such entry ) - -comment(// ----------------------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()predefined(array)operator(()string<delimiter(')content(Banana)delimiter(')>operator(,) string<delimiter(')content(Martini)delimiter(')>operator(\)) reserved(as) local_variable($name)operator(\)) -operator({) - reserved(if) operator(()predefined(isset)operator(()local_variable($food_colour)operator([)local_variable($name)operator(])operator(\))operator(\)) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($name)delimiter(})>content( is a food.)char(\\n)delimiter(")>operator(;) - reserved(else) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($name)delimiter(})>content( is a drink.)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ----------------------------) - -local_variable($age) operator(=) predefined(array)operator(()string<delimiter(')content(Toddler)delimiter(')> operator(=>) integer(3)operator(,) string<delimiter(')content(Unborn)delimiter(')> operator(=>) integer(0)operator(,) string<delimiter(')content(Phantasm)delimiter(')> operator(=>) pre_constant(NULL)operator(\))operator(;) - -reserved(foreach)operator(()predefined(array)operator(()string<delimiter(')content(Toddler)delimiter(')>operator(,) string<delimiter(')content(Unborn)delimiter(')>operator(,) string<delimiter(')content(Phantasm)delimiter(')>operator(,) string<delimiter(')content(Relic)delimiter(')>operator(\)) reserved(as) local_variable($thing)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($thing)delimiter(})>content(:)delimiter(")>operator(;) - reserved(if) operator(()predefined(array_key_exists)operator(()local_variable($thing)operator(,) local_variable($age)operator(\))operator(\)) predefined(echo) string<delimiter(')content( exists)delimiter(')>operator(;) - reserved(if) operator(()predefined(isset)operator(()local_variable($age)operator([)local_variable($thing)operator(])operator(\))operator(\)) predefined(echo) string<delimiter(')content( non-NULL)delimiter(')>operator(;) - reserved(if) operator(()local_variable($age)operator([)local_variable($thing)operator(])operator(\)) predefined(echo) string<delimiter(')content( TRUE)delimiter(')>operator(;) - predefined(echo) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_5.3) -comment(// Remove one, or more, hash entries) -predefined(unset)operator(()local_variable($hash)operator([)local_variable($key)operator(])operator(\))operator(;) - -predefined(unset)operator(()local_variable($hash)operator([)local_variable($key1)operator(])operator(,) local_variable($hash)operator([)local_variable($key2)operator(])operator(,) local_variable($hash)operator([)local_variable($key3)operator(])operator(\))operator(;) - -comment(// Remove entire hash) -predefined(unset)operator(()local_variable($hash)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(print_foods)operator(()operator(\)) -operator({) - comment(// Perl example uses a global variable) - reserved(global) local_variable($food_colour)operator(;) - - local_variable($foods) operator(=) predefined(array_keys)operator(()local_variable($food_colour)operator(\))operator(;) - - predefined(echo) string<delimiter(')content(Foods:)delimiter(')>operator(;) - reserved(foreach)operator(()local_variable($foods) reserved(as) local_variable($food)operator(\)) predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($food)delimiter(})>delimiter(")>operator(;) - - predefined(echo) string<delimiter(")char(\\n)content(Values:)char(\\n)delimiter(")>operator(;) - reserved(foreach)operator(()local_variable($foods) reserved(as) local_variable($food)operator(\)) - operator({) - local_variable($colour) operator(=) local_variable($food_colour)operator([)local_variable($food)operator(])operator(;) - - reserved(if) operator(()predefined(isset)operator(()local_variable($colour)operator(\))operator(\)) - predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($colour)delimiter(})>char(\\n)delimiter(")>operator(;) - reserved(else) - predefined(echo) string<delimiter(")content( nullified or removed)char(\\n)delimiter(")>operator(;) - operator(}) -operator(}) - -comment(// ------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -predefined(echo) string<delimiter(")content(Initially:)char(\\n)delimiter(")>operator(;) ident(print_foods)operator(()operator(\))operator(;) - -comment(// Nullify an entry) -local_variable($food_colour)operator([)string<delimiter(')content(Banana)delimiter(')>operator(]) operator(=) pre_constant(NULL)operator(;) -predefined(echo) string<delimiter(")char(\\n)content(With 'Banana' nullified)char(\\n)delimiter(")>operator(;) -ident(print_foods)operator(()operator(\))operator(;) - -comment(// Remove an entry) -predefined(unset)operator(()local_variable($food_colour)operator([)string<delimiter(')content(Banana)delimiter(')>operator(])operator(\))operator(;) -predefined(echo) string<delimiter(")char(\\n)content(With 'Banana' removed)char(\\n)delimiter(")>operator(;) -ident(print_foods)operator(()operator(\))operator(;) - -comment(// Destroy the hash) -predefined(unset)operator(()local_variable($food_colour)operator(\))operator(;) - -comment(// @@PLEAC@@_5.4) -comment(// Access keys and values) -reserved(foreach)operator(()local_variable($hash) reserved(as) local_variable($key) operator(=>) local_variable($value)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// Access keys only) -reserved(foreach)operator(()predefined(array_keys)operator(()local_variable($hash)operator(\)) reserved(as) local_variable($key)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// Access values only) -reserved(foreach)operator(()local_variable($hash) reserved(as) local_variable($value)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// ----------------------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($food_colour) reserved(as) local_variable($food) operator(=>) local_variable($colour)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($food)delimiter(})>content( is )inline<delimiter({)local_variable($colour)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -reserved(foreach)operator(()predefined(array_keys)operator(()local_variable($food_colour)operator(\)) reserved(as) local_variable($food)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($food)delimiter(})>content( is )inline<delimiter({)local_variable($food_colour)operator([)local_variable($food)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ----------------------------) - -comment(// 'countfrom' - count number of messages from each sender) - -local_variable($line) operator(=) predefined(fgets)operator(()constant(STDIN)operator(\))operator(;) - -reserved(while) operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) -operator({) - reserved(if) operator(()predefined(preg_match)operator(()string<delimiter(')content(/^From: (.*\)/)delimiter(')>operator(,) local_variable($line)operator(,) local_variable($matches)operator(\))operator(\)) - operator({) - reserved(if) operator(()predefined(isset)operator(()local_variable($from)operator([)local_variable($matches)operator([)integer(1)operator(])operator(])operator(\))operator(\)) - local_variable($from)operator([)local_variable($matches)operator([)integer(1)operator(])operator(]) operator(+=) integer(1)operator(;) - reserved(else) - local_variable($from)operator([)local_variable($matches)operator([)integer(1)operator(])operator(]) operator(=) integer(1)operator(;) - operator(}) - - local_variable($line) operator(=) predefined(fgets)operator(()constant(STDIN)operator(\))operator(;) -operator(}) - -reserved(if) operator(()predefined(isset)operator(()local_variable($from)operator(\))operator(\)) -operator({) - predefined(echo) string<delimiter(")content(Senders:)char(\\n)delimiter(")>operator(;) - reserved(foreach)operator(()local_variable($from) reserved(as) local_variable($sender) operator(=>) local_variable($count)operator(\)) predefined(echo) string<delimiter(")inline<delimiter({)local_variable($sender)delimiter(})>content( : )inline<delimiter({)local_variable($count)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) -reserved(else) -operator({) - predefined(echo) string<delimiter(")content(No valid data entered)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_5.5) -comment(// PHP offers, 'print_r', which prints hash contents in 'debug' form; it also) -comment(// works recursively, printing any contained arrays in similar form) -comment(// Array) -comment(// () -comment(// [key1] => value1 ) -comment(// [key2] => value2) -comment(// ...) -comment(// \)) - -predefined(print_r)operator(()local_variable($hash)operator(\))operator(;) - -comment(// ------------) - -comment(// Based on Perl example; non-recursive, so contained arrays not printed correctly) -reserved(foreach)operator(()local_variable($hash) reserved(as) local_variable($key) operator(=>) local_variable($value)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($key)delimiter(})>content( => )local_variable($value)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ----------------------------) - -comment(// Sorted by keys) - -comment(// 1. Sort the original hash) -predefined(ksort)operator(()local_variable($hash)operator(\))operator(;) - -comment(// 2. Extract keys, sort, traverse original in key order) -local_variable($keys) operator(=) predefined(array_keys)operator(()local_variable($hash)operator(\))operator(;) predefined(sort)operator(()local_variable($keys)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($keys) reserved(as) local_variable($key)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($key)delimiter(})>content( => )inline<delimiter({)local_variable($hash)operator([)local_variable($key)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// Sorted by values) - -comment(// 1. Sort the original hash) -predefined(asort)operator(()local_variable($hash)operator(\))operator(;) - -comment(// 2. Extract values, sort, traverse original in value order [warning: finds ) -comment(// only first matching key in the case where duplicate values exist]) -local_variable($values) operator(=) predefined(array_values)operator(()local_variable($hash)operator(\))operator(;) predefined(sort)operator(()local_variable($values)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($values) reserved(as) local_variable($value)operator(\)) -operator({) - predefined(echo) local_variable($value) operator(.) string<delimiter(')content( <= )delimiter(')> operator(.) predefined(array_search)operator(()local_variable($value)operator(,) local_variable($hash)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_5.6) -comment(// Unless sorted, hash elements remain in the order of insertion. If care is taken to) -comment(// always add a new element to the end of the hash, then element order is the order) -comment(// of insertion. The following function, 'array_push_associative' [modified from original) -comment(// found at 'array_push' section of PHP documentation], does just that) -reserved(function) function(array_push_associative)operator(()operator(&)local_variable($arr)operator(\)) -operator({) - reserved(foreach) operator(()predefined(func_get_args)operator(()operator(\)) reserved(as) local_variable($arg)operator(\)) - operator({) - reserved(if) operator(()predefined(is_array)operator(()local_variable($arg)operator(\))operator(\)) - reserved(foreach) operator(()local_variable($arg) reserved(as) local_variable($key) operator(=>) local_variable($value)operator(\)) operator({) local_variable($arr)operator([)local_variable($key)operator(]) operator(=) local_variable($value)operator(;) local_variable($ret)operator(++)operator(;) operator(}) - reserved(else) - local_variable($arr)operator([)local_variable($arg)operator(]) operator(=) string<delimiter(')delimiter(')>operator(;) - operator(}) - - reserved(return) local_variable($ret)operator(;) -operator(}) - -comment(// ------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()operator(\))operator(;) - -comment(// Individual calls, or ...) -ident(array_push_associative)operator(()local_variable($food_colour)operator(,) predefined(array)operator(()string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(Yellow)delimiter(')>operator(\))operator(\))operator(;) -ident(array_push_associative)operator(()local_variable($food_colour)operator(,) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(Green)delimiter(')>operator(\))operator(\))operator(;) -ident(array_push_associative)operator(()local_variable($food_colour)operator(,) predefined(array)operator(()string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(Yellow)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ... one call, one array; physical order retained) -comment(// array_push_associative($food_colour, array('Banana' => 'Yellow', 'Apple' => 'Green', 'Lemon' => 'Yellow'\)\);) - -predefined(print_r)operator(()local_variable($food_colour)operator(\))operator(;) - -predefined(echo) string<delimiter(")char(\\n)content(In insertion order:)char(\\n)delimiter(")>operator(;) -reserved(foreach)operator(()local_variable($food_colour) reserved(as) local_variable($food) operator(=>) local_variable($colour)operator(\)) predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($food)delimiter(})>content( => )inline<delimiter({)local_variable($colour)delimiter(})>char(\\n)delimiter(")>operator(;) - -local_variable($foods) operator(=) predefined(array_keys)operator(()local_variable($food_colour)operator(\))operator(;) - -predefined(echo) string<delimiter(")char(\\n)content(Still in insertion order:)char(\\n)delimiter(")>operator(;) -reserved(foreach)operator(()local_variable($foods) reserved(as) local_variable($food)operator(\)) predefined(echo) string<delimiter(")content( )inline<delimiter({)local_variable($food)delimiter(})>content( => )inline<delimiter({)local_variable($food_colour)operator([)local_variable($food)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_5.7) -reserved(foreach)operator(()predefined(array_slice)operator(()predefined(preg_split)operator(()string<delimiter(')content(/)content(\\n)content(/)delimiter(')>operator(,) string<delimiter(`)content(who)delimiter(`)>operator(\))operator(,) integer(0)operator(,) operator(-)integer(1)operator(\)) reserved(as) local_variable($entry)operator(\)) -operator({) - predefined(list)operator(()local_variable($user)operator(,) local_variable($tty)operator(\)) operator(=) predefined(preg_split)operator(()string<delimiter(')content(/)content(\\s)content(/)delimiter(')>operator(,) local_variable($entry)operator(\))operator(;) - local_variable($ttys)operator([)local_variable($user)operator(])operator([)operator(]) operator(=) local_variable($tty)operator(;) - - comment(// Could instead do this:) - comment(// $user = array_slice(preg_split('/\\s/', $entry\), 0, 2\);) - comment(// $ttys[$user[0]][] = $user[1];) -operator(}) - -predefined(ksort)operator(()local_variable($ttys)operator(\))operator(;) - -comment(// ------------) - -reserved(foreach)operator(()local_variable($ttys) reserved(as) local_variable($user) operator(=>) local_variable($all_ttys)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($user)delimiter(})>content(: )delimiter(")> operator(.) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($all_ttys)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -reserved(foreach)operator(()local_variable($ttys) reserved(as) local_variable($user) operator(=>) local_variable($all_ttys)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($user)delimiter(})>content(: )delimiter(")> operator(.) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($all_ttys)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - - reserved(foreach)operator(()local_variable($all_ttys) reserved(as) local_variable($tty)operator(\)) - operator({) - local_variable($stat) operator(=) predefined(stat)operator(()string<delimiter(')content(/dev/$tty)delimiter(')>operator(\))operator(;) - local_variable($pwent) operator(=) predefined(posix_getpwuid)operator(()local_variable($stat)operator([)string<delimiter(')content(uid)delimiter(')>operator(])operator(\))operator(;) - local_variable($user) operator(=) predefined(isset)operator(()local_variable($pwent)operator([)string<delimiter(')content(name)delimiter(')>operator(])operator(\)) operator(?) local_variable($pwent)operator([)string<delimiter(')content(name)delimiter(')>operator(]) operator(:) string<delimiter(')content(Not available)delimiter(')>operator(;) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($tty)delimiter(})>content( owned by: )inline<delimiter({)local_variable($user)delimiter(})>char(\\n)delimiter(")>operator(;) - operator(}) -operator(}) - -comment(// @@PLEAC@@_5.8) -comment(// PHP offers the 'array_flip' function to perform the task of exchanging the keys / values) -comment(// of a hash i.e. invert or 'flip' a hash) - -local_variable($reverse) operator(=) predefined(array_flip)operator(()local_variable($hash)operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($surname) operator(=) predefined(array)operator(()string<delimiter(')content(Babe)delimiter(')> operator(=>) string<delimiter(')content(Ruth)delimiter(')>operator(,) string<delimiter(')content(Mickey)delimiter(')> operator(=>) string<delimiter(')content(Mantle)delimiter(')>operator(\))operator(;) -local_variable($first_name) operator(=) predefined(array_flip)operator(()local_variable($surname)operator(\))operator(;) - -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($first_name)operator([)string<delimiter(')content(Mantle)delimiter(')>operator(])delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -predefined($argc) operator(==) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: )inline<delimiter({)predefined($argv)operator([)integer(0)operator(])delimiter(})>content( food|colour)char(\\n)delimiter(")>operator(\))operator(;) - -local_variable($given) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) - -local_variable($colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -local_variable($food) operator(=) predefined(array_flip)operator(()local_variable($colour)operator(\))operator(;) - -reserved(if) operator(()predefined(isset)operator(()local_variable($colour)operator([)local_variable($given)operator(])operator(\))operator(\)) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($given)delimiter(})>content( is a food with colour: )inline<delimiter({)local_variable($colour)operator([)local_variable($given)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) - -reserved(if) operator(()predefined(isset)operator(()local_variable($food)operator([)local_variable($given)operator(])operator(\))operator(\)) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($food)operator([)local_variable($given)operator(])delimiter(})>content( is a food with colour: )inline<delimiter({)local_variable($given)delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// ----------------------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($food_colour) reserved(as) local_variable($food) operator(=>) local_variable($colour)operator(\)) -operator({) - local_variable($foods_with_colour)operator([)local_variable($colour)operator(])operator([)operator(]) operator(=) local_variable($food)operator(;) -operator(}) - -local_variable($colour) operator(=) string<delimiter(')content(yellow)delimiter(')>operator(;) -predefined(echo) string<delimiter(")content(foods with colour )inline<delimiter({)local_variable($colour)delimiter(})>content( were: )delimiter(")> operator(.) predefined(join)operator(()string<delimiter(')content( )delimiter(')>operator(,) local_variable($foods_with_colour)operator([)local_variable($colour)operator(])operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_5.9) -comment(// PHP implements a swag of sorting functions, most designed to work with numerically-indexed) -comment(// arrays. For sorting hashes, the 'key' sorting functions are required:) -comment(// * 'ksort', 'krsort', 'uksort') - -comment(// Ascending order) -predefined(ksort)operator(()local_variable($hash)operator(\))operator(;) - -comment(// Descending order [i.e. reverse sort]) -predefined(krsort)operator(()local_variable($hash)operator(\))operator(;) - -comment(// Comparator-based sort) - -reserved(function) function(comparator)operator(()local_variable($left)operator(,) local_variable($right)operator(\)) -operator({) - comment(// Compare left key with right key) - reserved(return) local_variable($left) operator(>) local_variable($right)operator(;) -operator(}) - -predefined(uksort)operator(()local_variable($hash)operator(,) string<delimiter(')content(comparator)delimiter(')>operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -predefined(ksort)operator(()local_variable($food_colour)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($food_colour) reserved(as) local_variable($food) operator(=>) local_variable($colour)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($food)delimiter(})>content( is )inline<delimiter({)local_variable($colour)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -predefined(uksort)operator(()local_variable($food_colour)operator(,) predefined(create_function)operator(()string<delimiter(')content($left, $right)delimiter(')>operator(,) string<delimiter(')content(return $left > $right;)delimiter(')>operator(\))operator(\))operator(;) - -reserved(foreach)operator(()local_variable($food_colour) reserved(as) local_variable($food) operator(=>) local_variable($colour)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($food)delimiter(})>content( is )inline<delimiter({)local_variable($colour)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_5.10) -comment(// PHP offers the 'array_merge' function for this task [a related function, 'array_combine',) -comment(// may be used to create a hash from an array of keys, and one of values, respectively]) - -comment(// Merge two, or more, arrays) -local_variable($merged) operator(=) predefined(array_merge)operator(()local_variable($a)operator(,) local_variable($b)operator(,) local_variable($c)operator(\))operator(;) - -comment(// Create a hash from array of keys, and of values, respectively) -local_variable($hash) operator(=) predefined(array_combine)operator(()local_variable($keys)operator(,) local_variable($values)operator(\))operator(;) - -comment(// ------------) - -comment(// Can always merge arrays manually ) -reserved(foreach)operator(()predefined(array)operator(()local_variable($h1)operator(,) local_variable($h2)operator(,) local_variable($h3)operator(\)) reserved(as) local_variable($hash)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($hash) reserved(as) local_variable($key) operator(=>) local_variable($value)operator(\)) - operator({) - comment(// If same-key values differ, only latest retained) - local_variable($merged)operator([)local_variable($key)operator(]) operator(=) local_variable($value)operator(;) - - comment(// Do this to append values for that key) - comment(// $merged[$key][] = $value;) - operator(}) -operator(}) - -comment(// ----------------------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -local_variable($drink_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Galliano)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Mai Tai)delimiter(')> operator(=>) string<delimiter(')content(blue)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($ingested_colour) operator(=) predefined(array_merge)operator(()local_variable($food_colour)operator(,) local_variable($drink_colour)operator(\))operator(;) - -comment(// ------------) - -local_variable($substance_colour) operator(=) predefined(array)operator(()operator(\))operator(;) - -reserved(foreach)operator(()predefined(array)operator(()local_variable($food_colour)operator(,) local_variable($drink_colour)operator(\)) reserved(as) local_variable($hash)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($hash) reserved(as) local_variable($substance) operator(=>) local_variable($colour)operator(\)) - operator({) - reserved(if) operator(()predefined(array_key_exists)operator(()local_variable($substance)operator(,) local_variable($substance_colour)operator(\))operator(\)) - operator({) - predefined(echo) string<delimiter(")content(Warning )inline<delimiter({)local_variable($substance_colour)operator([)local_variable($substance)operator(])delimiter(})>content( seen twice. Using first definition.)char(\\n)delimiter(")>operator(;) - reserved(continue)operator(;) - operator(}) - local_variable($substance_colour)operator([)local_variable($substance)operator(]) operator(=) local_variable($colour)operator(;) - operator(}) -operator(}) - -comment(// @@PLEAC@@_5.11) -comment(// PHP offers a number of array-based 'set operation' functions:) -comment(// * union: array_merge) -comment(// * intersection: array_intersect and family) -comment(// * difference: array_diff and family) -comment(// which may be used for this type of task) - -comment(// Keys occurring in both hashes) -local_variable($common) operator(=) predefined(array_intersect_key)operator(()local_variable($h1)operator(,) local_variable($h2)operator(\))operator(;) - -comment(// Keys occurring in the first hash [left side], but not in the second hash) -local_variable($this_not_that) operator(=) predefined(array_diff_key)operator(()local_variable($h1)operator(,) local_variable($h2)operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($food_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) - string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -local_variable($citrus_colour) operator(=) predefined(array)operator(()string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Orange)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(,) string<delimiter(')content(Lime)delimiter(')> operator(=>) string<delimiter(')content(green)delimiter(')>operator(\))operator(;) - -local_variable($non_citrus) operator(=) predefined(array_diff_key)operator(()local_variable($food_colour)operator(,) local_variable($citrus_colour)operator(\))operator(;) - -comment(// @@PLEAC@@_5.12) -comment(// PHP implements a special type known as a 'resource' that encompasses things like file handles,) -comment(// sockets, database connections, and many others. The 'resource' type is, essentially, a) -comment(// reference variable that is not readily serialisable. That is to say:) -comment(// * A 'resource' may be converted to a string representation via the 'var_export' function) -comment(// * That same string cannot be converted back into a 'resource') -comment(// So, in terms of array handling, 'resource' types may be stored as array reference values,) -comment(// but cannot be used as keys. ) -comment(//) -comment(// I suspect it is this type of problem that the Perl::Tie package helps resolve. However, since) -comment(// PHP doesn't, AFAIK, sport a similar facility, the examples in this section cannot be) -comment(// implemented using file handles as keys) - -local_variable($filenames) operator(=) predefined(array)operator(()string<delimiter(')content(/etc/termcap)delimiter(')>operator(,) string<delimiter(')content(/vmlinux)delimiter(')>operator(,) string<delimiter(')content(/bin/cat)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($filenames) reserved(as) local_variable($filename)operator(\)) -operator({) - reserved(if) operator(()operator(!)operator(()local_variable($fh) operator(=) predefined(fopen)operator(()local_variable($filename)operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(\))operator(\)) reserved(continue)operator(;) - - comment(// Cannot do this as required by the Perl code:) - comment(// $name[$fh] = $filename;) - - comment(// Ok) - local_variable($name)operator([)local_variable($filename)operator(]) operator(=) local_variable($fh)operator(;) -operator(}) - -comment(// Would traverse array via:) -comment(//) -comment(// foreach(array_keys($name\) as $fh\)) -comment(// ...) -comment(// or) -comment(//) -comment(// foreach($name as $fh => $filename\)) -comment(// ...) -comment(// but since '$fh' cannot be a key, either of these will work:) -comment(//) -comment(// foreach($name as $filename => $fh\)) -comment(// or) -reserved(foreach)operator(()predefined(array_values)operator(()local_variable($name)operator(\)) reserved(as) local_variable($fh)operator(\)) -operator({) - predefined(fclose)operator(()local_variable($fh)operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_5.13) -comment(// PHP hashes are dynamic expanding and contracting as entries are added, and removed,) -comment(// respectively. Thus, there is no need to presize a hash, nor is there, AFAIK, any) -comment(// means of doing so except by the number of datums used when defining the hash) - -comment(// zero elements) -local_variable($hash) operator(=) predefined(array)operator(()operator(\))operator(;) - -comment(// ------------) - -comment(// three elements) -local_variable($hash) operator(=) predefined(array)operator(()string<delimiter(')content(Apple)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(Lemon)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(Carrot)delimiter(')> operator(=>) string<delimiter(')content(orange)delimiter(')>operator(\))operator(;) - -comment(// @@PLEAC@@_5.14) -reserved(foreach)operator(()local_variable($array) reserved(as) local_variable($element)operator(\)) local_variable($count)operator([)local_variable($element)operator(]) operator(+=) integer(1)operator(;) - -comment(// @@PLEAC@@_5.15) -local_variable($father) operator(=) predefined(array)operator(()string<delimiter(')content(Cain)delimiter(')> operator(=>) string<delimiter(')content(Adam)delimiter(')>operator(,) string<delimiter(')content(Abel)delimiter(')> operator(=>) string<delimiter(')content(Adam)delimiter(')>operator(,) string<delimiter(')content(Seth)delimiter(')> operator(=>) string<delimiter(')content(Adam)delimiter(')>operator(,) string<delimiter(')content(Enoch)delimiter(')> operator(=>) string<delimiter(')content(Cain)delimiter(')>operator(,) - string<delimiter(')content(Irad)delimiter(')> operator(=>) string<delimiter(')content(Enoch)delimiter(')>operator(,) string<delimiter(')content(Mehujael)delimiter(')> operator(=>) string<delimiter(')content(Irad)delimiter(')>operator(,) string<delimiter(')content(Methusael)delimiter(')>operator(=>) string<delimiter(')content(Mehujael)delimiter(')>operator(,) - string<delimiter(')content(Lamech)delimiter(')> operator(=>) string<delimiter(')content(Methusael)delimiter(')>operator(,) string<delimiter(')content(Jabal)delimiter(')> operator(=>) string<delimiter(')content(Lamech)delimiter(')>operator(,) string<delimiter(')content(Jubal)delimiter(')> operator(=>) string<delimiter(')content(Lamech)delimiter(')>operator(,) - string<delimiter(')content(Tubalcain)delimiter(')> operator(=>) string<delimiter(')content(Lamech)delimiter(')>operator(,) string<delimiter(')content(Enos)delimiter(')> operator(=>) string<delimiter(')content(Seth)delimiter(')>operator(\))operator(;) - -comment(// ------------) - -local_variable($name) operator(=) predefined(trim)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(\))operator(;) - -reserved(while) operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) -operator({) - reserved(while) operator(()pre_constant(TRUE)operator(\)) - operator({) - predefined(echo) string<delimiter(")local_variable($name)char(\\n)delimiter(")>operator(;) - - comment(// Can use either:) - reserved(if) operator(()operator(!)predefined(isset)operator(()local_variable($father)operator([)local_variable($name)operator(])operator(\))operator(\)) reserved(break)operator(;) - local_variable($name) operator(=) local_variable($father)operator([)local_variable($name)operator(])operator(;) - - comment(// or:) - comment(// if (!key_exists($name, $father\)\) break;) - comment(// $name = $father[$name];) - - comment(// or combine the two lines:) - comment(// if (!($name = $father[$name]\)\) break;) - operator(}) - - predefined(echo) string<delimiter(")char(\\n)delimiter(")>operator(;) - local_variable($name) operator(=) predefined(trim)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(\))operator(;) -operator(}) - -comment(// ----------------------------) - -predefined(define)operator(()constant(SEP)operator(,) string<delimiter(')content( )delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($father) reserved(as) local_variable($child) operator(=>) local_variable($parent)operator(\)) -operator({) - reserved(if) operator(()operator(!)local_variable($children)operator([)local_variable($parent)operator(])operator(\)) - local_variable($children)operator([)local_variable($parent)operator(]) operator(=) local_variable($child)operator(;) - reserved(else) - local_variable($children)operator([)local_variable($parent)operator(]) operator(.=) constant(SEP) operator(.) local_variable($child)operator(;) -operator(}) - -local_variable($name) operator(=) predefined(trim)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(\))operator(;) - -reserved(while) operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) -operator({) - predefined(echo) local_variable($name) operator(.) string<delimiter(')content( begat )delimiter(')>operator(;) - - reserved(if) operator(()operator(!)local_variable($children)operator([)local_variable($name)operator(])operator(\)) - predefined(echo) string<delimiter(")content(Nothing)char(\\n)delimiter(")> - reserved(else) - predefined(echo) predefined(str_replace)operator(()constant(SEP)operator(,) string<delimiter(')content(, )delimiter(')>operator(,) local_variable($children)operator([)local_variable($name)operator(])operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - - local_variable($name) operator(=) predefined(trim)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(\))operator(;) -operator(}) - -comment(// ----------------------------) - -predefined(define)operator(()constant(SEP)operator(,) string<delimiter(')content( )delimiter(')>operator(\))operator(;) - -local_variable($files) operator(=) predefined(array)operator(()string<delimiter(')content(/tmp/a)delimiter(')>operator(,) string<delimiter(')content(/tmp/b)delimiter(')>operator(,) string<delimiter(')content(/tmp/c)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($files) reserved(as) local_variable($file)operator(\)) -operator({) - reserved(if) operator(()operator(!)predefined(is_file)operator(()local_variable($file)operator(\))operator(\)) operator({) predefined(echo) string<delimiter(")content(Skipping )inline<delimiter({)local_variable($file)delimiter(})>char(\\n)delimiter(")>operator(;) reserved(continue)operator(;) operator(}) - reserved(if) operator(()operator(!)operator(()local_variable($fh) operator(=) predefined(fopen)operator(()local_variable($file)operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(\))operator(\)) operator({) predefined(echo) string<delimiter(")content(Skipping )inline<delimiter({)local_variable($file)delimiter(})>char(\\n)delimiter(")>operator(;) reserved(continue)operator(;) operator(}) - - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($fh)operator(\))operator(;) - - reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($fh)operator(\))operator(\)) - operator({) - reserved(if) operator(()predefined(preg_match)operator(()string<delimiter(')content(/^)content(\\s)content(*#)content(\\s)content(*include)content(\\s)content(*<([^>]+\)>/)delimiter(')>operator(,) local_variable($line)operator(,) local_variable($matches)operator(\))operator(\)) - operator({) - reserved(if) operator(()predefined(isset)operator(()local_variable($includes)operator([)local_variable($matches)operator([)integer(1)operator(])operator(])operator(\))operator(\)) - local_variable($includes)operator([)local_variable($matches)operator([)integer(1)operator(])operator(]) operator(.=) constant(SEP) operator(.) local_variable($file)operator(;) - reserved(else) - local_variable($includes)operator([)local_variable($matches)operator([)integer(1)operator(])operator(]) operator(=) local_variable($file)operator(;) - operator(}) - - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($fh)operator(\))operator(;) - operator(}) - - predefined(fclose)operator(()local_variable($fh)operator(\))operator(;) -operator(}) - -predefined(print_r)operator(()local_variable($includes)operator(\))operator(;) - -comment(// @@PLEAC@@_5.16) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_9.0) -local_variable($entry) operator(=) predefined(stat)operator(()string<delimiter(')content(/bin/vi)delimiter(')>operator(\))operator(;) -local_variable($entry) operator(=) predefined(stat)operator(()string<delimiter(')content(/usr/bin)delimiter(')>operator(\))operator(;) -local_variable($entry) operator(=) predefined(stat)operator(()predefined($argv)operator([)integer(1)operator(])operator(\))operator(;) - -comment(// ------------) - -local_variable($entry) operator(=) predefined(stat)operator(()string<delimiter(')content(/bin/vi)delimiter(')>operator(\))operator(;) - -local_variable($ctime) operator(=) local_variable($entry)operator([)string<delimiter(')content(ctime)delimiter(')>operator(])operator(;) -local_variable($size) operator(=) local_variable($entry)operator([)string<delimiter(')content(size)delimiter(')>operator(])operator(;) - -comment(// ----------------------------) - -comment(// For the simple task of determining whether a file contains, text', a simple) -comment(// function that searches for a newline could be implemented. Not exactly) -comment(// foolproof, but very simple, low overhead, no installation headaches ...) -reserved(function) function(containsText)operator(()local_variable($file)operator(\)) -operator({) - local_variable($status) operator(=) pre_constant(FALSE)operator(;) - - reserved(if) operator(()operator(()local_variable($fp) operator(=) predefined(fopen)operator(()local_variable($file)operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(\))operator(\)) - operator({) - reserved(while) operator(()pre_constant(FALSE) operator(!==) operator(()local_variable($char) operator(=) predefined(fgetc)operator(()local_variable($fp)operator(\))operator(\))operator(\)) - operator({) - reserved(if) operator(()local_variable($char) operator(==) string<delimiter(")char(\\n)delimiter(")>operator(\)) operator({) local_variable($status) operator(=) pre_constant(TRUE)operator(;) reserved(break)operator(;) operator(}) - operator(}) - - predefined(fclose)operator(()local_variable($fp)operator(\))operator(;) - operator(}) - - reserved(return) local_variable($status)operator(;) -operator(}) - -comment(// PHP offers the [currently experimental] Fileinfo group of functions to) -comment(// determine file types based on their contents / 'magic numbers'. This) -comment(// is functionality similar to the *NIX, 'file' utility. Note that it must) -comment(// first be installed using the PEAR utility [see PHP documentation] ) -reserved(function) function(isTextFile)operator(()local_variable($file)operator(\)) -operator({) - comment(// Note: untested code, but I believe this is how it is supposed to work) - local_variable($finfo) operator(=) ident(finfo_open)operator(()constant(FILEINFO_NONE)operator(\))operator(;) - local_variable($status) operator(=) operator(()ident(finfo_file)operator(()local_variable($finfo)operator(,) local_variable($file)operator(\)) operator(==) string<delimiter(')content(ASCII text)delimiter(')>operator(\))operator(;) - ident(finfo_close)operator(()local_variable($finfo)operator(\))operator(;) - reserved(return) local_variable($status)operator(;) -operator(}) - -comment(// Alternatively, use the *NIX utility, 'file', directly) -reserved(function) function(isTextFile)operator(()local_variable($file)operator(\)) -operator({) - reserved(return) predefined(exec)operator(()predefined(trim)operator(()string<delimiter(')content(file -bN )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($file)operator(\))operator(\))operator(\)) operator(==) string<delimiter(')content(ASCII text)delimiter(')>operator(;) -operator(}) - -comment(// ----) - -ident(containsText)operator(()predefined($argv)operator([)integer(1)operator(])operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(File )inline<delimiter({)predefined($argv)operator([)integer(1)operator(])delimiter(})>content( doesn't have any text in it)char(\\n)delimiter(")>operator(\))operator(;) - -ident(isTextFile)operator(()predefined($argv)operator([)integer(1)operator(])operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(File )inline<delimiter({)predefined($argv)operator([)integer(1)operator(])delimiter(})>content( doesn't have any text in it)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($dirname) operator(=) string<delimiter(')content(/usr/bin/)delimiter(')>operator(;) - -operator(()local_variable($dirhdl) operator(=) predefined(opendir)operator(()local_variable($dirname)operator(\))operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Couldn't open )inline<delimiter({)local_variable($dirname)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -reserved(while) operator(()operator(()local_variable($file) operator(=) predefined(readdir)operator(()local_variable($dirhdl)operator(\))operator(\)) operator(!==) pre_constant(FALSE)operator(\)) -operator({) - predefined(printf)operator(()string<delimiter(")content(Inside %s is something called: %s)char(\\n)delimiter(")>operator(,) local_variable($dirname)operator(,) local_variable($file)operator(\))operator(;) -operator(}) - -predefined(closedir)operator(()local_variable($dirhdl)operator(\))operator(;) - -comment(// @@PLEAC@@_9.1) -local_variable($filename) operator(=) string<delimiter(')content(example.txt)delimiter(')>operator(;) - -comment(// Get the file's current access and modification time, respectively) -local_variable($fs) operator(=) predefined(stat)operator(()local_variable($filename)operator(\))operator(;) - -local_variable($readtime) operator(=) local_variable($fs)operator([)string<delimiter(')content(atime)delimiter(')>operator(])operator(;) -local_variable($writetime) operator(=) local_variable($fs)operator([)string<delimiter(')content(mtime)delimiter(')>operator(])operator(;) - -comment(// Alter $writetime, and $readtime ...) - -comment(// Update file timestamp) -predefined(touch)operator(()local_variable($filename)operator(,) local_variable($writetime)operator(,) local_variable($readtime)operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($filename) operator(=) string<delimiter(')content(example.txt)delimiter(')>operator(;) - -comment(// Get the file's current access and modification time, respectively) -local_variable($fs) operator(=) predefined(stat)operator(()local_variable($filename)operator(\))operator(;) - -local_variable($atime) operator(=) local_variable($fs)operator([)string<delimiter(')content(atime)delimiter(')>operator(])operator(;) -local_variable($mtime) operator(=) local_variable($fs)operator([)string<delimiter(')content(mtime)delimiter(')>operator(])operator(;) - -comment(// Dedicated functions also exist to retrieve this information:) -comment(//) -comment(// $atime = $fileatime($filename\);) -comment(// $mtime = $filemtime($filename\);) -comment(//) - -comment(// Perform date arithmetic. Traditional approach where arithmetic is performed) -comment(// directly with Epoch Seconds [i.e. the *NIX time stamp value] will work ...) - -predefined(define)operator(()string<delimiter(')content(SECONDS_PER_DAY)delimiter(')>operator(,) integer(60) operator(*) integer(60) operator(*) integer(24)operator(\))operator(;) - -comment(// Set file's access and modification times to 1 week ago) -local_variable($atime) operator(-=) integer(7) operator(*) constant(SECONDS_PER_DAY)operator(;) -local_variable($mtime) operator(-=) integer(7) operator(*) constant(SECONDS_PER_DAY)operator(;) - -comment(// ... but care must be taken to account for daylight saving. Therefore, the) -comment(// recommended approach is to use library functions to perform such tasks:) - -local_variable($atime) operator(=) predefined(strtotime)operator(()string<delimiter(')content(-7 days)delimiter(')>operator(,) local_variable($atime)operator(\))operator(;) -local_variable($mtime) operator(=) predefined(strtotime)operator(()string<delimiter(')content(-7 days)delimiter(')>operator(,) local_variable($mtime)operator(\))operator(;) - -comment(// Update file timestamp) -predefined(touch)operator(()local_variable($filename)operator(,) local_variable($mtime)operator(,) local_variable($atime)operator(\))operator(;) - -comment(// Good idea to clear the cache after such updates have occurred so fresh) -comment(// values will be retrieved on next access) -predefined(clearstatcache)operator(()operator(\))operator(;) - -comment(// ----------------------------) - -predefined($argc) operator(==) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: )inline<delimiter({)predefined($argv)operator([)integer(0)operator(])delimiter(})>content( filename)char(\\n)delimiter(")>operator(\))operator(;) - -local_variable($filename) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) -local_variable($fs) operator(=) predefined(stat)operator(()local_variable($filename)operator(\))operator(;) - -local_variable($atime) operator(=) local_variable($fs)operator([)string<delimiter(')content(atime)delimiter(')>operator(])operator(;) -local_variable($mtime) operator(=) local_variable($fs)operator([)string<delimiter(')content(mtime)delimiter(')>operator(])operator(;) - -comment(// Careful here: since interactive, use, 'system', not 'exec', to launch [latter) -comment(// does not work under *NIX - at least, not for me :\)]) -predefined(system)operator(()predefined(trim)operator(()predefined(getenv)operator(()string<delimiter(')content(EDITOR)delimiter(')>operator(\)) operator(.) string<delimiter(')content( vi )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($filename)operator(\))operator(\))operator(,) local_variable($retcode)operator(\))operator(;) - -predefined(touch)operator(()local_variable($filename)operator(,) local_variable($mtime)operator(,) local_variable($atime)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Error updating timestamp on file, )inline<delimiter({)local_variable($filename)delimiter(})>content(!)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// @@PLEAC@@_9.2) -comment(// The 'unlink' function is used to delete regular files, whilst the 'rmdir' function) -comment(// does the same on non-empty directories. AFAIK, no recursive-deletion facility) -comment(// exists, and must be manually programmed) - -local_variable($filename) operator(=) string<delimiter(')content(...)delimiter(')>operator(;) - -exception(@)predefined(unlink)operator(()local_variable($filename)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Can't delete, )inline<delimiter({)local_variable($filename)delimiter(})>content(!)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// ------------) - -local_variable($files) operator(=) predefined(glob)operator(()string<delimiter(')content(...)delimiter(')>operator(\))operator(;) -local_variable($problem) operator(=) pre_constant(FALSE)operator(;) - -comment(// Could simply use a foreach loop) -reserved(foreach)operator(()local_variable($files) reserved(as) local_variable($filename)operator(\)) operator({) exception(@)predefined(unlink)operator(()local_variable($filename)operator(\)) operator(||) local_variable($problem) operator(=) pre_constant(TRUE)operator(;) operator(}) - -comment(//) -comment(// Alternatively, an applicative approach could be used, one closer in spirit to) -comment(// largely-functional languages like Scheme) -comment(//) -comment(// function is_all_deleted($deleted, $filename\) { return @unlink($filename\) && $deleted; }) -comment(// $problem = !array_reduce($files, 'is_all_deleted', TRUE\);) -comment(//) - -reserved(if) operator(()local_variable($problem)operator(\)) -operator({) - predefined(fwrite)operator(()constant(STDERR)operator(,) string<delimiter(')content(Could not delete all of:)delimiter(')>operator(\))operator(;) - reserved(foreach)operator(()local_variable($files) reserved(as) local_variable($filename)operator(\)) operator({) predefined(fwrite)operator(()constant(STDERR)operator(,) string<delimiter(')content( )delimiter(')> operator(.) local_variable($filename)operator(\))operator(;) operator(}) - predefined(fwrite)operator(()constant(STDERR)operator(,) string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) predefined(exit)operator(()integer(1)operator(\))operator(;) -operator(}) - -comment(// ------------) - -reserved(function) function(rmAll)operator(()local_variable($files)operator(\)) -operator({) - local_variable($count) operator(=) integer(0)operator(;) - - reserved(foreach)operator(()local_variable($files) reserved(as) local_variable($filename)operator(\)) operator({) exception(@)predefined(unlink)operator(()local_variable($filename)operator(\)) operator(&&) local_variable($count)operator(++)operator(;) operator(})operator(;) - - reserved(return) local_variable($count)operator(;) - -comment(// An applicative alternative using 'create_function', PHP's rough equivalent of 'lambda' ...) -comment(//) -comment(// return array_reduce($files,) -comment(// create_function('$count, $filename', 'return @unlink($filename\) && $count++;'\), 0\);) -operator(}) - -comment(// ----) - -local_variable($files) operator(=) predefined(glob)operator(()string<delimiter(')content(...)delimiter(')>operator(\))operator(;) -local_variable($toBeDeleted) operator(=) predefined(sizeof)operator(()local_variable($files)operator(\))operator(;) -local_variable($count) operator(=) ident(rmAll)operator(()local_variable($files)operator(\))operator(;) - -operator(()local_variable($count) operator(==) local_variable($toBeDeleted)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Could only delete )inline<delimiter({)local_variable($count)delimiter(})>content( of )inline<delimiter({)local_variable($toBeDeleted)delimiter(})>content( files)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// @@PLEAC@@_9.3) -local_variable($oldfile) operator(=) string<delimiter(')content(/tmp/old)delimiter(')>operator(;) local_variable($newfile) operator(=) string<delimiter(')content(/tmp/new)delimiter(')>operator(;) - -predefined(copy)operator(()local_variable($oldfile)operator(,) local_variable($newfile)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Error copying file)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// ----------------------------) - -comment(// All the following copy a file by copying its contents. Examples do so in a single) -comment(// operation, but it is also possible to copy arbitrary blocks, or, line-by-line in ) -comment(// the case of 'text' files) -local_variable($oldfile) operator(=) string<delimiter(')content(/tmp/old)delimiter(')>operator(;) local_variable($newfile) operator(=) string<delimiter(')content(/tmp/new)delimiter(')>operator(;) - -reserved(if) operator(()predefined(is_file)operator(()local_variable($oldfile)operator(\))operator(\)) - predefined(file_put_contents)operator(()local_variable($newfile)operator(,) predefined(file_get_contents)operator(()local_variable($oldfile)operator(\))operator(\))operator(;) -reserved(else) - predefined(die)operator(()string<delimiter(")content(Problem copying file )inline<delimiter({)local_variable($oldfile)delimiter(})>content( to file )inline<delimiter({)local_variable($newfile)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -comment(// ------------) - -local_variable($oldfile) operator(=) string<delimiter(')content(/tmp/old)delimiter(')>operator(;) local_variable($newfile) operator(=) string<delimiter(')content(/tmp/new)delimiter(')>operator(;) - -predefined(fwrite)operator(()operator(()local_variable($nh) operator(=) predefined(fopen)operator(()local_variable($newfile)operator(,) string<delimiter(')content(wb)delimiter(')>operator(\))operator(\))operator(,) predefined(fread)operator(()operator(()local_variable($oh) operator(=) predefined(fopen)operator(()local_variable($oldfile)operator(,) string<delimiter(')content(rb)delimiter(')>operator(\))operator(\))operator(,) predefined(filesize)operator(()local_variable($oldfile)operator(\))operator(\))operator(\))operator(;) -predefined(fclose)operator(()local_variable($oh)operator(\))operator(;) -predefined(fclose)operator(()local_variable($nh)operator(\))operator(;) - -comment(// ------------) - -comment(// As above, but with some error checking / handling) -local_variable($oldfile) operator(=) string<delimiter(')content(/tmp/old)delimiter(')>operator(;) local_variable($newfile) operator(=) string<delimiter(')content(/tmp/new)delimiter(')>operator(;) - -operator(()local_variable($oh) operator(=) predefined(fopen)operator(()local_variable($oldfile)operator(,) string<delimiter(')content(rb)delimiter(')>operator(\))operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Problem opening input file )inline<delimiter({)local_variable($oldfile)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) -operator(()local_variable($nh) operator(=) predefined(fopen)operator(()local_variable($newfile)operator(,) string<delimiter(')content(wb)delimiter(')>operator(\))operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Problem opening output file )inline<delimiter({)local_variable($newfile)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -reserved(if) operator(()operator(()local_variable($filesize) operator(=) predefined(filesize)operator(()local_variable($oldfile)operator(\))operator(\)) operator(>) integer(0)operator(\)) -operator({) - predefined(fwrite)operator(()local_variable($nh)operator(,) predefined(fread)operator(()local_variable($oh)operator(,) local_variable($filesize)operator(\))operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Problem reading / writing file data)char(\\n)delimiter(")>operator(\))operator(;) -operator(}) - -predefined(fclose)operator(()local_variable($oh)operator(\))operator(;) -predefined(fclose)operator(()local_variable($nh)operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Should there be platform-specfic problems copying 'very large' files, it is) -comment(// a simple matter to call a system command utility via, 'exec') - -comment(// *NIX-specific example. Could check whether, 'exec', succeeded, but checking whether) -comment(// a file exists after the operation might be a better approach) -local_variable($oldfile) operator(=) string<delimiter(')content(/tmp/old)delimiter(')>operator(;) local_variable($newfile) operator(=) string<delimiter(')content(/tmp/new)delimiter(')>operator(;) - -predefined(is_file)operator(()local_variable($newfile)operator(\)) operator(&&) predefined(unlink)operator(()local_variable($newfile)operator(\))operator(;) - -predefined(exec)operator(()predefined(trim)operator(()string<delimiter(')content(cp --force )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($oldfile)operator(\)) operator(.) string<delimiter(')content( )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($newfile)operator(\))operator(\))operator(\))operator(;) - -predefined(is_file)operator(()local_variable($newfile)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Problem copying file )inline<delimiter({)local_variable($oldfile)delimiter(})>content( to file )inline<delimiter({)local_variable($newfile)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -comment(// For other operating systems just change:) -comment(// * filenames) -comment(// * command being 'exec'ed) -comment(// as the rest of the code is platform independant) - -comment(// @@PLEAC@@_9.4) -reserved(function) function(makeDevInodePair)operator(()local_variable($filename)operator(\)) -operator({) - reserved(if) operator(()operator(!)operator(()local_variable($fs) operator(=) exception(@)predefined(stat)operator(()local_variable($filename)operator(\))operator(\))operator(\)) reserved(return) pre_constant(FALSE)operator(;) - reserved(return) predefined(strval)operator(()local_variable($fs)operator([)string<delimiter(')content(dev)delimiter(')>operator(]) operator(.) local_variable($fs)operator([)string<delimiter(')content(ino)delimiter(')>operator(])operator(\))operator(;) -operator(}) - -comment(// ------------) - -reserved(function) function(do_my_thing)operator(()local_variable($filename)operator(\)) -operator({) - comment(// Using a global variable to mimic Perl example, but could easily have passed) - comment(// '$seen' as an argument) - reserved(global) local_variable($seen)operator(;) - - local_variable($devino) operator(=) ident(makeDevInodePair)operator(()local_variable($filename)operator(\))operator(;) - - comment(// Process $filename if it has not previously been seen, else just increment) - reserved(if) operator(()operator(!)predefined(isset)operator(()local_variable($seen)operator([)local_variable($devino)operator(])operator(\))operator(\)) - operator({) - comment(// ... process $filename ...) - - comment(// Set initial count) - local_variable($seen)operator([)local_variable($devino)operator(]) operator(=) integer(1)operator(;) - operator(}) - reserved(else) - operator({) - comment(// Otherwise, just increment the count) - local_variable($seen)operator([)local_variable($devino)operator(]) operator(+=) integer(1)operator(;) - operator(}) -operator(}) - -comment(// ----) - -comment(// Simple example) -local_variable($seen) operator(=) predefined(array)operator(()operator(\))operator(;) - -ident(do_my_thing)operator(()string<delimiter(')content(/tmp/old)delimiter(')>operator(\))operator(;) -ident(do_my_thing)operator(()string<delimiter(')content(/tmp/old)delimiter(')>operator(\))operator(;) -ident(do_my_thing)operator(()string<delimiter(')content(/tmp/old)delimiter(')>operator(\))operator(;) -ident(do_my_thing)operator(()string<delimiter(')content(/tmp/new)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($seen) reserved(as) local_variable($devino) operator(=>) local_variable($count)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($devino)delimiter(})>content( -> )inline<delimiter({)local_variable($count)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -comment(// A variation on the above avoiding use of global variables, and illustrating use of) -comment(// easily-implemented 'higher order' techniques) - -comment(// Helper function loosely modelled on, 'array_reduce', but using an array as) -comment(// 'accumulator', which is returned on completion) -reserved(function) function(array_update)operator(()local_variable($arr)operator(,) local_variable($lambda)operator(,) local_variable($updarr)operator(\)) -operator({) - reserved(foreach)operator(()local_variable($arr) reserved(as) local_variable($key)operator(\)) local_variable($lambda)operator(()local_variable($updarr)operator(,) local_variable($key)operator(\))operator(;) - reserved(return) local_variable($updarr)operator(;) -operator(}) - -reserved(function) function(do_my_thing)operator(()operator(&)local_variable($seen)operator(,) local_variable($filename)operator(\)) -operator({) - reserved(if) operator(()operator(!)predefined(array_key_exists)operator(()operator(()local_variable($devino) operator(=) ident(makeDevInodePair)operator(()local_variable($filename)operator(\))operator(\))operator(,) local_variable($seen)operator(\))operator(\)) - operator({) - comment(// ... processing $filename ...) - - comment(// Update $seen) - local_variable($seen)operator([)local_variable($devino)operator(]) operator(=) integer(1)operator(;) - operator(}) - reserved(else) - operator({) - comment(// Update $seen) - local_variable($seen)operator([)local_variable($devino)operator(]) operator(+=) integer(1)operator(;) - operator(}) -operator(}) - -comment(// ----) - -comment(// Simple example) -local_variable($files) operator(=) predefined(array)operator(()string<delimiter(')content(/tmp/old)delimiter(')>operator(,) string<delimiter(')content(/tmp/old)delimiter(')>operator(,) string<delimiter(')content(/tmp/old)delimiter(')>operator(,) string<delimiter(')content(/tmp/new)delimiter(')>operator(\))operator(;) - -comment(// Could do this ...) -local_variable($seen) operator(=) predefined(array)operator(()operator(\))operator(;) -ident(array_update)operator(()local_variable($files)operator(,) string<delimiter(')content(do_my_thing)delimiter(')>operator(,) operator(&)local_variable($seen)operator(\))operator(;) - -comment(// or this:) -local_variable($seen) operator(=) ident(array_update)operator(()local_variable($files)operator(,) string<delimiter(')content(do_my_thing)delimiter(')>operator(,) predefined(array)operator(()operator(\))operator(\))operator(;) - -comment(// or a 'lambda' could be used:) -ident(array_update)operator(()local_variable($files)operator(,) - predefined(create_function)operator(()string<delimiter(')content($seen, $filename)delimiter(')>operator(,) string<delimiter(')content(... code not shown ...)delimiter(')>operator(\))operator(,) - operator(&)local_variable($seen)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($seen) reserved(as) local_variable($devino) operator(=>) local_variable($count)operator(\)) -operator({) - predefined(echo) string<delimiter(")inline<delimiter({)local_variable($devino)delimiter(})>content( -> )inline<delimiter({)local_variable($count)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ----------------------------) - -local_variable($files) operator(=) predefined(glob)operator(()string<delimiter(')content(/tmp/*)delimiter(')>operator(\))operator(;) - -predefined(define)operator(()constant(SEP)operator(,) string<delimiter(')content(;)delimiter(')>operator(\))operator(;) -local_variable($seen) operator(=) predefined(array)operator(()operator(\))operator(;) - -reserved(foreach)operator(()local_variable($files) reserved(as) local_variable($filename)operator(\)) -operator({) - reserved(if) operator(()operator(!)predefined(array_key_exists)operator(()operator(()local_variable($devino) operator(=) ident(makeDevInodePair)operator(()local_variable($filename)operator(\))operator(\))operator(,) local_variable($seen)operator(\))operator(\)) - local_variable($seen)operator([)local_variable($devino)operator(]) operator(=) local_variable($filename)operator(;) - reserved(else) - local_variable($seen)operator([)local_variable($devino)operator(]) operator(=) local_variable($seen)operator([)local_variable($devino)operator(]) operator(.) constant(SEP) operator(.) local_variable($filename)operator(;) -operator(}) - -local_variable($devino) operator(=) predefined(array_keys)operator(()local_variable($seen)operator(\))operator(;) -predefined(sort)operator(()local_variable($devino)operator(\))operator(;) - -reserved(foreach)operator(()local_variable($devino) reserved(as) local_variable($key)operator(\)) -operator({) - predefined(echo) local_variable($key) operator(.) string<delimiter(')content(:)delimiter(')>operator(;) - reserved(foreach)operator(()predefined(split)operator(()constant(SEP)operator(,) local_variable($seen)operator([)local_variable($key)operator(])operator(\)) reserved(as) local_variable($filename)operator(\)) predefined(echo) string<delimiter(')content( )delimiter(')> operator(.) local_variable($filename)operator(;) - predefined(echo) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_9.5) -comment(// Conventional POSIX-like approach to directory traversal) -local_variable($dirname) operator(=) string<delimiter(')content(/usr/bin/)delimiter(')>operator(;) - -operator(()local_variable($dirhdl) operator(=) predefined(opendir)operator(()local_variable($dirname)operator(\))operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Couldn't open )inline<delimiter({)local_variable($dirname)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -reserved(while) operator(()operator(()local_variable($file) operator(=) predefined(readdir)operator(()local_variable($dirhdl)operator(\))operator(\)) operator(!==) pre_constant(FALSE)operator(\)) -operator({) - operator(;) comment(// ... do something with $dirname/$file) - comment(// ...) -operator(}) - -predefined(closedir)operator(()local_variable($dirhdl)operator(\))operator(;) - -comment(// ------------) - -comment(// Newer [post PHP 4], 'applicative' approach - an array of filenames is) -comment(// generated that may be processed via external loop ...) - -local_variable($dirname) operator(=) string<delimiter(')content(/usr/bin/)delimiter(')>operator(;) - -reserved(foreach)operator(()predefined(scandir)operator(()local_variable($dirname)operator(\)) reserved(as) local_variable($file)operator(\)) -operator({) - operator(;) comment(// ... do something with $dirname/$file) - comment(// ...) -operator(}) - -comment(// .. or, via callback application, perhaps after massaging by one of the) -comment(// 'array' family of functions [also uses, 'array_update', from earlier section]) - -local_variable($newlist) operator(=) ident(array_update)operator(()predefined(array_reverse)operator(()predefined(scandir)operator(()local_variable($dirname)operator(\))operator(\))operator(,) - predefined(create_function)operator(()string<delimiter(')content($filelist, $file)delimiter(')>operator(,) string<delimiter(')content( ; )delimiter(')>operator(\))operator(,) - predefined(array)operator(()operator(\))operator(\))operator(;) - -comment(// And don't forget that the old standby, 'glob', that returns an array of) -comment(// paths filtered using the Bourne Shell-based wildcards, '?' and '*', is) -comment(// also available) - -reserved(foreach)operator(()predefined(glob)operator(()local_variable($dirname) operator(.) string<delimiter(')content(*)delimiter(')>operator(\)) reserved(as) local_variable($path)operator(\)) -operator({) - operator(;) comment(// ... do something with $path) - comment(// ...) -operator(}) - -comment(// ----------------------------) - -comment(// Uses, 'isTextFile', from an earlier section) -local_variable($dirname) operator(=) string<delimiter(')content(/usr/bin/)delimiter(')>operator(;) - -predefined(echo) string<delimiter(")content(Text files in )inline<delimiter({)local_variable($dirname)delimiter(})>content(:)char(\\n)delimiter(")>operator(;) - -reserved(foreach)operator(()predefined(scandir)operator(()local_variable($dirname)operator(\)) reserved(as) local_variable($file)operator(\)) -operator({) - comment(// Take care when constructing paths to ensure cross-platform operability ) - local_variable($path) operator(=) local_variable($dirname) operator(.) local_variable($file)operator(;) - - reserved(if) operator(()predefined(is_file)operator(()local_variable($path)operator(\)) operator(&&) ident(isTextFile)operator(()local_variable($path)operator(\))operator(\)) predefined(echo) local_variable($path) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ----------------------------) - -reserved(function) function(plain_files)operator(()local_variable($dirname)operator(\)) -operator({) - operator(()local_variable($dirlist) operator(=) predefined(glob)operator(()local_variable($dirname) operator(.) string<delimiter(')content(*)delimiter(')>operator(\))operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Couldn't glob )inline<delimiter({)local_variable($dirname)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - - comment(// Pass function name directly if only a single function performs filter test) - reserved(return) predefined(array_filter)operator(()local_variable($dirlist)operator(,) string<delimiter(')content(is_file)delimiter(')>operator(\))operator(;) - - comment(// Use, 'create_function', if a multi-function test is needed) - comment(//) - comment(// return array_filter($dirlist, create_function('$path', 'return is_file($path\);'\)\);) - comment(//) -operator(}) - -comment(// ------------) - -reserved(foreach)operator(()ident(plain_files)operator(()string<delimiter(')content(/tmp/)delimiter(')>operator(\)) reserved(as) local_variable($path)operator(\)) -operator({) - predefined(echo) local_variable($path) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_9.6) -local_variable($dirname) operator(=) string<delimiter(')content(/tmp/)delimiter(')>operator(;) - -comment(// Full paths) -local_variable($pathlist) operator(=) predefined(glob)operator(()local_variable($dirname) operator(.) string<delimiter(')content(*.c)delimiter(')>operator(\))operator(;) - -comment(// File names only - glob-based matching) -local_variable($filelist) operator(=) predefined(array_filter)operator(()predefined(scandir)operator(()local_variable($dirname)operator(\))operator(,) - predefined(create_function)operator(()string<delimiter(')content($file)delimiter(')>operator(,) string<delimiter(')content(return fnmatch("*.c", $file\);)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($dirname) operator(=) string<delimiter(')content(/tmp/)delimiter(')>operator(;) - -comment(// File names only - regex-based matching [case-insensitive]) -local_variable($filelist) operator(=) predefined(array_filter)operator(()predefined(scandir)operator(()local_variable($dirname)operator(\))operator(,) - predefined(create_function)operator(()string<delimiter(')content($file)delimiter(')>operator(,) string<delimiter(')content(return eregi(")content(\\.)content([ch]$", $file\);)delimiter(')>operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($dirname) operator(=) string<delimiter(')content(/tmp/)delimiter(')>operator(;) - -comment(// Directory names - all-digit names) -local_variable($dirs) operator(=) predefined(array_filter)operator(()predefined(glob)operator(()local_variable($dirname) operator(.) string<delimiter(')content(*)delimiter(')>operator(,) constant(GLOB_ONLYDIR)operator(\))operator(,) - predefined(create_function)operator(()string<delimiter(')content($path)delimiter(')>operator(,) string<delimiter(')content(return ereg("^[0-9]+$", basename($path\)\);)delimiter(')>operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_9.7) -comment(// Recursive directory traversal function and helper: traverses a directory tree) -comment(// applying a function [and a variable number of accompanying arguments] to each) -comment(// file) - -reserved(class) class(Accumulator) -operator({) - reserved(public) local_variable($value)operator(;) - reserved(public) reserved(function) function(__construct)operator(()local_variable($start_value)operator(\)) operator({) local_variable($this)operator(->)ident(value) operator(=) local_variable($start_value)operator(;) operator(}) -operator(}) - -comment(// ------------) - -reserved(function) function(process_directory_)operator(()local_variable($op)operator(,) local_variable($func_args)operator(\)) -operator({) - reserved(if) operator(()predefined(is_dir)operator(()local_variable($func_args)operator([)integer(0)operator(])operator(\))operator(\)) - operator({) - local_variable($current) operator(=) local_variable($func_args)operator([)integer(0)operator(])operator(;) - reserved(foreach)operator(()predefined(scandir)operator(()local_variable($current)operator(\)) reserved(as) local_variable($entry)operator(\)) - operator({) - reserved(if) operator(()local_variable($entry) operator(==) string<delimiter(')content(.)delimiter(')> operator(||) local_variable($entry) operator(==) string<delimiter(')content(..)delimiter(')>operator(\)) reserved(continue)operator(;) - local_variable($func_args)operator([)integer(0)operator(]) operator(=) local_variable($current) operator(.) string<delimiter(')content(/)delimiter(')> operator(.) local_variable($entry)operator(;) - ident(process_directory_)operator(()local_variable($op)operator(,) local_variable($func_args)operator(\))operator(;) - operator(}) - operator(}) - reserved(else) - operator({) - predefined(call_user_func_array)operator(()local_variable($op)operator(,) local_variable($func_args)operator(\))operator(;) - operator(}) -operator(}) - -reserved(function) function(process_directory)operator(()local_variable($op)operator(,) local_variable($dir)operator(\)) -operator({) - reserved(if) operator(()operator(!)predefined(is_dir)operator(()local_variable($dir)operator(\))operator(\)) reserved(return) pre_constant(FALSE)operator(;) - local_variable($func_args) operator(=) predefined(array_slice)operator(()predefined(func_get_args)operator(()operator(\))operator(,) integer(1)operator(\))operator(;) - ident(process_directory_)operator(()local_variable($op)operator(,) local_variable($func_args)operator(\))operator(;) - reserved(return) pre_constant(TRUE)operator(;) -operator(}) - -comment(// ----------------------------) - -local_variable($dirlist) operator(=) predefined(array)operator(()string<delimiter(')content(/tmp/d1)delimiter(')>operator(,) string<delimiter(')content(/tmp/d2)delimiter(')>operator(,) string<delimiter(')content(/tmp/d3)delimiter(')>operator(\))operator(;) - -comment(// Do something with each directory in the list) -reserved(foreach)operator(()local_variable($dirlist) reserved(as) local_variable($dir)operator(\)) -operator({) - operator(;) - comment(// Delete directory [if empty] -> rmdir($dir\); ) - comment(// Make it the 'current directory' -> chdir($dir\);) - comment(// Get list of files it contains -> $filelist = scandir($dir\);) - comment(// Get directory metadata -> $ds = stat($dir\);) -operator(}) - -comment(// ------------) - -local_variable($dirlist) operator(=) predefined(array)operator(()string<delimiter(')content(/tmp/d1)delimiter(')>operator(,) string<delimiter(')content(/tmp/d2)delimiter(')>operator(,) string<delimiter(')content(/tmp/d3)delimiter(')>operator(\))operator(;) - -reserved(function) function(pf)operator(()local_variable($path)operator(\)) -operator({) - comment(// ... do something to the file or directory ...) - predefined(printf)operator(()string<delimiter(")content(%s)char(\\n)delimiter(")>operator(,) local_variable($path)operator(\))operator(;) -operator(}) - -comment(// For each directory in the list ...) -reserved(foreach)operator(()local_variable($dirlist) reserved(as) local_variable($dir)operator(\)) -operator({) - comment(// Is this a valid directory ?) - reserved(if) operator(()operator(!)predefined(is_dir)operator(()local_variable($dir)operator(\))operator(\)) operator({) predefined(printf)operator(()string<delimiter(")content(%s does not exist)char(\\n)delimiter(")>operator(,) local_variable($dir)operator(\))operator(;) reserved(continue)operator(;) operator(}) - - comment(// Ok, so get all the directory's entries) - local_variable($filelist) operator(=) predefined(scandir)operator(()local_variable($dir)operator(\))operator(;) - - comment(// An 'empty' directory will contain at least two entries: '..' and '.') - reserved(if) operator(()predefined(count)operator(()local_variable($filelist)operator(\)) operator(==) integer(2)operator(\)) operator({) predefined(printf)operator(()string<delimiter(")content(%s is empty)char(\\n)delimiter(")>operator(,) local_variable($dir)operator(\))operator(;) reserved(continue)operator(;) operator(}) - - comment(// For each file / directory in the directory ...) - reserved(foreach)operator(()local_variable($filelist) reserved(as) local_variable($file)operator(\)) - operator({) - comment(// Ignore '..' and '.' entries) - reserved(if) operator(()local_variable($file) operator(==) string<delimiter(')content(.)delimiter(')> operator(||) local_variable($file) operator(==) string<delimiter(')content(..)delimiter(')>operator(\)) reserved(continue)operator(;) - - comment(// Apply function to process the file / directory) - ident(pf)operator(()local_variable($dir) operator(.) string<delimiter(')content(/)delimiter(')> operator(.) local_variable($file)operator(\))operator(;) - operator(}) -operator(}) - -comment(// ----------------------------) - -reserved(function) function(accum_filesize)operator(()local_variable($file)operator(,) local_variable($accum)operator(\)) -operator({) - predefined(is_file)operator(()local_variable($file)operator(\)) operator(&&) operator(()local_variable($accum)operator(->)ident(value) operator(+=) predefined(filesize)operator(()local_variable($file)operator(\))operator(\))operator(;) -operator(}) - -comment(// ------------) - -comment(// Verify arguments ...) -predefined($argc) operator(==) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: )inline<delimiter({)predefined($argv)operator([)integer(0)operator(])delimiter(})>content( dir)char(\\n)delimiter(")>operator(\))operator(;) -local_variable($dir) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) - -predefined(is_dir)operator(()local_variable($dir)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")inline<delimiter({)local_variable($dir)delimiter(})>content( does not exist / not a directory)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// Collect data [use an object to accumulate results]) -local_variable($dirsize) operator(=) reserved(new) constant(Accumulator)operator(()integer(0)operator(\))operator(;) -ident(process_directory)operator(()string<delimiter(')content(accum_filesize)delimiter(')>operator(,) local_variable($dir)operator(,) local_variable($dirsize)operator(\))operator(;) - -comment(// Report results) -predefined(printf)operator(()string<delimiter(")content(%s contains %d bytes)char(\\n)delimiter(")>operator(,) local_variable($dir)operator(,) local_variable($dirsize)operator(->)ident(value)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(biggest_file)operator(()local_variable($file)operator(,) local_variable($accum)operator(\)) -operator({) - reserved(if) operator(()predefined(is_file)operator(()local_variable($file)operator(\))operator(\)) - operator({) - local_variable($fs) operator(=) predefined(filesize)operator(()local_variable($file)operator(\))operator(;) - reserved(if) operator(()local_variable($accum)operator(->)ident(value)operator([)integer(1)operator(]) operator(<) local_variable($fs)operator(\)) operator({) local_variable($accum)operator(->)ident(value)operator([)integer(0)operator(]) operator(=) local_variable($file)operator(;) local_variable($accum)operator(->)ident(value)operator([)integer(1)operator(]) operator(=) local_variable($fs)operator(;) operator(}) - operator(}) -operator(}) - -comment(// ------------) - -comment(// Verify arguments ...) -predefined($argc) operator(==) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: )inline<delimiter({)predefined($argv)operator([)integer(0)operator(])delimiter(})>content( dir)char(\\n)delimiter(")>operator(\))operator(;) -local_variable($dir) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) - -predefined(is_dir)operator(()local_variable($dir)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")inline<delimiter({)local_variable($dir)delimiter(})>content( does not exist / not a directory)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// Collect data [use an object to accumulate results]) -local_variable($biggest) operator(=) reserved(new) constant(Accumulator)operator(()predefined(array)operator(()string<delimiter(')delimiter(')>operator(,) integer(0)operator(\))operator(\))operator(;) -ident(process_directory)operator(()string<delimiter(')content(biggest_file)delimiter(')>operator(,) local_variable($dir)operator(,) local_variable($biggest)operator(\))operator(;) - -comment(// Report results) -predefined(printf)operator(()string<delimiter(")content(Biggest file is %s containing %d bytes)char(\\n)delimiter(")>operator(,) local_variable($biggest)operator(->)ident(value)operator([)integer(0)operator(])operator(,) local_variable($biggest)operator(->)ident(value)operator([)integer(1)operator(])operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(youngest_file)operator(()local_variable($file)operator(,) local_variable($accum)operator(\)) -operator({) - reserved(if) operator(()predefined(is_file)operator(()local_variable($file)operator(\))operator(\)) - operator({) - local_variable($fct) operator(=) predefined(filectime)operator(()local_variable($file)operator(\))operator(;) - reserved(if) operator(()local_variable($accum)operator(->)ident(value)operator([)integer(1)operator(]) operator(>) local_variable($fct)operator(\)) operator({) local_variable($accum)operator(->)ident(value)operator([)integer(0)operator(]) operator(=) local_variable($file)operator(;) local_variable($accum)operator(->)ident(value)operator([)integer(1)operator(]) operator(=) local_variable($fct)operator(;) operator(}) - operator(}) -operator(}) - -comment(// ------------) - -comment(// Verify arguments ...) -predefined($argc) operator(==) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: )inline<delimiter({)predefined($argv)operator([)integer(0)operator(])delimiter(})>content( dir)char(\\n)delimiter(")>operator(\))operator(;) -local_variable($dir) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) - -predefined(is_dir)operator(()local_variable($dir)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")inline<delimiter({)local_variable($dir)delimiter(})>content( does not exist / not a directory)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// Collect data [use an object to accumulate results]) -local_variable($youngest) operator(=) reserved(new) constant(Accumulator)operator(()predefined(array)operator(()string<delimiter(')delimiter(')>operator(,) integer(2147483647)operator(\))operator(\))operator(;) -ident(process_directory)operator(()string<delimiter(')content(youngest_file)delimiter(')>operator(,) local_variable($dir)operator(,) local_variable($youngest)operator(\))operator(;) - -comment(// Report results) -predefined(printf)operator(()string<delimiter(")content(Youngest file is %s dating %s)char(\\n)delimiter(")>operator(,) local_variable($youngest)operator(->)ident(value)operator([)integer(0)operator(])operator(,) predefined(date)operator(()constant(DATE_ATOM)operator(,) local_variable($youngest)operator(->)ident(value)operator([)integer(1)operator(])operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_9.8) -comment(// AFAICT, there is currently no library function that recursively removes a) -comment(// directory tree [i.e. a directory, it's subdirectories, and any other files]) -comment(// with a single call. Such a function needs to be custom built. PHP tools) -comment(// with which to do this:) -comment(// * 'unlink', 'rmdir', 'is_dir', and 'is_file' functions, will all take care) -comment(// of the file testing and deletion) -comment(// * Actual directory traversal requires obtaining directory / subdirectory) -comment(// lists, and here there is much choice available, though care must be taken) -comment(// as each has it's own quirks) -comment(// - 'opendir', 'readdir', 'closedir') -comment(// - 'scandir') -comment(// - 'glob') -comment(// - SPL 'directory iterator' classes [newish / experimental - not shown here]) -comment(//) -comment(// The PHP documentation for 'rmdir' contains several examples, each illustrating) -comment(// one of each approach; the example shown here is loosely based on one of these) -comment(// examples) - -comment(// Recursor - recursively traverses directory tree) -reserved(function) function(rmtree_)operator(()local_variable($dir)operator(\)) -operator({) - local_variable($dir) operator(=) string<delimiter(")local_variable($dir)delimiter(")>operator(;) - - reserved(if) operator(()local_variable($dh) operator(=) predefined(opendir)operator(()local_variable($dir)operator(\))operator(\)) - operator({) - reserved(while) operator(()pre_constant(FALSE) operator(!==) operator(()local_variable($item) operator(=) predefined(readdir)operator(()local_variable($dh)operator(\))operator(\))operator(\)) - operator({) - reserved(if) operator(()local_variable($item) operator(!=) string<delimiter(')content(.)delimiter(')> operator(&&) local_variable($item) operator(!=) string<delimiter(')content(..)delimiter(')>operator(\)) - operator({) - local_variable($subdir) operator(=) local_variable($dir) operator(.) string<delimiter(')content(/)delimiter(')> operator(.) string<delimiter(")local_variable($item)delimiter(")>operator(;) - - reserved(if) operator(()predefined(is_dir)operator(()local_variable($subdir)operator(\))operator(\)) ident(rmtree_)operator(()local_variable($subdir)operator(\))operator(;) - reserved(else) exception(@)predefined(unlink)operator(()local_variable($subdir)operator(\))operator(;) - operator(}) - operator(}) - - predefined(closedir)operator(()local_variable($dh)operator(\))operator(;) exception(@)predefined(rmdir)operator(()local_variable($dir)operator(\))operator(;) - operator(}) -operator(}) - -comment(// Launcher - performs validation then starts recursive routine) -reserved(function) function(rmtree)operator(()local_variable($dir)operator(\)) -operator({) - reserved(if) operator(()predefined(is_dir)operator(()local_variable($dir)operator(\))operator(\)) - operator({) - operator(()predefined(substr)operator(()local_variable($dir)operator(,) operator(-)integer(1)operator(,) integer(1)operator(\)) operator(==) string<delimiter(')content(/)delimiter(')>operator(\)) operator(&&) operator(()local_variable($dir) operator(=) predefined(substr)operator(()local_variable($dir)operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(\))operator(;) - ident(rmtree_)operator(()local_variable($dir)operator(\))operator(;) reserved(return) operator(!)predefined(is_dir)operator(()local_variable($dir)operator(\))operator(;) - operator(}) - - reserved(return) pre_constant(FALSE)operator(;) -operator(}) - -comment(// ------------) - -predefined($argc) operator(==) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: rmtree dir)char(\\n)delimiter(")>operator(\))operator(;) - -ident(rmtree)operator(()predefined($argv)operator([)integer(1)operator(])operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Could not remove directory )inline<delimiter({)predefined($argv)operator([)integer(1)operator(])delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -comment(// @@PLEAC@@_9.9) -local_variable($filepairs) operator(=) predefined(array)operator(()string<delimiter(')content(x.txt)delimiter(')> operator(=>) string<delimiter(')content(x2.txt)delimiter(')>operator(,) string<delimiter(')content(y.txt)delimiter(')> operator(=>) string<delimiter(')content(y.doc)delimiter(')>operator(,) string<delimiter(')content(zxc.txt)delimiter(')> operator(=>) string<delimiter(')content(cxz.txt)delimiter(')>operator(\))operator(;) - -reserved(foreach)operator(()local_variable($filepairs) reserved(as) local_variable($oldfile) operator(=>) local_variable($newfile)operator(\)) -operator({) - exception(@)predefined(rename)operator(()local_variable($oldfile)operator(,) local_variable($newfile)operator(\)) operator(||) predefined(fwrite)operator(()constant(STDERR)operator(,) predefined(sprintf)operator(()string<delimiter(")content(Could not rename %s to %s)char(\\n)delimiter(")>operator(,) local_variable($oldfile)operator(,) local_variable($newfile)operator(\))operator(\))operator(;) -operator(}) - -comment(// ----------------------------) - -comment(// Call a system command utility via, 'exec'. *NIX-specific example. Could check whether,) -comment(// 'exec', succeeded, but checking whether a renamed file exists after the operation might) -comment(// be a better approach) - -local_variable($oldfile) operator(=) string<delimiter(')content(/tmp/old)delimiter(')>operator(;) local_variable($newfile) operator(=) string<delimiter(')content(/tmp/new)delimiter(')>operator(;) - -predefined(is_file)operator(()local_variable($newfile)operator(\)) operator(&&) predefined(unlink)operator(()local_variable($newfile)operator(\))operator(;) - -predefined(exec)operator(()predefined(trim)operator(()string<delimiter(')content(mv --force )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($oldfile)operator(\)) operator(.) string<delimiter(')content( )delimiter(')> operator(.) predefined(escapeshellarg)operator(()local_variable($newfile)operator(\))operator(\))operator(\))operator(;) - -predefined(is_file)operator(()local_variable($oldfile)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Problem renaming file )inline<delimiter({)local_variable($oldfile)delimiter(})>content( to file )inline<delimiter({)local_variable($newfile)delimiter(})>char(\\n)delimiter(")>operator(\))operator(;) - -comment(// For other operating systems just change:) -comment(// * filenames) -comment(// * command being 'exec'ed) -comment(// as the rest of the code is platform independant) - -comment(// ----------------------------) - -comment(// A modified implementation of Larry's Filename Fixer. Rather than passing) -comment(// a single expression, a 'from' regexp is passed; each match in the file) -comment(// name(s\) is changed to the value of 'to'. It otherwise behaves the same) -comment(//) - -predefined($argc) operator(>) integer(2) operator(||) predefined(die)operator(()string<delimiter(")content(usage: rename from to [file ...])char(\\n)delimiter(")>operator(\))operator(;) - -local_variable($from) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) -local_variable($to) operator(=) predefined($argv)operator([)integer(2)operator(])operator(;) - -reserved(if) operator(()predefined(count)operator(()operator(()predefined($argv) operator(=) predefined(array_slice)operator(()predefined($argv)operator(,) integer(3)operator(\))operator(\))operator(\)) operator(<) integer(1)operator(\)) - reserved(while) operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) predefined($argv)operator([)operator(]) operator(=) predefined(substr)operator(()predefined(fgets)operator(()constant(STDIN)operator(\))operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(;) - -reserved(foreach)operator(()predefined($argv) reserved(as) local_variable($file)operator(\)) -operator({) - local_variable($was) operator(=) local_variable($file)operator(;) - local_variable($file) operator(=) ident(ereg_replace)operator(()local_variable($from)operator(,) local_variable($to)operator(,) local_variable($file)operator(\))operator(;) - - reserved(if) operator(()predefined(strcmp)operator(()local_variable($was)operator(,) local_variable($file)operator(\)) operator(!=) integer(0)operator(\)) - exception(@)predefined(rename)operator(()local_variable($was)operator(,) local_variable($file)operator(\)) operator(||) predefined(fwrite)operator(()constant(STDERR)operator(,) predefined(sprintf)operator(()string<delimiter(")content(Could not rename %s to %s)char(\\n)delimiter(")>operator(,) local_variable($was)operator(,) local_variable($file)operator(\))operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_9.10) -local_variable($base) operator(=) predefined(basename)operator(()local_variable($path)operator(\))operator(;) -local_variable($dir) operator(=) predefined(dirname)operator(()local_variable($path)operator(\))operator(;) - -comment(// PHP's equivalent to Perl's 'fileparse') -local_variable($pathinfo) operator(=) predefined(pathinfo)operator(()local_variable($path)operator(\))operator(;) - -local_variable($base) operator(=) local_variable($pathinfo)operator([)string<delimiter(')content(basename)delimiter(')>operator(])operator(;) -local_variable($dir) operator(=) local_variable($pathinfo)operator([)string<delimiter(')content(dirname)delimiter(')>operator(])operator(;) -local_variable($ext) operator(=) local_variable($pathinfo)operator([)string<delimiter(')content(extension)delimiter(')>operator(])operator(;) - -comment(// ----------------------------) - -local_variable($path) operator(=) string<delimiter(')content(/usr/lib/libc.a)delimiter(')>operator(;) - -predefined(printf)operator(()string<delimiter(")content(dir is %s, file is %s)char(\\n)delimiter(")>operator(,) predefined(dirname)operator(()local_variable($path)operator(\))operator(,) predefined(basename)operator(()local_variable($path)operator(\))operator(\))operator(;) - -comment(// ------------) - -local_variable($path) operator(=) string<delimiter(')content(/usr/lib/libc.a)delimiter(')>operator(;) - -local_variable($pathinfo) operator(=) predefined(pathinfo)operator(()local_variable($path)operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(dir is %s, name is %s, extension is %s)char(\\n)delimiter(")>operator(,) local_variable($pathinfo)operator([)string<delimiter(')content(dirname)delimiter(')>operator(])operator(,) local_variable($pathinfo)operator([)string<delimiter(')content(basename)delimiter(')>operator(])operator(,) local_variable($pathinfo)operator([)string<delimiter(')content(extension)delimiter(')>operator(])operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Handle Mac example as a simple parse task. However, AFAIK, 'pathinfo' is cross-platform,) -comment(// so should handle file path format differences transparently) -local_variable($path) operator(=) string<delimiter(')content(Hard%20Drive:System%20Folder:README.txt)delimiter(')>operator(;) - -local_variable($macp) operator(=) predefined(array_combine)operator(()predefined(array)operator(()string<delimiter(')content(drive)delimiter(')>operator(,) string<delimiter(')content(folder)delimiter(')>operator(,) string<delimiter(')content(filename)delimiter(')>operator(\))operator(,) predefined(split)operator(()string<delimiter(")content(\\:)delimiter(")>operator(,) predefined(str_replace)operator(()string<delimiter(')content(%20)delimiter(')>operator(,) string<delimiter(')content( )delimiter(')>operator(,) local_variable($path)operator(\))operator(\))operator(\))operator(;) -local_variable($macf) operator(=) predefined(array_combine)operator(()predefined(array)operator(()string<delimiter(')content(name)delimiter(')>operator(,) string<delimiter(')content(extension)delimiter(')>operator(\))operator(,) predefined(split)operator(()string<delimiter(")content(\\.)delimiter(")>operator(,) local_variable($macp)operator([)string<delimiter(')content(filename)delimiter(')>operator(])operator(\))operator(\))operator(;) - -predefined(printf)operator(()string<delimiter(")content(dir is %s, name is %s, extension is %s)char(\\n)delimiter(")>operator(,) operator(()local_variable($macp)operator([)string<delimiter(')content(drive)delimiter(')>operator(]) operator(.) string<delimiter(')content(:)delimiter(')> operator(.) local_variable($macp)operator([)string<delimiter(')content(folder)delimiter(')>operator(])operator(\))operator(,) local_variable($macf)operator([)string<delimiter(')content(name)delimiter(')>operator(])operator(,) operator(()string<delimiter(')content(.)delimiter(')> operator(.) local_variable($macf)operator([)string<delimiter(')content(extension)delimiter(')>operator(])operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Not really necessary since we have, 'pathinfo', but better matches Perl example) -reserved(function) function(file_extension)operator(()local_variable($filename)operator(,) local_variable($separator) operator(=) string<delimiter(')content(.)delimiter(')>operator(\)) -operator({) - reserved(return) predefined(end)operator(()predefined(split)operator(()operator(()string<delimiter(")char(\\\\)delimiter(")> operator(.) local_variable($separator)operator(\))operator(,) local_variable($filename)operator(\))operator(\))operator(;) -operator(}) - -comment(// ----) - -predefined(echo) ident(file_extension)operator(()string<delimiter(')content(readme.txt)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_9.11) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_9.12) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_10.0) -comment(// Since defined at outermost scope, $greeted may be considered a global variable) -local_variable($greeted) operator(=) integer(0)operator(;) - -comment(// ------------) - -comment(// Must use, 'global', keyword to inform functions that $greeted already exists as) -comment(// a global variable. If this is not done, a local variable of that name is implicitly) -comment(// defined) -reserved(function) function(howManyGreetings)operator(()operator(\)) -operator({) - reserved(global) local_variable($greeted)operator(;) - reserved(return) local_variable($greeted)operator(;) -operator(}) - -reserved(function) function(hello)operator(()operator(\)) -operator({) - reserved(global) local_variable($greeted)operator(;) - local_variable($greeted)operator(++)operator(;) - predefined(echo) string<delimiter(")content(high there!, this procedure has been called )inline<delimiter({)local_variable($greeted)delimiter(})>content( times)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -ident(hello)operator(()operator(\))operator(;) -local_variable($greetings) operator(=) ident(howManyGreetings)operator(()operator(\))operator(;) -predefined(echo) string<delimiter(")content(bye there!, there have been )inline<delimiter({)local_variable($greetings)delimiter(})>content( greetings so far)char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_10.1) -comment(// Conventionally-defined function together with parameter list) -reserved(function) function(hypotenuse)operator(()local_variable($side1)operator(,) local_variable($side2)operator(\)) -operator({) - reserved(return) predefined(sqrt)operator(()predefined(pow)operator(()local_variable($side1)operator(,) integer(2)operator(\)) operator(+) predefined(pow)operator(()local_variable($side2)operator(,) integer(2)operator(\))operator(\))operator(;) -operator(}) - -comment(// ----) - -comment(// Alternative is to define the function without parameter list, then use) -comment(// 'func_get_arg' to extract arguments) -reserved(function) function(hypotenuse)operator(()operator(\)) -operator({) - comment(// Could check number of arguments passed with: 'func_num_args', which) - comment(// would be the approach used if dealing with variable number of arguments) - local_variable($side1) operator(=) predefined(func_get_arg)operator(()integer(0)operator(\))operator(;) local_variable($side2) operator(=) predefined(func_get_arg)operator(()integer(1)operator(\))operator(;) - - reserved(return) predefined(sqrt)operator(()predefined(pow)operator(()local_variable($side1)operator(,) integer(2)operator(\)) operator(+) predefined(pow)operator(()local_variable($side2)operator(,) integer(2)operator(\))operator(\))operator(;) -operator(}) - -comment(// ------------) - -comment(// 1. Conventional function call) -local_variable($diag) operator(=) ident(hypotenuse)operator(()integer(3)operator(,) integer(4)operator(\))operator(;) - -comment(// ------------) - -comment(// 2. Function call using, 'call_user_func' library routines) -local_variable($funcname) operator(=) string<delimiter(')content(hypotenuse)delimiter(')>operator(;) - -comment(// a. Pass function name, and variable number of arguments) -local_variable($diag) operator(=) ident(call_user_func)operator(()local_variable($funcname)operator(,) integer(3)operator(,) integer(4)operator(\))operator(;) - -comment(// b. Package arguments as array, pass together with function name) -local_variable($args) operator(=) predefined(array)operator(()integer(3)operator(,) integer(4)operator(\))operator(;) -local_variable($diag) operator(=) predefined(call_user_func_array)operator(()local_variable($funcname)operator(,) local_variable($args)operator(\))operator(;) - -comment(// ----------------------------) - -local_variable($nums) operator(=) predefined(array)operator(()float(1.4)operator(,) float(3.5)operator(,) float(6.7)operator(\))operator(;) - -comment(// ------------) - -comment(// Pass-by-value) -reserved(function) function(int_all)operator(()local_variable($arr)operator(\)) -operator({) - reserved(return) predefined(array_map)operator(()predefined(create_function)operator(()string<delimiter(')content($n)delimiter(')>operator(,) string<delimiter(')content(return (int\) $n;)delimiter(')>operator(\))operator(,) local_variable($arr)operator(\))operator(;) -operator(}) - -comment(// Pass-by-reference) -reserved(function) function(trunc_em)operator(()operator(&)local_variable($n)operator(\)) -operator({) - reserved(foreach) operator(()local_variable($n) reserved(as) operator(&)local_variable($value)operator(\)) local_variable($value) operator(=) operator(()pre_type(int)operator(\)) local_variable($value)operator(;) -operator(}) - -comment(// ------------) - -comment(// $nums untouched; $ints is new array) -local_variable($ints) operator(=) ident(int_all)operator(()local_variable($nums)operator(\))operator(;) - -comment(// $nums updated) -ident(trunc_em)operator(()local_variable($nums)operator(\))operator(;) - -comment(// @@PLEAC@@_10.2) -comment(// Strictly-speaking, PHP is neither lexically [no environment capture] nor) -comment(// dynamically [no variable shadowing] scoped. A script in which several) -comment(// functions have been defined has two, entirely separate, scopes:) -comment(//) -comment(// * A 'top-level' scope i.e. everything outside each function) -comment(//) -comment(// * A 'local scope' within each function; each function is a self-contained) -comment(// entity and cannot [via conventional means] access variables outside its) -comment(// local scope. Accessing a variable that has not been locally defined) -comment(// serves to define it i.e. accessing a variable assumed to be global) -comment(// sees a local variable of that name defined) -comment(//) -comment(// The way 'global' variables are provided is via a predefined array of) -comment(// variable names, $GLOBALS [it is one of a special set of variables known) -comment(// as 'superglobals'; such variables *are* accessable in all scopes]. Each) -comment(// entry in this array is a 'global' variable name, and may be freely) -comment(// accessed / updated. A more convenient means of accessing such variables) -comment(// is via the 'global' keyword: one or more variables within a function is) -comment(// declared 'global', and those names are then taken to refer to entries) -comment(// in the $GLOBALS array rather than seeing local variables of that name) -comment(// accessed or defined) - -reserved(function) function(some_func)operator(()operator(\)) -operator({) - comment(// Variables declared within a function are local to that function) - local_variable($variable) operator(=) string<delimiter(')content(something)delimiter(')>operator(;) -operator(}) - -comment(// ----------------------------) - -comment(// Top-level declared variables) -local_variable($name) operator(=) predefined($argv)operator([)integer(1)operator(])operator(;) local_variable($age) operator(=) predefined($argv)operator([)integer(2)operator(])operator(;) - -local_variable($c) operator(=) ident(fetch_time)operator(()operator(\))operator(;) - -local_variable($condition) operator(=) integer(0)operator(;) - -comment(// ------------) - -reserved(function) function(run_check)operator(()operator(\)) -operator({) - comment(// The globally-declared variable, '$condition', is not accessable within) - comment(// the function unless it declared as 'global. Had this not been done then) - comment(// attempts to access, '$condition', would have seen a local variable) - comment(// of that name declared and updated. Same applies to other variables) - reserved(global) local_variable($condition)operator(,) local_variable($name)operator(,) local_variable($age)operator(,) local_variable($c)operator(;) - - local_variable($condition) operator(=) integer(1)operator(;) - comment(// ...) -operator(}) - -reserved(function) function(check_x)operator(()local_variable($x)operator(\)) -operator({) - local_variable($y) operator(=) string<delimiter(')content(whatever)delimiter(')>operator(;) - - comment(// This function only has access to the parameter, '$x', and the locally) - comment(// declared variable, '$y'.) - - comment(// Whilst 'run_check' has access to several global variables, the current) - comment(// function does not. For it to access the global variable, '$condition',) - comment(// it must be declared 'global') - ident(run_check)operator(()operator(\))operator(;) - - reserved(global) local_variable($condition)operator(;) - - comment(// 'run_check' will have updated, '$condition', and since it has been) - comment(// declared 'global' here, it is accessable) - - reserved(if) operator(()local_variable($condition)operator(\)) - operator({) - operator(;) comment(// ...) - operator(}) -operator(}) - -comment(// @@PLEAC@@_10.3) -comment(// Local scopes are not created in the same way as in Perl [by simply enclosing) -comment(// within braces]: only via the creation of functions are local scopes created) - -comment(// Doesn't create a local scope; '$myvariable' is created at top-level) -operator({) - local_variable($myvariable) operator(=) integer(7)operator(;) -operator(}) - -comment(// '$myvariable' is accessable here) -predefined(echo) local_variable($myvariable) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// ------------) - -operator({) - local_variable($counter) operator(=) integer(0)operator(;) - - comment(// Local scope created within function, but not within surrounding braces) - comment(// so:) - comment(// * '$counter' is actually a top-level variable, so globally accessable) - comment(// * 'next_counter' has no implict access to '$counter'; must be granted) - comment(// via 'global' keyword) - - reserved(function) function(next_counter)operator(()operator(\)) operator({) reserved(global) local_variable($counter)operator(;) local_variable($counter)operator(++)operator(;) operator(}) -operator(}) - -comment(// ----------------------------) - -comment(// PHP doesn't, AFAIK, offer an equivalent to Perl's BEGIN block. Similar) -comment(// behaviour may be obtained by defining a class, and including such code) -comment(// in its constructor) - -reserved(class) class(BEGIN) -operator({) - reserved(private) local_variable($myvariable)operator(;) - - reserved(function) function(__construct)operator(()operator(\)) - operator({) - local_variable($this)operator(->)ident(myvariable) operator(=) integer(5)operator(;) - operator(}) - - reserved(function) function(othersub)operator(()operator(\)) - operator({) - predefined(echo) local_variable($this)operator(->)ident(myvariable) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - operator(}) -operator(}) - -comment(// ------------) - -local_variable($b) operator(=) reserved(new) constant(BEGIN)operator(()operator(\))operator(;) - -local_variable($b)operator(->)ident(othersub)operator(()operator(\))operator(;) - -comment(// ----------------------------) - -comment(// PHP, like C, supports 'static' local variables, that is, those that upon) -comment(// first access are initialised, and thence retain their value between function) -comment(// calls. However, the 'counter' example is better implemented as a class) - -reserved(class) class(Counter) -operator({) - reserved(private) local_variable($counter)operator(;) - - reserved(function) function(__construct)operator(()local_variable($counter_init)operator(\)) - operator({) - local_variable($this)operator(->)ident(counter) operator(=) local_variable($counter_init)operator(;) - operator(}) - - reserved(function) function(next_counter)operator(()operator(\)) operator({) local_variable($this)operator(->)ident(counter)operator(++)operator(;) reserved(return) local_variable($this)operator(->)ident(counter)operator(;) operator(}) - reserved(function) function(prev_counter)operator(()operator(\)) operator({) local_variable($this)operator(->)ident(counter)operator(;) reserved(return) local_variable($this)operator(->)ident(counter)operator(;) operator(}) -operator(}) - -comment(// ------------) - -local_variable($counter) operator(=) reserved(new) constant(Counter)operator(()integer(42)operator(\))operator(;) -predefined(echo) local_variable($counter)operator(->)ident(next_counter)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) local_variable($counter)operator(->)ident(next_counter)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -predefined(echo) local_variable($counter)operator(->)ident(prev_counter)operator(()operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// @@PLEAC@@_10.4) -comment(// AFAICT there is no means of obtaining the name of the currently executing) -comment(// function, or, for that matter, perform any stack / activation record,) -comment(// inspection. It *is* possible to:) -comment(//) -comment(// * Obtain a list of the currently-defined functions ['get_defined_functions']) -comment(// * Check whether a specific function exists ['function_exists']) -comment(// * Use the 'Reflection API') -comment(//) -comment(// So, to solve this problem would seem to require adopting a convention where) -comment(// a string representing the function name is passed as an argument, or a local) -comment(// variable [perhaps called, '$name'] is so set [contrived, and of limited use]) - -reserved(function) function(whoami)operator(()operator(\)) -operator({) - local_variable($name) operator(=) string<delimiter(')content(whoami)delimiter(')>operator(;) - predefined(echo) string<delimiter(")content(I am: )inline<delimiter({)local_variable($name)delimiter(})>char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -ident(whoami)operator(()operator(\))operator(;) - -comment(// @@PLEAC@@_10.5) -comment(// In PHP all items exist as 'memory references' [i.e. non-modifiable pointers],) -comment(// so when passing an item as a function argument, or returning an item from) -comment(// a function, it is this 'memory reference' that is passed, and *not* the) -comment(// contents of that item. Should several references to an item exist [e.g. if) -comment(// passed to a function then at least two such references would exist in) -comment(// different scopes] they would all be refering to the same copy of the item.) -comment(// However, if an attempt is made to alter the item is made, a copy is made) -comment(// and it is the copy that is altered, leaving the original intact.) -comment(// ) -comment(// The PHP reference mechanism is used to selectively prevent this behaviour,) -comment(// and ensure that if a change is made to an item that no copy is made, and that) -comment(// it is the original item that is changed. Importantly, there is no efficiency) -comment(// gain from passing function parameters using references if the parameter item) -comment(// is not altered.) - -comment(// A copy of the item referred to by, '$arr', is made, and altered; original) -comment(// remains intact) -reserved(function) function(array_by_value)operator(()local_variable($arr)operator(\)) -operator({) - local_variable($arr)operator([)integer(0)operator(]) operator(=) integer(7)operator(;) - predefined(echo) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// No copy is made; original item referred to by, '$arr', is altered) -reserved(function) function(array_by_ref)operator(()operator(&)local_variable($arr)operator(\)) -operator({) - local_variable($arr)operator([)integer(0)operator(]) operator(=) integer(7)operator(;) - predefined(echo) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// ------------) - -local_variable($arr) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(\))operator(;) - -predefined(echo) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// output: 1 ) -ident(array_by_value)operator(()local_variable($arr)operator(\))operator(;) comment(// output: 7) -predefined(echo) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// output: 1 ) - -local_variable($arr) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(\))operator(;) - -predefined(echo) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// output: 1 ) -ident(array_by_ref)operator(()local_variable($arr)operator(\))operator(;) comment(// output: 7) -predefined(echo) local_variable($arr)operator([)integer(0)operator(]) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) comment(// output: 7 ) - -comment(// ----------------------------) - -comment(// Since, 'add_vecpair', does not attempt to alter either, '$x' or '$y', it makes) -comment(// no difference whether they are 'passed by value', or 'passed by reference') -reserved(function) function(add_vecpair)operator(()local_variable($x)operator(,) local_variable($y)operator(\)) -operator({) - local_variable($r) operator(=) predefined(array)operator(()operator(\))operator(;) - local_variable($length) operator(=) predefined(count)operator(()local_variable($x)operator(\))operator(;) - reserved(for)operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) local_variable($length)operator(;) local_variable($i)operator(++)operator(\)) local_variable($r)operator([)local_variable($i)operator(]) operator(=) local_variable($x)operator([)local_variable($i)operator(]) operator(+) local_variable($y)operator([)local_variable($i)operator(])operator(;) - reserved(return) local_variable($r)operator(;) -operator(}) - -comment(// ...) -predefined(count)operator(()local_variable($arr1)operator(\)) operator(==) predefined(count)operator(()local_variable($arr2)operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(usage: add_vecpair ARR1 ARR2)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// 'passed by value') -local_variable($arr3) operator(=) ident(add_vecpair)operator(()local_variable($arr1)operator(,) local_variable($arr2)operator(\))operator(;) - -comment(// 'passed by reference' [also possible to override default 'passed by value') -comment(// if required]) -local_variable($arr3) operator(=) ident(add_vecpair)operator(()operator(&)local_variable($arr1)operator(,) operator(&)local_variable($arr2)operator(\))operator(;) - -comment(// @@PLEAC@@_10.6) -comment(// PHP can be described as a dynamically typed language because variables serve) -comment(// as identifiers, and the same variable may refer to data of various types.) -comment(// As such, the set of arguments passed to a function may vary in type between) -comment(// calls, as can the type of return value. Where this is likely to occur type) -comment(// checking should be performed either / both within the function body, and) -comment(// when obtaining it's return value. As for Perl-style 'return context', I) -comment(// don't believe it is supported by PHP) - -comment(// Can return any type) -reserved(function) function(mysub)operator(()operator(\)) -operator({) - comment(// ...) - reserved(return) integer(5)operator(;) - comment(// ...) - reserved(return) predefined(array)operator(()integer(5)operator(\))operator(;) - comment(// ...) - reserved(return) string<delimiter(')content(5)delimiter(')>operator(;) -operator(}) - -comment(// Throw away return type [i.e. returns a 'void' type ?]) -ident(mysub)operator(()operator(\))operator(;) - -comment(// Check return type. Can do via:) -comment(// * gettype($var\)) -comment(// * is_xxx e.g. is_array($var\), is_muneric($var\), ...) -local_variable($ret) operator(=) ident(mysub)operator(()operator(\))operator(;) - -reserved(if) operator(()predefined(is_numeric)operator(()local_variable($ret)operator(\))operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(if) operator(()predefined(is_array)operator(()local_variable($ret)operator(\))operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(if) operator(()predefined(is_string)operator(()local_variable($ret)operator(\))operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// @@PLEAC@@_10.7) -comment(// PHP doesn't directly support named / keyword parameters, but these can be) -comment(// easily mimiced using a class of key / value pairs, and passing a variable) -comment(// number of arguments) - -reserved(class) class(KeyedValue) -operator({) - reserved(public) local_variable($key)operator(,) local_variable($value)operator(;) - reserved(public) reserved(function) function(__construct)operator(()local_variable($key)operator(,) local_variable($value)operator(\)) operator({) local_variable($this)operator(->)predefined(key) operator(=) local_variable($key)operator(;) local_variable($this)operator(->)ident(value) operator(=) local_variable($value)operator(;) operator(}) -operator(}) - -reserved(function) function(the_func)operator(()operator(\)) -operator({) - reserved(foreach) operator(()predefined(func_get_args)operator(()operator(\)) reserved(as) local_variable($arg)operator(\)) - operator({) - predefined(printf)operator(()string<delimiter(")content(Key: %10s|Value:%10s)char(\\n)delimiter(")>operator(,) local_variable($arg)operator(->)predefined(key)operator(,) local_variable($arg)operator(->)ident(value)operator(\))operator(;) - operator(}) -operator(}) - -comment(// ----) - -ident(the_func)operator(()reserved(new) constant(KeyedValue)operator(()string<delimiter(')content(name)delimiter(')>operator(,) string<delimiter(')content(Bob)delimiter(')>operator(\))operator(,) - reserved(new) constant(KeyedValue)operator(()string<delimiter(')content(age)delimiter(')>operator(,) integer(36)operator(\))operator(,) - reserved(new) constant(KeyedValue)operator(()string<delimiter(')content(income)delimiter(')>operator(,) integer(51000)operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Alternatively, an associative array of key / value pairs may be constructed.) -comment(// With the aid of the 'extract' built-in function, the key part of this array) -comment(// may be intsntiated to a variable name, thus more closely approximating the) -comment(// behaviour of true named parameters) - -reserved(function) function(the_func)operator(()local_variable($var_array)operator(\)) -operator({) - predefined(extract)operator(()local_variable($var_array)operator(\))operator(;) - - reserved(if) operator(()predefined(isset)operator(()local_variable($name)operator(\))operator(\)) predefined(printf)operator(()string<delimiter(")content(Name: %s)char(\\n)delimiter(")>operator(,) local_variable($name)operator(\))operator(;) - reserved(if) operator(()predefined(isset)operator(()local_variable($age)operator(\))operator(\)) predefined(printf)operator(()string<delimiter(")content(Age: %s)char(\\n)delimiter(")>operator(,) local_variable($age)operator(\))operator(;) - reserved(if) operator(()predefined(isset)operator(()local_variable($income)operator(\))operator(\)) predefined(printf)operator(()string<delimiter(")content(Income: %s)char(\\n)delimiter(")>operator(,) local_variable($income)operator(\))operator(;) -operator(}) - -comment(// ----) - -ident(the_func)operator(()predefined(array)operator(()string<delimiter(')content(name)delimiter(')> operator(=>) string<delimiter(')content(Bob)delimiter(')>operator(,) string<delimiter(')content(age)delimiter(')> operator(=>) integer(36)operator(,) string<delimiter(')content(income)delimiter(')> operator(=>) integer(51000)operator(\))operator(\))operator(;) - -comment(// ----------------------------) - -reserved(class) class(RaceTime) -operator({) - reserved(public) local_variable($time)operator(,) local_variable($dim)operator(;) - reserved(public) reserved(function) function(__construct)operator(()local_variable($time)operator(,) local_variable($dim)operator(\)) operator({) local_variable($this)operator(->)predefined(time) operator(=) local_variable($time)operator(;) local_variable($this)operator(->)ident(dim) operator(=) local_variable($dim)operator(;) operator(}) -operator(}) - -reserved(function) function(the_func)operator(()local_variable($var_array)operator(\)) -operator({) - predefined(extract)operator(()local_variable($var_array)operator(\))operator(;) - - reserved(if) operator(()predefined(isset)operator(()local_variable($start_time)operator(\))operator(\)) predefined(printf)operator(()string<delimiter(")content(start: %d - %s)char(\\n)delimiter(")>operator(,) local_variable($start_time)operator(->)predefined(time)operator(,) local_variable($start_time)operator(->)ident(dim)operator(\))operator(;) - reserved(if) operator(()predefined(isset)operator(()local_variable($finish_time)operator(\))operator(\)) predefined(printf)operator(()string<delimiter(")content(finish: %d - %s)char(\\n)delimiter(")>operator(,) local_variable($finish_time)operator(->)predefined(time)operator(,) local_variable($finish_time)operator(->)ident(dim)operator(\))operator(;) - reserved(if) operator(()predefined(isset)operator(()local_variable($incr_time)operator(\))operator(\)) predefined(printf)operator(()string<delimiter(")content(incr: %d - %s)char(\\n)delimiter(")>operator(,) local_variable($incr_time)operator(->)predefined(time)operator(,) local_variable($incr_time)operator(->)ident(dim)operator(\))operator(;) -operator(}) - -comment(// ----) - -ident(the_func)operator(()predefined(array)operator(()string<delimiter(')content(start_time)delimiter(')> operator(=>) reserved(new) constant(RaceTime)operator(()integer(20)operator(,) string<delimiter(')content(s)delimiter(')>operator(\))operator(,) - string<delimiter(')content(finish_time)delimiter(')> operator(=>) reserved(new) constant(RaceTime)operator(()integer(5)operator(,) string<delimiter(')content(m)delimiter(')>operator(\))operator(,) - string<delimiter(')content(incr_time)delimiter(')> operator(=>) reserved(new) constant(RaceTime)operator(()integer(3)operator(,) string<delimiter(')content(m)delimiter(')>operator(\))operator(\))operator(\))operator(;) - -ident(the_func)operator(()predefined(array)operator(()string<delimiter(')content(start_time)delimiter(')> operator(=>) reserved(new) constant(RaceTime)operator(()integer(5)operator(,) string<delimiter(')content(m)delimiter(')>operator(\))operator(,) - string<delimiter(')content(finish_time)delimiter(')> operator(=>) reserved(new) constant(RaceTime)operator(()integer(30)operator(,) string<delimiter(')content(m)delimiter(')>operator(\))operator(\))operator(\))operator(;) - -ident(the_func)operator(()predefined(array)operator(()string<delimiter(')content(start_time)delimiter(')> operator(=>) reserved(new) constant(RaceTime)operator(()integer(30)operator(,) string<delimiter(')content(m)delimiter(')>operator(\))operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_10.8) -comment(// The 'list' keyword [looks like a function but is actually a special language) -comment(// construct] may be used to perform multiple assignments from a numerically) -comment(// indexed array of values, and offers the added bonus of being able to skip) -comment(// assignment of one, or more, of those values) - -reserved(function) function(func)operator(()operator(\)) operator({) reserved(return) predefined(array)operator(()integer(3)operator(,) integer(6)operator(,) integer(9)operator(\))operator(;) operator(}) - -comment(// ------------) - -predefined(list)operator(()local_variable($a)operator(,) local_variable($b)operator(,) local_variable($c)operator(\)) operator(=) predefined(array)operator(()integer(6)operator(,) integer(7)operator(,) integer(8)operator(\))operator(;) - -comment(// Provided 'func' returns an numerically-indexed array, the following) -comment(// multiple assignment will work) -predefined(list)operator(()local_variable($a)operator(,) local_variable($b)operator(,) local_variable($c)operator(\)) operator(=) ident(func)operator(()operator(\))operator(;) - -comment(// Any existing variables no longer wanted would need to be 'unset') -predefined(unset)operator(()local_variable($b)operator(\))operator(;) - -comment(// As above, but second element of return array discarded) -predefined(list)operator(()local_variable($a)operator(,)operator(,)local_variable($c)operator(\)) operator(=) ident(func)operator(()operator(\))operator(;) - -comment(// ----------------------------) - -comment(// Care needed to ensure returned array is numerically-indexed) -predefined(list)operator(()local_variable($dev)operator(,) local_variable($ino)operator(,)operator(,)operator(,)local_variable($uid)operator(\)) operator(=) predefined(array_slice)operator(()predefined(array_values)operator(()predefined(stat)operator(()local_variable($filename)operator(\))operator(\))operator(,) integer(0)operator(,) integer(13)operator(\))operator(;) - -comment(// @@PLEAC@@_10.9) -comment(// Multiple return values are possible via packing a set of values within a) -comment(// numerically-indexed array and using 'list' to extract them) - -reserved(function) function(some_func)operator(()operator(\)) operator({) reserved(return) predefined(array)operator(()predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(\))operator(,) predefined(array)operator(()string<delimiter(')content(a)delimiter(')> operator(=>) integer(1)operator(,) string<delimiter(')content(b)delimiter(')> operator(=>) integer(2)operator(\))operator(\))operator(;) operator(}) - -comment(// ------------) - -predefined(list)operator(()local_variable($arr)operator(,) local_variable($hash)operator(\)) operator(=) ident(some_func)operator(()operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(some_func)operator(()operator(&)local_variable($arr)operator(,) operator(&)local_variable($hash)operator(\)) operator({) reserved(return) predefined(array)operator(()local_variable($arr)operator(,) local_variable($hash)operator(\))operator(;) operator(}) - -comment(// ------------) - -local_variable($arrin) operator(=) predefined(array)operator(()integer(1)operator(,) integer(2)operator(,) integer(3)operator(\))operator(;) local_variable($hashin) operator(=) predefined(array)operator(()string<delimiter(')content(a)delimiter(')> operator(=>) integer(1)operator(,) string<delimiter(')content(b)delimiter(')> operator(=>) integer(2)operator(\))operator(;) - -predefined(list)operator(()local_variable($arr)operator(,) local_variable($hash)operator(\)) operator(=) ident(some_func)operator(()local_variable($arrin)operator(,) local_variable($hashin)operator(\))operator(;) - -comment(// @@PLEAC@@_10.10) -comment(// AFAICT, most of the PHP library functions are designed to return some required ) -comment(// value on success, and FALSE on exit. Whilst it is possible to return NULL, or) -comment(// one of the recognised 'empty' values [e.g. '' or 0 or an empty array etc],) -comment(// FALSE actually seems to be the preferred means of indicating failure) - -reserved(function) function(a_func)operator(()operator(\)) operator({) reserved(return) pre_constant(FALSE)operator(;) operator(}) - -ident(a_func)operator(()operator(\)) operator(||) predefined(die)operator(()string<delimiter(")content(Function failed)char(\\n)delimiter(")>operator(\))operator(;) - -reserved(if) operator(()operator(!)ident(a_func)operator(()operator(\))operator(\)) predefined(die)operator(()string<delimiter(")content(Function failed)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// @@PLEAC@@_10.11) -comment(// Whether PHP is seen to support prototyping depends on the accepted) -comment(// definition of this term:) -comment(//) -comment(// * Prototyping along the lines used in Ada, Modula X, and even C / C++,) -comment(// in which a function's interface is declared separately from its) -comment(// implementation, is *not* supported) -comment(//) -comment(// * Prototyping in which, as part of the function definition, parameter) -comment(// information must be supplied. In PHP a function definition neither) -comment(// parameter, nor return type, information needs to be supplied, though) -comment(// it is usual to see a parameter list supplied [indicates the number,) -comment(// positional order, and optionally, whether a parameter is passed by) -comment(// reference; no type information is present]. In short, prototyping in) -comment(// PHP is optional, and limited) - -reserved(function) function(func_with_one_arg)operator(()local_variable($arg1)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(function) function(func_with_two_arg)operator(()local_variable($arg1)operator(,) local_variable($arg2)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -reserved(function) function(func_with_three_arg)operator(()local_variable($arg1)operator(,) local_variable($arg2)operator(,) local_variable($arg3)operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// The following may be interpreted as meaning a function accepting no) -comment(// arguments:) -reserved(function) function(func_with_no_arg)operator(()operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// whilst the following may mean a function taking zero or more arguments) -reserved(function) function(func_with_no_arg_information)operator(()operator(\)) -operator({) - operator(;) comment(// ...) -operator(}) - -comment(// @@PLEAC@@_10.12) -comment(// Unlike in Perl, PHP's 'die' [actually an alias for 'exit'] doesn't throw) -comment(// an exception, but instead terminates the script, optionally either) -comment(// returning an integer value to the operating system, or printing a message.) -comment(// So, the following, does not exhibit the same behaviour as the Perl example) - -predefined(die)operator(()string<delimiter(")content(some message)char(\\n)delimiter(")>operator(\))operator(;) - -comment(// Instead, like so many modern languages, PHP implements exception handling) -comment(// via the 'catch' and 'throw' keywords. Furthermore, a C++ or Java programmer) -comment(// would find PHP's exception handling facility remarkably similar to those) -comment(// of their respective languages. A simple, canonical example follows:) - -comment(// Usual to derive new exception classes from the built-in, 'Exception',) -comment(// class) -reserved(class) class(MyException) reserved(extends) pre_constant(Exception) -operator({) - comment(// ...) -operator(}) - -comment(// ...) - -reserved(try) -operator({) - comment(// ...) - reserved(if) operator(()local_variable($some_problem_detected)operator(\)) reserved(throw) reserved(new) constant(MyException)operator(()string<delimiter(')content(some message)delimiter(')>operator(,) local_variable($some_error_code)operator(\))operator(;) - comment(// ..) -operator(}) - -reserved(catch) operator(()constant(MyException) local_variable($e)operator(\)) -operator({) - operator(;) comment(// ... handle the problem ...) -operator(}) - -comment(// ----------------------------) - -reserved(class) class(FullMoonException) reserved(extends) pre_constant(Exception) -operator({) - comment(// ...) -operator(}) - -comment(// ...) - -reserved(try) -operator({) - comment(// ...) - reserved(if) operator(()local_variable($some_problem_detected)operator(\)) reserved(throw) reserved(new) constant(FullMoonException)operator(()string<delimiter(')content(...)delimiter(')>operator(,) local_variable($full_moon_error_code)operator(\))operator(;) - comment(// ..) -operator(}) - -reserved(catch) operator(()constant(FullMoonException) local_variable($e)operator(\)) -operator({) - comment(// ... rethrow the exception - will propagate to higher level ...) - reserved(throw) local_variable($e)operator(;) -operator(}) - -comment(// @@PLEAC@@_10.13) -comment(// Please refer to discussion about PHP scope in section two of this chapter.) -comment(// Briefly, PHP assumes a variable name within a function to be local unless) -comment(// it has been specifically declared with the, 'global', keyword, in which) -comment(// case it refers to a variable in the 'superglobal' array, '$GLOBALS'. Thus,) -comment(// inadvertant variable name shadowing cannot occur since it is it not possible ) -comment(// to use the same name to refer to both a local and a global variable. If) -comment(// accessing a global variable care should be taken to not accidentally update) -comment(// it. The techniques used in this section are simply not required.) - -comment(// *** NOT TRANSLATED ***) - -comment(// @@PLEAC@@_10.14) -comment(// In PHP once a function has been defined it remains defined. In other words,) -comment(// it cannot be undefined / deleted, nor can that particular function name be) -comment(// reused to reference another function body. Even the lambda-like functions) -comment(// created via the 'create_function' built-in, cannot be undefined [they exist) -comment(// until script termination, thus creating too many of these can actually) -comment(// exhaust memory !]. However, since the latter can be assigned to variables,) -comment(// the same variable name can be used to reference difference functions [and) -comment(// when this is done the reference to the previous function is lost (unless) -comment(// deliberately saved\), though the function itself continues to exist].) -comment(//) -comment(// If, however, all that is needed is a simple function aliasing facility,) -comment(// then just assign the function name to a variable, and execute using the) -comment(// variable name) - -comment(// Original function) -reserved(function) function(expand)operator(()operator(\)) operator({) predefined(echo) string<delimiter(")content(expand)char(\\n)delimiter(")>operator(;) operator(}) - -comment(// Prove that function exists) -predefined(echo) operator(()predefined(function_exists)operator(()string<delimiter(')content(expand)delimiter(')>operator(\)) operator(?) string<delimiter(')content(yes)delimiter(')> operator(:) string<delimiter(')content(no)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// Use a variable to alias it) -local_variable($grow) operator(=) string<delimiter(')content(expand)delimiter(')>operator(;) - -comment(// Call function via original name, and variable, respectively) -ident(expand)operator(()operator(\))operator(;) - -local_variable($grow)operator(()operator(\))operator(;) - -comment(// Remove alias variable) -predefined(unset)operator(()local_variable($grow)operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(fred)operator(()operator(\)) operator({) predefined(echo) string<delimiter(")content(fred)char(\\n)delimiter(")>operator(;) operator(}) - -local_variable($barney) operator(=) string<delimiter(')content(fred)delimiter(')>operator(;) - -local_variable($barney)operator(()operator(\))operator(;) - -predefined(unset)operator(()local_variable($barney)operator(\))operator(;) - -ident(fred)operator(()operator(\))operator(;) - -comment(// ------------) - -local_variable($fred) operator(=) predefined(create_function)operator(()string<delimiter(')delimiter(')>operator(,) string<delimiter(')content(echo "fred)content(\\n)content(";)delimiter(')>operator(\))operator(;) - -local_variable($barney) operator(=) local_variable($fred)operator(;) - -local_variable($barney)operator(()operator(\))operator(;) - -predefined(unset)operator(()local_variable($barney)operator(\))operator(;) - -local_variable($fred)operator(()operator(\))operator(;) - -comment(// ----------------------------) - -reserved(function) function(red)operator(()local_variable($text)operator(\)) operator({) reserved(return) string<delimiter(")content(<FONT COLOR='red'>)local_variable($text)content(</FONT>)delimiter(")>operator(;) operator(}) - -predefined(echo) ident(red)operator(()string<delimiter(')content(careful here)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -comment(// ------------) - -local_variable($colour) operator(=) string<delimiter(')content(red)delimiter(')>operator(;) - -operator($)local_variable($colour) operator(=) predefined(create_function)operator(()string<delimiter(')content($text)delimiter(')>operator(,) string<delimiter(')content(global $colour; -return "<FONT COLOR=)char(\\')content($colour)char(\\')content(>$text</FONT>";)delimiter(')>operator(\))operator(;) - -predefined(echo) operator($)local_variable($colour)operator(()string<delimiter(')content(careful here)delimiter(')>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - -predefined(unset)operator(()operator($)local_variable($colour)operator(\))operator(;) - -comment(// ----) - -local_variable($colours) operator(=) predefined(split)operator(()string<delimiter(')content( )delimiter(')>operator(,) string<delimiter(')content(red blue green yellow orange purple violet)delimiter(')>operator(\))operator(;) - -reserved(foreach) operator(()local_variable($colours) reserved(as) local_variable($colour)operator(\)) -operator({) - operator($)local_variable($colour) operator(=) predefined(create_function)operator(()string<delimiter(')content($text)delimiter(')>operator(,) string<delimiter(')content(global $colour; - return "<FONT COLOR=)char(\\')content($colour)char(\\')content(>$text</FONT>";)delimiter(')>operator(\))operator(;) -operator(}) - -reserved(foreach) operator(()local_variable($colours) reserved(as) local_variable($colour)operator(\)) operator({) predefined(echo) operator($)local_variable($colour)operator(()string<delimiter(")content(Careful with this )local_variable($colour)content(, James)delimiter(")>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) operator(}) - -reserved(foreach) operator(()local_variable($colours) reserved(as) local_variable($colour)operator(\)) operator({) predefined(unset)operator(()operator($)local_variable($colour)operator(\))operator(;) operator(}) - -comment(// @@PLEAC@@_10.15) -comment(// PHP sports an AUTOLOAD facility that is quite easy to use, but, AFAICT, is geared) -comment(// towards the detection of unavailable classes rather than for individual functions.) -comment(// Here is a rudimentary example:) - -reserved(function) function(__autoload)operator(()local_variable($classname)operator(\)) -operator({) - reserved(if) operator(()operator(!)predefined(file_exists)operator(()local_variable($classname)operator(\))operator(\)) - operator({) - comment(// Class file does not exist, so handle situation; in this case,) - comment(// issue error message, and exit program) - predefined(die)operator(()string<delimiter(")content(File for class: )inline<delimiter({)local_variable($classname)delimiter(})>content( not found - aborting)char(\\n)delimiter(")>operator(\))operator(;) - operator(}) - reserved(else) - operator({) - comment(// Class file exists, so load it) - predefined(require_once) local_variable($classname)operator(;) - operator(}) -operator(}) - -comment(// ------------) - -comment(// Attempt to instantiate object of undefined class ) -reserved(new) constant(UnknownClassObject)operator(()operator(\))operator(;) - -comment(// Execution continues here if class exists) -comment(// ...) - -comment(// ----------------------------) - -comment(// It is also possible to perform [quite extensive] introspection on functions,) -comment(// variables etc, so it is possible to check whether a function exists before) -comment(// executing it, thus allowing a non-existent functions to be searched for and) -comment(// loaded from a source file, or perhaps dynamically defined. An example of what) -comment(// could be described as a custom autoload facility appears below.) - -local_variable($colours) operator(=) predefined(array)operator(()string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(blue)delimiter(')>operator(,) string<delimiter(')content(green)delimiter(')>operator(,) string<delimiter(')content(yellow)delimiter(')>operator(,) string<delimiter(')content(orange)delimiter(')>operator(,) string<delimiter(')content(purple)delimiter(')>operator(,) string<delimiter(')content(violet)delimiter(')>operator(\))operator(;) - -reserved(foreach) operator(()local_variable($colours) reserved(as) local_variable($colour)operator(\)) -operator({) - operator($)local_variable($colour) operator(=) predefined(create_function)operator(()string<delimiter(')content($text)delimiter(')>operator(,) string<delimiter(')content(global $colour; - return "<FONT COLOR=)char(\\')content($colour)char(\\')content(>$text</FONT>";)delimiter(')>operator(\))operator(;) -operator(}) - -comment(// Let's add a new colour to the list) -predefined(array_push)operator(()local_variable($colours)operator(,) string<delimiter(')content(chartreuse)delimiter(')>operator(\))operator(;) - -reserved(foreach) operator(()local_variable($colours) reserved(as) local_variable($colour)operator(\)) -operator({) - comment(// Checking whether function is defined) - reserved(if) operator(()operator(!)predefined(function_exists)operator(()operator($)local_variable($colour)operator(\))operator(\)) - operator({) - comment(// Doesn't exist, so dynamically define it) - operator($)local_variable($colour) operator(=) predefined(create_function)operator(()string<delimiter(')content($text)delimiter(')>operator(,) string<delimiter(')content(global $colour; - return "<FONT COLOR=)char(\\')content($colour)char(\\')content(>$text</FONT>";)delimiter(')>operator(\))operator(;) - - comment(// Alternatively, if it exists in a source file, 'include' the file:) - comment(// include 'newcolours.php') - operator(}) - - predefined(echo) operator($)local_variable($colour)operator(()string<delimiter(")content(Careful with this )local_variable($colour)content(, James)delimiter(")>operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) -operator(}) - -reserved(foreach) operator(()local_variable($colours) reserved(as) local_variable($colour)operator(\)) predefined(unset)operator(()operator($)local_variable($colour)operator(\))operator(;) - -comment(// @@PLEAC@@_10.16) -comment(// *** Warning *** Whilst PHP *does* allow functions to be defined within other) -comment(// functions it needs to be clearly understood that these 'inner' functions:) -comment(// * Do not exist until the outer function is called a first time, at which time) -comment(// they then remain defined) -comment(// * Are global in scope, so are accessable outside the function by their name;) -comment(// the fact that they are nested within another function has, AFAICT, no bearing) -comment(// on name resolution) -comment(// * Do not form a closure: the inner function is merely 'parked' within the) -comment(// outer function, and has no implicit access to the outer function's variables) -comment(// or other inner functions) - -reserved(function) function(outer)operator(()local_variable($arg)operator(\)) -operator({) - local_variable($x) operator(=) local_variable($arg) operator(+) integer(35)operator(;) - reserved(function) function(inner)operator(()operator(\)) operator({) reserved(return) local_variable($x) operator(*) integer(19)operator(;) operator(}) - - comment(// *** wrong *** 'inner' returns 0 * 19, not ($arg + 35\) * 19) - reserved(return) local_variable($x) operator(+) ident(inner)operator(()operator(\))operator(;) -operator(}) - -comment(// ----------------------------) - -reserved(function) function(outer)operator(()local_variable($arg)operator(\)) -operator({) - local_variable($x) operator(=) local_variable($arg) operator(+) integer(35)operator(;) - - comment(// No implicit access to outer function scope; any required data must be) - comment(// explicity passed) - reserved(function) function(inner)operator(()local_variable($x)operator(\)) operator({) reserved(return) local_variable($x) operator(*) integer(19)operator(;) operator(}) - - reserved(return) local_variable($x) operator(+) ident(inner)operator(()local_variable($x)operator(\))operator(;) -operator(}) - -comment(// ------------ ) - -comment(// Equivalent to previously-shown code) -reserved(function) function(inner)operator(()local_variable($x)operator(\)) -operator({) - reserved(return) local_variable($x) operator(*) integer(19)operator(;) -operator(}) - -reserved(function) function(outer)operator(()local_variable($arg)operator(\)) -operator({) - local_variable($x) operator(=) local_variable($arg) operator(+) integer(35)operator(;) - reserved(return) local_variable($x) operator(+) ident(inner)operator(()local_variable($x)operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_10.17) -comment(// @@INCOMPLETE@@) -comment(// @@INCOMPLETE@@) - -comment(// @@PLEAC@@_16.1) -comment(// Run a command and return its results as a string.) -local_variable($output_string) operator(=) predefined(shell_exec)operator(()string<delimiter(')content(program args)delimiter(')>operator(\))operator(;) - -comment(// Same as above, using backtick operator.) -local_variable($output_string) operator(=) string<delimiter(`)content(program args)delimiter(`)>operator(;) - -comment(// Run a command and return its results as a list of strings,) -comment(// one per line.) -local_variable($output_lines) operator(=) predefined(array)operator(()operator(\))operator(;) -predefined(exec)operator(()string<delimiter(')content(program args)delimiter(')>operator(,) local_variable($output_lines)operator(\))operator(;) - -comment(// -----------------------------) - -comment(// The only way to execute a program without using the shell is to) -comment(// use pcntl_exec(\). However, there is no way to do redirection, so) -comment(// you can't capture its output.) - -local_variable($pid) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) -reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) -operator(}) reserved(elseif) operator(()local_variable($pid)operator(\)) operator({) - predefined(pcntl_waitpid)operator(()local_variable($pid)operator(,) local_variable($status)operator(\))operator(;) -operator(}) reserved(else) operator({) - comment(// Note that pcntl_exec(\) automatically prepends the program name) - comment(// to the array of arguments; the program name cannot be spoofed.) - predefined(pcntl_exec)operator(()local_variable($program)operator(,) predefined(array)operator(()local_variable($arg1)operator(,) local_variable($arg2)operator(\))operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_16.2) -comment(// Run a simple command and retrieve its result code.) -predefined(exec)operator(()string<delimiter(")content(vi )local_variable($myfile)delimiter(")>operator(,) local_variable($output)operator(,) local_variable($result_code)operator(\))operator(;) - -comment(// -----------------------------) - -comment(// Use the shell to perform redirection.) -predefined(exec)operator(()string<delimiter(')content(cmd1 args | cmd2 | cmd3 >outfile)delimiter(')>operator(\))operator(;) -predefined(exec)operator(()string<delimiter(')content(cmd args <infile >outfile 2>errfile)delimiter(')>operator(\))operator(;) - -comment(// -----------------------------) - -comment(// Run a command, handling its result code or signal.) -local_variable($pid) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) -reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) -operator(}) reserved(elseif) operator(()local_variable($pid)operator(\)) operator({) - predefined(pcntl_waitpid)operator(()local_variable($pid)operator(,) local_variable($status)operator(\))operator(;) - reserved(if) operator(()predefined(pcntl_wifexited)operator(()local_variable($status)operator(\))operator(\)) operator({) - local_variable($status) operator(=) predefined(pcntl_wexitstatus)operator(()local_variable($status)operator(\))operator(;) - predefined(echo) string<delimiter(")content(program exited with status )local_variable($status)char(\\n)delimiter(")>operator(;) - operator(}) reserved(elseif) operator(()predefined(pcntl_wifsignaled)operator(()local_variable($status)operator(\))operator(\)) operator({) - local_variable($signal) operator(=) predefined(pcntl_wtermsig)operator(()local_variable($status)operator(\))operator(;) - predefined(echo) string<delimiter(")content(program killed by signal )local_variable($signal)char(\\n)delimiter(")>operator(;) - operator(}) reserved(elseif) operator(()predefined(pcntl_wifstopped)operator(()local_variable($status)operator(\))operator(\)) operator({) - local_variable($signal) operator(=) predefined(pcntl_wstopsig)operator(()local_variable($status)operator(\))operator(;) - predefined(echo) string<delimiter(")content(program stopped by signal )local_variable($signal)char(\\n)delimiter(")>operator(;) - operator(}) -operator(}) reserved(else) operator({) - predefined(pcntl_exec)operator(()local_variable($program)operator(,) local_variable($args)operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -comment(// Run a command while blocking interrupt signals.) -local_variable($pid) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) -reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) -operator(}) reserved(elseif) operator(()local_variable($pid)operator(\)) operator({) - comment(// parent catches INT and berates user) - reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) - reserved(function) function(handle_sigint)operator(()local_variable($signal)operator(\)) operator({) - predefined(echo) string<delimiter(")content(Tsk tsk, no process interruptus)char(\\n)delimiter(")>operator(;) - operator(}) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(handle_sigint)delimiter(')>operator(\))operator(;) - reserved(while) operator(()operator(!)predefined(pcntl_waitpid)operator(()local_variable($pid)operator(,) local_variable($status)operator(,) constant(WNOHANG)operator(\))operator(\)) operator({)operator(}) -operator(}) reserved(else) operator({) - comment(// child ignores INT and does its thing) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) constant(SIG_IGN)operator(\))operator(;) - predefined(pcntl_exec)operator(()string<delimiter(')content(/bin/sleep)delimiter(')>operator(,) predefined(array)operator(()string<delimiter(')content(10)delimiter(')>operator(\))operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -comment(// Since there is no direct access to execv(\) and friends, and) -comment(// pcntl_exec(\) won't let us supply an alternate program name) -comment(// in the argument list, there is no way to run a command with) -comment(// a different name in the process table.) - -comment(// @@PLEAC@@_16.3) -comment(// Transfer control to the shell to run another program.) -predefined(pcntl_exec)operator(()string<delimiter(')content(/bin/sh)delimiter(')>operator(,) predefined(array)operator(()string<delimiter(')content(-c)delimiter(')>operator(,) string<delimiter(')content(archive *.data)delimiter(')>operator(\))operator(\))operator(;) -comment(// Transfer control directly to another program.) -predefined(pcntl_exec)operator(()string<delimiter(')content(/path/to/archive)delimiter(')>operator(,) predefined(array)operator(()string<delimiter(')content(accounting.data)delimiter(')>operator(\))operator(\))operator(;) - -comment(// @@PLEAC@@_16.4) -comment(// Handle each line in the output of a process.) -local_variable($readme) operator(=) predefined(popen)operator(()string<delimiter(')content(program arguments)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(;) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($readme)operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($readme)operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(===) pre_constant(false)operator(\)) reserved(break)operator(;) - comment(// ...) -operator(}) -predefined(pclose)operator(()local_variable($readme)operator(\))operator(;) - -comment(// -----------------------------) - -comment(// Write to the input of a process.) -local_variable($writeme) operator(=) predefined(popen)operator(()string<delimiter(')content(program arguments)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(;) -predefined(fwrite)operator(()local_variable($writeme)operator(,) string<delimiter(')content(data)delimiter(')>operator(\))operator(;) -predefined(pclose)operator(()local_variable($writeme)operator(\))operator(;) - -comment(// -----------------------------) - -comment(// Wait for a process to complete.) -local_variable($f) operator(=) predefined(popen)operator(()string<delimiter(')content(sleep 1000000)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(;) comment(// child goes to sleep) -predefined(pclose)operator(()local_variable($f)operator(\))operator(;) comment(// and parent goes to lala land) - -comment(// -----------------------------) - -local_variable($writeme) operator(=) predefined(popen)operator(()string<delimiter(')content(program arguments)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(;) -predefined(fwrite)operator(()local_variable($writeme)operator(,) string<delimiter(")content(hello)char(\\n)delimiter(")>operator(\))operator(;) comment(// program will get hello\\n on STDIN) -predefined(pclose)operator(()local_variable($writeme)operator(\))operator(;) comment(// program will get EOF on STDIN) - -comment(// -----------------------------) - -comment(// Output buffering callback that sends output to the pager.) -reserved(function) function(ob_pager)operator(()local_variable($output)operator(,) local_variable($mode)operator(\)) operator({) - reserved(static) local_variable($pipe)operator(;) - reserved(if) operator(()local_variable($mode) operator(&) pre_constant(PHP_OUTPUT_HANDLER_START)operator(\)) operator({) - local_variable($pager) operator(=) predefined(getenv)operator(()string<delimiter(')content(PAGER)delimiter(')>operator(\))operator(;) - reserved(if) operator(()operator(!)local_variable($pager)operator(\)) local_variable($pager) operator(=) string<delimiter(')content(/usr/bin/less)delimiter(')>operator(;) comment(// XXX: might not exist) - local_variable($pipe) operator(=) predefined(popen)operator(()local_variable($pager)operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(;) - operator(}) - predefined(fwrite)operator(()local_variable($pipe)operator(,) local_variable($output)operator(\))operator(;) - reserved(if) operator(()local_variable($mode) operator(&) pre_constant(PHP_OUTPUT_HANDLER_END)operator(\)) operator({) - predefined(pclose)operator(()local_variable($pipe)operator(\))operator(;) - operator(}) -operator(}) - -comment(// Redirect standard output to the pager.) -ident(ob_start)operator(()string<delimiter(')content(ob_pager)delimiter(')>operator(\))operator(;) - -comment(// Do something useful that writes to standard output, then) -comment(// close the output buffer.) -comment(// ...) -ident(ob_end_flush)operator(()operator(\))operator(;) - -comment(// @@PLEAC@@_16.5) -comment(// Output buffering: Only display a certain number of lines of output.) -reserved(class) class(Head) operator({) - reserved(function) function(Head)operator(()local_variable($lines)operator(=)integer(20)operator(\)) operator({) - local_variable($this)operator(->)ident(lines) operator(=) local_variable($lines)operator(;) - operator(}) - - reserved(function) function(filter)operator(()local_variable($output)operator(,) local_variable($mode)operator(\)) operator({) - local_variable($result) operator(=) predefined(array)operator(()operator(\))operator(;) - local_variable($newline) operator(=) string<delimiter(')delimiter(')>operator(;) - reserved(if) operator(()predefined(strlen)operator(()local_variable($output)operator(\)) operator(>) integer(0) operator(&&) local_variable($output)operator([)predefined(strlen)operator(()local_variable($output)operator(\)) operator(-) integer(1)operator(]) operator(==) string<delimiter(")char(\\n)delimiter(")>operator(\)) operator({) - local_variable($newline) operator(=) string<delimiter(")char(\\n)delimiter(")>operator(;) - local_variable($output) operator(=) predefined(substr)operator(()local_variable($output)operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(;) - operator(}) - reserved(foreach) operator(()predefined(explode)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) local_variable($output)operator(\)) reserved(as) local_variable($i) operator(=>) local_variable($line)operator(\)) operator({) - reserved(if) operator(()local_variable($this)operator(->)ident(lines) operator(>) integer(0)operator(\)) operator({) - local_variable($this)operator(->)ident(lines)operator(--)operator(;) - local_variable($result)operator([)operator(]) operator(=) local_variable($line)operator(;) - operator(}) - operator(}) - reserved(return) local_variable($result) operator(?) predefined(implode)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) local_variable($result)operator(\)) operator(.) local_variable($newline) operator(:) string<delimiter(')delimiter(')>operator(;) - operator(}) -operator(}) - -comment(// Output buffering: Prepend line numbers to each line of output.) -reserved(class) class(Number) operator({) - reserved(function) function(Number)operator(()operator(\)) operator({) - local_variable($this)operator(->)ident(line_number) operator(=) integer(0)operator(;) - operator(}) - - reserved(function) function(filter)operator(()local_variable($output)operator(,) local_variable($mode)operator(\)) operator({) - local_variable($result) operator(=) predefined(array)operator(()operator(\))operator(;) - local_variable($newline) operator(=) string<delimiter(')delimiter(')>operator(;) - reserved(if) operator(()predefined(strlen)operator(()local_variable($output)operator(\)) operator(>) integer(0) operator(&&) local_variable($output)operator([)predefined(strlen)operator(()local_variable($output)operator(\)) operator(-) integer(1)operator(]) operator(==) string<delimiter(")char(\\n)delimiter(")>operator(\)) operator({) - local_variable($newline) operator(=) string<delimiter(")char(\\n)delimiter(")>operator(;) - local_variable($output) operator(=) predefined(substr)operator(()local_variable($output)operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(;) - operator(}) - reserved(foreach) operator(()predefined(explode)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) local_variable($output)operator(\)) reserved(as) local_variable($i) operator(=>) local_variable($line)operator(\)) operator({) - local_variable($this)operator(->)ident(line_number)operator(++)operator(;) - local_variable($result)operator([)operator(]) operator(=) local_variable($this)operator(->)ident(line_number) operator(.) string<delimiter(')content(: )delimiter(')> operator(.) local_variable($line)operator(;) - operator(}) - reserved(return) predefined(implode)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) local_variable($result)operator(\)) operator(.) local_variable($newline)operator(;) - operator(}) -operator(}) - -comment(// Output buffering: Prepend "> " to each line of output.) -reserved(class) class(Quote) operator({) - reserved(function) function(Quote)operator(()operator(\)) operator({) - operator(}) - - reserved(function) function(filter)operator(()local_variable($output)operator(,) local_variable($mode)operator(\)) operator({) - local_variable($result) operator(=) predefined(array)operator(()operator(\))operator(;) - local_variable($newline) operator(=) string<delimiter(')delimiter(')>operator(;) - reserved(if) operator(()predefined(strlen)operator(()local_variable($output)operator(\)) operator(>) integer(0) operator(&&) local_variable($output)operator([)predefined(strlen)operator(()local_variable($output)operator(\)) operator(-) integer(1)operator(]) operator(==) string<delimiter(")char(\\n)delimiter(")>operator(\)) operator({) - local_variable($newline) operator(=) string<delimiter(")char(\\n)delimiter(")>operator(;) - local_variable($output) operator(=) predefined(substr)operator(()local_variable($output)operator(,) integer(0)operator(,) operator(-)integer(1)operator(\))operator(;) - operator(}) - reserved(foreach) operator(()predefined(explode)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) local_variable($output)operator(\)) reserved(as) local_variable($i) operator(=>) local_variable($line)operator(\)) operator({) - local_variable($result)operator([)operator(]) operator(=) string<delimiter(")content(> )local_variable($line)delimiter(")>operator(;) - operator(}) - reserved(return) predefined(implode)operator(()string<delimiter(")char(\\n)delimiter(")>operator(,) local_variable($result)operator(\)) operator(.) local_variable($newline)operator(;) - operator(}) -operator(}) - -comment(// Use arrays as callbacks to register filter methods.) -ident(ob_start)operator(()predefined(array)operator(()reserved(new) constant(Head)operator(()integer(100)operator(\))operator(,) string<delimiter(')content(filter)delimiter(')>operator(\))operator(\))operator(;) -ident(ob_start)operator(()predefined(array)operator(()reserved(new) constant(Number)operator(()operator(\))operator(,) string<delimiter(')content(filter)delimiter(')>operator(\))operator(\))operator(;) -ident(ob_start)operator(()predefined(array)operator(()reserved(new) constant(Quote)operator(()operator(\))operator(,) string<delimiter(')content(filter)delimiter(')>operator(\))operator(\))operator(;) - -comment(// Act like /bin/cat.) -reserved(while) operator(()operator(!)predefined(feof)operator(()constant(STDIN)operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()constant(STDIN)operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(===) pre_constant(false)operator(\)) reserved(break)operator(;) - predefined(echo) local_variable($line)operator(;) -operator(}) - -comment(// Should match number of calls to ob_start(\).) -ident(ob_end_flush)operator(()operator(\))operator(;) -ident(ob_end_flush)operator(()operator(\))operator(;) -ident(ob_end_flush)operator(()operator(\))operator(;) - -comment(// @@PLEAC@@_16.6) -comment(// Process command-line arguments using fopen(\). PHP supports URLs for) -comment(// filenames as long as the "allow_url_fopen" configuration option is set.) -comment(//) -comment(// Valid URL protocols include:) -comment(// - http://www.myserver.com/myfile.html) -comment(// - ftp://ftp.myserver.com/myfile.txt) -comment(// - compress.zlib://myfile.gz) -comment(// - php://stdin) -comment(//) -comment(// See http://www.php.net/manual/en/wrappers.php for details.) -comment(//) -local_variable($filenames) operator(=) predefined(array_slice)operator(()predefined($argv)operator(,) integer(1)operator(\))operator(;) -reserved(if) operator(()operator(!)local_variable($filenames)operator(\)) local_variable($filenames) operator(=) predefined(array)operator(()string<delimiter(')content(php://stdin)delimiter(')>operator(\))operator(;) -reserved(foreach) operator(()local_variable($filenames) reserved(as) local_variable($filename)operator(\)) operator({) - local_variable($handle) operator(=) exception(@)predefined(fopen)operator(()local_variable($filename)operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(;) - reserved(if) operator(()local_variable($handle)operator(\)) operator({) - reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($handle)operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($handle)operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(===) pre_constant(false)operator(\)) reserved(break)operator(;) - comment(// ...) - operator(}) - predefined(fclose)operator(()local_variable($handle)operator(\))operator(;) - operator(}) reserved(else) operator({) - predefined(die)operator(()string<delimiter(")content(can't open )local_variable($filename)char(\\n)delimiter(")>operator(\))operator(;) - operator(}) -operator(}) - -comment(// @@PLEAC@@_16.7) -local_variable($output) operator(=) string<delimiter(`)content(cmd 2>&1)delimiter(`)>operator(;) comment(// with backticks) -comment(// or) -local_variable($ph) operator(=) predefined(popen)operator(()string<delimiter(')content(cmd 2>&1)delimiter(')>operator(\))operator(;) comment(// with an open pipe) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($ph)operator(\))operator(\)) operator({) local_variable($line) operator(=) predefined(fgets)operator(()local_variable($ph)operator(\))operator(;) operator(}) comment(// plus a read) -comment(// -----------------------------) -local_variable($output) operator(=) string<delimiter(`)content(cmd 2>/dev/null)delimiter(`)>operator(;) comment(// with backticks) -comment(// or) -local_variable($ph) operator(=) predefined(popen)operator(()string<delimiter(')content(cmd 2>/dev/null)delimiter(')>operator(\))operator(;) comment(// with an open pipe) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($ph)operator(\))operator(\)) operator({) local_variable($line) operator(=) predefined(fgets)operator(()local_variable($ph)operator(\))operator(;) operator(}) comment(// plus a read) -comment(// -----------------------------) -local_variable($output) operator(=) string<delimiter(`)content(cmd 2>&1 1>/dev/null)delimiter(`)>operator(;) comment(// with backticks) -comment(// or) -local_variable($ph) operator(=) predefined(popen)operator(()string<delimiter(')content(cmd 2>&1 1>/dev/null)delimiter(')>operator(\))operator(;) comment(// with an open pipe) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($ph)operator(\))operator(\)) operator({) local_variable($line) operator(=) predefined(fgets)operator(()local_variable($ph)operator(\))operator(;) operator(}) comment(// plus a read) -comment(// -----------------------------) -local_variable($output) operator(=) string<delimiter(`)content(cmd 3>&1 1>&2 2>&3 3>&-)delimiter(`)>operator(;) comment(// with backticks) -comment(// or) -local_variable($ph) operator(=) predefined(popen)operator(()string<delimiter(')content(cmd 3>&1 1>&2 2>&3 3>&-|)delimiter(')>operator(\))operator(;) comment(// with an open pipe) -reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($ph)operator(\))operator(\)) operator({) local_variable($line) operator(=) predefined(fgets)operator(()local_variable($ph)operator(\))operator(;) operator(}) comment(// plus a read) -comment(// -----------------------------) -predefined(exec)operator(()string<delimiter(')content(program args 1>/tmp/program.stdout 2>/tmp/program.stderr)delimiter(')>operator(\))operator(;) -comment(// -----------------------------) -local_variable($output) operator(=) string<delimiter(`)content(cmd 3>&1 1>&2 2>&3 3>&-)delimiter(`)>operator(;) -comment(// -----------------------------) -local_variable($fd3) operator(=) local_variable($fd1)operator(;) -local_variable($fd1) operator(=) local_variable($fd2)operator(;) -local_variable($fd2) operator(=) local_variable($fd3)operator(;) -local_variable($fd3) operator(=) pre_constant(null)operator(;) -comment(// -----------------------------) -predefined(exec)operator(()string<delimiter(')content(prog args 1>tmpfile 2>&1)delimiter(')>operator(\))operator(;) -predefined(exec)operator(()string<delimiter(')content(prog args 2>&1 1>tmpfile)delimiter(')>operator(\))operator(;) -comment(// -----------------------------) -comment(// exec('prog args 1>tmpfile 2>&1'\);) -local_variable($fd1) operator(=) string<delimiter(")content(tmpfile)delimiter(")>operator(;) comment(// change stdout destination first) -local_variable($fd2) operator(=) local_variable($fd1)operator(;) comment(// now point stderr there, too) -comment(// -----------------------------) -comment(// exec('prog args 2>&1 1>tmpfile'\);) -local_variable($fd2) operator(=) local_variable($fd1)operator(;) comment(// stderr same destination as stdout) -local_variable($fd1) operator(=) string<delimiter(")content(tmpfile)delimiter(")>operator(;) comment(// but change stdout destination) - -comment(// @@PLEAC@@_16.8) -comment(// Connect to input and output of a process.) -local_variable($proc) operator(=) ident(proc_open)operator(()local_variable($program)operator(,) - predefined(array)operator(()integer(0) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(,) - integer(1) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(\))operator(,) - local_variable($pipes)operator(\))operator(;) -reserved(if) operator(()predefined(is_resource)operator(()local_variable($proc)operator(\))operator(\)) operator({) - predefined(fwrite)operator(()local_variable($pipes)operator([)integer(0)operator(])operator(,) string<delimiter(")content(here's your input)char(\\n)delimiter(")>operator(\))operator(;) - predefined(fclose)operator(()local_variable($pipes)operator([)integer(0)operator(])operator(\))operator(;) - predefined(echo) ident(stream_get_contents)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(\))operator(;) - predefined(fclose)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(\))operator(;) - local_variable($result_code) operator(=) ident(proc_close)operator(()local_variable($proc)operator(\))operator(;) - predefined(echo) string<delimiter(")local_variable($result_code)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// -----------------------------) - -local_variable($all) operator(=) predefined(array)operator(()operator(\))operator(;) -local_variable($outlines) operator(=) predefined(array)operator(()operator(\))operator(;) -local_variable($errlines) operator(=) predefined(array)operator(()operator(\))operator(;) -predefined(exec)operator(()string<delimiter(")content(( )local_variable($cmd)content( | sed -e 's/^/stdout: /' \) 2>&1)delimiter(")>operator(,) local_variable($all)operator(\))operator(;) -reserved(foreach) operator(()local_variable($all) reserved(as) local_variable($line)operator(\)) operator({) - local_variable($pos) operator(=) predefined(strpos)operator(()local_variable($line)operator(,) string<delimiter(')content(stdout: )delimiter(')>operator(\))operator(;) - reserved(if) operator(()local_variable($pos) operator(!==) pre_constant(false) operator(&&) local_variable($pos) operator(==) integer(0)operator(\)) operator({) - local_variable($outlines)operator([)operator(]) operator(=) predefined(substr)operator(()local_variable($line)operator(,) integer(8)operator(\))operator(;) - operator(}) reserved(else) operator({) - local_variable($errlines)operator([)operator(]) operator(=) local_variable($line)operator(;) - operator(}) -operator(}) -predefined(print)operator(()string<delimiter(")content(STDOUT:)char(\\n)delimiter(")>operator(\))operator(;) -predefined(print_r)operator(()local_variable($outlines)operator(\))operator(;) -predefined(print)operator(()string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) -predefined(print)operator(()string<delimiter(")content(STDERR:)char(\\n)delimiter(")>operator(\))operator(;) -predefined(print_r)operator(()local_variable($errlines)operator(\))operator(;) -predefined(print)operator(()string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) - -comment(// @@PLEAC@@_16.9) -local_variable($proc) operator(=) ident(proc_open)operator(()local_variable($cmd)operator(,) - predefined(array)operator(()integer(0) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(,) - integer(1) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(,) - integer(2) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(\))operator(,) - local_variable($pipes)operator(\))operator(;) - -reserved(if) operator(()predefined(is_resource)operator(()local_variable($proc)operator(\))operator(\)) operator({) - comment(// give end of file to kid, or feed him) - predefined(fclose)operator(()local_variable($pipes)operator([)integer(0)operator(])operator(\))operator(;) - - comment(// read till EOF) - local_variable($outlines) operator(=) predefined(array)operator(()operator(\))operator(;) - reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(===) pre_constant(false)operator(\)) reserved(break)operator(;) - local_variable($outlines)operator([)operator(]) operator(=) predefined(rtrim)operator(()local_variable($line)operator(\))operator(;) - operator(}) - - comment(// XXX: block potential if massive) - local_variable($errlines) operator(=) predefined(array)operator(()operator(\))operator(;) - reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($pipes)operator([)integer(2)operator(])operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($pipes)operator([)integer(2)operator(])operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(===) pre_constant(false)operator(\)) reserved(break)operator(;) - local_variable($errlines)operator([)operator(]) operator(=) predefined(rtrim)operator(()local_variable($line)operator(\))operator(;) - operator(}) - - predefined(fclose)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(\))operator(;) - predefined(fclose)operator(()local_variable($pipes)operator([)integer(2)operator(])operator(\))operator(;) - ident(proc_close)operator(()local_variable($proc)operator(\))operator(;) - - predefined(print)operator(()string<delimiter(")content(STDOUT:)char(\\n)delimiter(")>operator(\))operator(;) - predefined(print_r)operator(()local_variable($outlines)operator(\))operator(;) - predefined(print)operator(()string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) - predefined(print)operator(()string<delimiter(")content(STDERR:)char(\\n)delimiter(")>operator(\))operator(;) - predefined(print_r)operator(()local_variable($errlines)operator(\))operator(;) - predefined(print)operator(()string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -comment(// cmd3sel - control all three of kids in, out, and error.) -local_variable($cmd) operator(=) string<delimiter(")content(grep vt33 /none/such - /etc/termcap)delimiter(")>operator(;) -local_variable($proc) operator(=) ident(proc_open)operator(()local_variable($cmd)operator(,) - predefined(array)operator(()integer(0) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(,) - integer(1) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(,) - integer(2) operator(=>) predefined(array)operator(()string<delimiter(')content(pipe)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(\))operator(,) - local_variable($pipes)operator(\))operator(;) - -reserved(if) operator(()predefined(is_resource)operator(()local_variable($proc)operator(\))operator(\)) operator({) - predefined(fwrite)operator(()local_variable($pipes)operator([)integer(0)operator(])operator(,) string<delimiter(")content(This line has a vt33 lurking in it)char(\\n)delimiter(")>operator(\))operator(;) - predefined(fclose)operator(()local_variable($pipes)operator([)integer(0)operator(])operator(\))operator(;) - - local_variable($readers) operator(=) predefined(array)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(,) local_variable($pipes)operator([)integer(2)operator(])operator(\))operator(;) - reserved(while) operator(()ident(stream_select)operator(()local_variable($read)operator(=)local_variable($readers)operator(,) - local_variable($write)operator(=)pre_constant(null)operator(,) - local_variable($except)operator(=)pre_constant(null)operator(,) - integer(0)operator(,) integer(200000)operator(\)) operator(>) integer(0)operator(\)) operator({) - reserved(foreach) operator(()local_variable($read) reserved(as) local_variable($stream)operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($stream)operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(!==) pre_constant(false)operator(\)) operator({) - reserved(if) operator(()local_variable($stream) operator(===) local_variable($pipes)operator([)integer(1)operator(])operator(\)) operator({) - predefined(print) string<delimiter(")content(STDOUT: )local_variable($line)delimiter(")>operator(;) - operator(}) reserved(else) operator({) - predefined(print) string<delimiter(")content(STDERR: )local_variable($line)delimiter(")>operator(;) - operator(}) - operator(}) - reserved(if) operator(()predefined(feof)operator(()local_variable($stream)operator(\))operator(\)) operator({) - local_variable($readers) operator(=) predefined(array_diff)operator(()local_variable($readers)operator(,) predefined(array)operator(()local_variable($stream)operator(\))operator(\))operator(;) - operator(}) - operator(}) - operator(}) - - predefined(fclose)operator(()local_variable($pipes)operator([)integer(1)operator(])operator(\))operator(;) - predefined(fclose)operator(()local_variable($pipes)operator([)integer(2)operator(])operator(\))operator(;) - ident(proc_close)operator(()local_variable($proc)operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_16.10) -comment(// PHP supports fork/exec/wait but not pipe. However, it does) -comment(// support socketpair, which can do everything pipes can as well) -comment(// as bidirectional communication. The original recipes have been) -comment(// modified here to use socketpair only.) - -comment(// -----------------------------) - -comment(// pipe1 - use socketpair and fork so parent can send to child) -local_variable($sockets) operator(=) predefined(array)operator(()operator(\))operator(;) -reserved(if) operator(()operator(!)ident(socket_create_pair)operator(()constant(AF_UNIX)operator(,) constant(SOCK_STREAM)operator(,) integer(0)operator(,) local_variable($sockets)operator(\))operator(\)) operator({) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) -operator(}) -predefined(list)operator(()local_variable($reader)operator(,) local_variable($writer)operator(\)) operator(=) local_variable($sockets)operator(;) - -local_variable($pid) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) -reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) -operator(}) reserved(elseif) operator(()local_variable($pid)operator(\)) operator({) - ident(socket_close)operator(()local_variable($reader)operator(\))operator(;) - local_variable($line) operator(=) predefined(sprintf)operator(()string<delimiter(")content(Parent Pid %d is sending this)char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(\))operator(;) - reserved(if) operator(()operator(!)ident(socket_write)operator(()local_variable($writer)operator(,) local_variable($line)operator(,) predefined(strlen)operator(()local_variable($line)operator(\))operator(\))operator(\)) operator({) - ident(socket_close)operator(()local_variable($writer)operator(\))operator(;) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) - operator(}) - ident(socket_close)operator(()local_variable($writer)operator(\))operator(;) - predefined(pcntl_waitpid)operator(()local_variable($pid)operator(,) local_variable($status)operator(\))operator(;) -operator(}) reserved(else) operator({) - ident(socket_close)operator(()local_variable($writer)operator(\))operator(;) - local_variable($line) operator(=) ident(socket_read)operator(()local_variable($reader)operator(,) integer(1024)operator(,) constant(PHP_NORMAL_READ)operator(\))operator(;) - predefined(printf)operator(()string<delimiter(")content(Child Pid %d just read this: `%s')char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(,) predefined(rtrim)operator(()local_variable($line)operator(\))operator(\))operator(;) - ident(socket_close)operator(()local_variable($reader)operator(\))operator(;) comment(// this will happen anyway) - predefined(exit)operator(()integer(0)operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -comment(// pipe2 - use socketpair and fork so child can send to parent) -local_variable($sockets) operator(=) predefined(array)operator(()operator(\))operator(;) -reserved(if) operator(()operator(!)ident(socket_create_pair)operator(()constant(AF_UNIX)operator(,) constant(SOCK_STREAM)operator(,) integer(0)operator(,) local_variable($sockets)operator(\))operator(\)) operator({) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) -operator(}) -predefined(list)operator(()local_variable($reader)operator(,) local_variable($writer)operator(\)) operator(=) local_variable($sockets)operator(;) - -local_variable($pid) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) -reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) -operator(}) reserved(elseif) operator(()local_variable($pid)operator(\)) operator({) - ident(socket_close)operator(()local_variable($writer)operator(\))operator(;) - local_variable($line) operator(=) ident(socket_read)operator(()local_variable($reader)operator(,) integer(1024)operator(,) constant(PHP_NORMAL_READ)operator(\))operator(;) - predefined(printf)operator(()string<delimiter(")content(Parent Pid %d just read this: `%s')char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(,) predefined(rtrim)operator(()local_variable($line)operator(\))operator(\))operator(;) - ident(socket_close)operator(()local_variable($reader)operator(\))operator(;) - predefined(pcntl_waitpid)operator(()local_variable($pid)operator(,) local_variable($status)operator(\))operator(;) -operator(}) reserved(else) operator({) - ident(socket_close)operator(()local_variable($reader)operator(\))operator(;) - local_variable($line) operator(=) predefined(sprintf)operator(()string<delimiter(")content(Child Pid %d is sending this)char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(\))operator(;) - reserved(if) operator(()operator(!)ident(socket_write)operator(()local_variable($writer)operator(,) local_variable($line)operator(,) predefined(strlen)operator(()local_variable($line)operator(\))operator(\))operator(\)) operator({) - ident(socket_close)operator(()local_variable($writer)operator(\))operator(;) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) - operator(}) - ident(socket_close)operator(()local_variable($writer)operator(\))operator(;) comment(// this will happen anyway) - predefined(exit)operator(()integer(0)operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -comment(// pipe3 and pipe4 demonstrate the use of perl's "forking open") -comment(// feature to reimplement pipe1 and pipe2. pipe5 uses two pipes) -comment(// to simulate socketpair. Since PHP supports socketpair but not) -comment(// pipe, and does not have a "forking open" feature, these) -comment(// examples are skipped here.) - -comment(// -----------------------------) - -comment(// pipe6 - bidirectional communication using socketpair) -local_variable($sockets) operator(=) predefined(array)operator(()operator(\))operator(;) -reserved(if) operator(()operator(!)ident(socket_create_pair)operator(()constant(AF_UNIX)operator(,) constant(SOCK_STREAM)operator(,) integer(0)operator(,) local_variable($sockets)operator(\))operator(\)) operator({) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) -operator(}) -predefined(list)operator(()local_variable($child)operator(,) local_variable($parent)operator(\)) operator(=) local_variable($sockets)operator(;) - -local_variable($pid) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) -reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) -operator(}) reserved(elseif) operator(()local_variable($pid)operator(\)) operator({) - ident(socket_close)operator(()local_variable($parent)operator(\))operator(;) - local_variable($line) operator(=) predefined(sprintf)operator(()string<delimiter(")content(Parent Pid %d is sending this)char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(\))operator(;) - reserved(if) operator(()operator(!)ident(socket_write)operator(()local_variable($child)operator(,) local_variable($line)operator(,) predefined(strlen)operator(()local_variable($line)operator(\))operator(\))operator(\)) operator({) - ident(socket_close)operator(()local_variable($child)operator(\))operator(;) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) - operator(}) - local_variable($line) operator(=) ident(socket_read)operator(()local_variable($child)operator(,) integer(1024)operator(,) constant(PHP_NORMAL_READ)operator(\))operator(;) - predefined(printf)operator(()string<delimiter(")content(Parent Pid %d just read this: `%s')char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(,) predefined(rtrim)operator(()local_variable($line)operator(\))operator(\))operator(;) - ident(socket_close)operator(()local_variable($child)operator(\))operator(;) - predefined(pcntl_waitpid)operator(()local_variable($pid)operator(,) local_variable($status)operator(\))operator(;) -operator(}) reserved(else) operator({) - ident(socket_close)operator(()local_variable($child)operator(\))operator(;) - local_variable($line) operator(=) ident(socket_read)operator(()local_variable($parent)operator(,) integer(1024)operator(,) constant(PHP_NORMAL_READ)operator(\))operator(;) - predefined(printf)operator(()string<delimiter(")content(Child Pid %d just read this: `%s')char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(,) predefined(rtrim)operator(()local_variable($line)operator(\))operator(\))operator(;) - local_variable($line) operator(=) predefined(sprintf)operator(()string<delimiter(")content(Child Pid %d is sending this)char(\\n)delimiter(")>operator(,) predefined(getmypid)operator(()operator(\))operator(\))operator(;) - reserved(if) operator(()operator(!)ident(socket_write)operator(()local_variable($parent)operator(,) local_variable($line)operator(,) predefined(strlen)operator(()local_variable($line)operator(\))operator(\))operator(\)) operator({) - ident(socket_close)operator(()local_variable($parent)operator(\))operator(;) - predefined(die)operator(()ident(socket_strerror)operator(()ident(socket_last_error)operator(()operator(\))operator(\))operator(\))operator(;) - operator(}) - ident(socket_close)operator(()local_variable($parent)operator(\))operator(;) - predefined(exit)operator(()integer(0)operator(\))operator(;) -operator(}) - -comment(// @@PLEAC@@_16.11) -comment(// -----------------------------) -comment(// % mkfifo /path/to/named.pipe) -comment(// -----------------------------) - -local_variable($fifo) operator(=) predefined(fopen)operator(()string<delimiter(')content(/path/to/named.pipe)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(;) -reserved(if) operator(()local_variable($fifo) operator(!==) pre_constant(false)operator(\)) operator({) - reserved(while) operator(()operator(!)predefined(feof)operator(()local_variable($fifo)operator(\))operator(\)) operator({) - local_variable($line) operator(=) predefined(fgets)operator(()local_variable($fifo)operator(\))operator(;) - reserved(if) operator(()local_variable($line) operator(===) pre_constant(false)operator(\)) reserved(break)operator(;) - predefined(echo) string<delimiter(")content(Got: )local_variable($line)delimiter(")>operator(;) - operator(}) - predefined(fclose)operator(()local_variable($fifo)operator(\))operator(;) -operator(}) reserved(else) operator({) - predefined(die)operator(()string<delimiter(')content(could not open fifo for read)delimiter(')>operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -local_variable($fifo) operator(=) predefined(fopen)operator(()string<delimiter(')content(/path/to/named.pipe)delimiter(')>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(;) -reserved(if) operator(()local_variable($fifo) operator(!==) pre_constant(false)operator(\)) operator({) - predefined(fwrite)operator(()local_variable($fifo)operator(,) string<delimiter(")content(Smoke this.)char(\\n)delimiter(")>operator(\))operator(;) - predefined(fclose)operator(()local_variable($fifo)operator(\))operator(;) -operator(}) reserved(else) operator({) - predefined(die)operator(()string<delimiter(')content(could not open fifo for write)delimiter(')>operator(\))operator(;) -operator(}) - -comment(// -----------------------------) -comment(// % mkfifo ~/.plan # isn't this everywhere yet?) -comment(// % mknod ~/.plan p # in case you don't have mkfifo) -comment(// -----------------------------) - -comment(// dateplan - place current date and time in .plan file) -reserved(while) operator(()pre_constant(true)operator(\)) operator({) - local_variable($home) operator(=) predefined(getenv)operator(()string<delimiter(')content(HOME)delimiter(')>operator(\))operator(;) - local_variable($fifo) operator(=) predefined(fopen)operator(()string<delimiter(")local_variable($home)content(/.plan)delimiter(")>operator(,) string<delimiter(')content(w)delimiter(')>operator(\))operator(;) - reserved(if) operator(()local_variable($fifo) operator(===) pre_constant(false)operator(\)) operator({) - predefined(die)operator(()string<delimiter(")content(Couldn't open )local_variable($home)content(/.plan for writing.)char(\\n)delimiter(")>operator(\))operator(;) - operator(}) - predefined(fwrite)operator(()local_variable($fifo)operator(,) - string<delimiter(')content(The current time is )delimiter(')> - operator(.) predefined(strftime)operator(()string<delimiter(')content(%a, %d %b %Y %H:%M:%S %z)delimiter(')>operator(\)) - operator(.) string<delimiter(")char(\\n)delimiter(")>operator(\))operator(;) - predefined(fclose)operator(()local_variable($fifo)operator(\))operator(;) - predefined(sleep)operator(()integer(1)operator(\))operator(;) -operator(}) - -comment(// -----------------------------) - -comment(// fifolog - read and record log msgs from fifo) - -local_variable($fifo) operator(=) pre_constant(null)operator(;) - -reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) -reserved(function) function(handle_alarm)operator(()local_variable($signal)operator(\)) operator({) - reserved(global) local_variable($fifo)operator(;) - reserved(if) operator(()local_variable($fifo)operator(\)) predefined(fclose)operator(()local_variable($fifo)operator(\))operator(;) comment(// move on to the next queued process) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGALRM)operator(,) string<delimiter(')content(handle_alarm)delimiter(')>operator(\))operator(;) - -reserved(while) operator(()pre_constant(true)operator(\)) operator({) - predefined(pcntl_alarm)operator(()integer(0)operator(\))operator(;) comment(// turn off alarm for blocking open) - local_variable($fifo) operator(=) predefined(fopen)operator(()string<delimiter(')content(/tmp/log)delimiter(')>operator(,) string<delimiter(')content(r)delimiter(')>operator(\))operator(;) - reserved(if) operator(()local_variable($fifo) operator(===) pre_constant(false)operator(\)) operator({) - predefined(die)operator(()string<delimiter(")content(can't open /tmp/log)delimiter(")>operator(\))operator(;) - operator(}) - predefined(pcntl_alarm)operator(()integer(1)operator(\))operator(;) comment(// you have 1 second to log) - - local_variable($service) operator(=) predefined(fgets)operator(()local_variable($fifo)operator(\))operator(;) - reserved(if) operator(()local_variable($service) operator(===) pre_constant(false)operator(\)) reserved(continue)operator(;) comment(// interrupt or nothing logged) - local_variable($service) operator(=) predefined(rtrim)operator(()local_variable($service)operator(\))operator(;) - - local_variable($message) operator(=) predefined(fgets)operator(()local_variable($fifo)operator(\))operator(;) - reserved(if) operator(()local_variable($message) operator(===) pre_constant(false)operator(\)) reserved(continue)operator(;) comment(// interrupt or nothing logged) - local_variable($message) operator(=) predefined(rtrim)operator(()local_variable($message)operator(\))operator(;) - - predefined(pcntl_alarm)operator(()integer(0)operator(\))operator(;) comment(// turn off alarms for message processing) - - reserved(if) operator(()local_variable($service) operator(==) string<delimiter(')content(http)delimiter(')>operator(\)) operator({) - comment(// ignoring) - operator(}) reserved(elseif) operator(()local_variable($service) operator(==) string<delimiter(')content(login)delimiter(')>operator(\)) operator({) - comment(// log to /var/log/login) - local_variable($log) operator(=) predefined(fopen)operator(()string<delimiter(')content(/var/log/login)delimiter(')>operator(,) string<delimiter(')content(a)delimiter(')>operator(\))operator(;) - reserved(if) operator(()local_variable($log) operator(!==) pre_constant(false)operator(\)) operator({) - predefined(fwrite)operator(()local_variable($log)operator(,) - predefined(strftime)operator(()string<delimiter(')content(%a, %d %b %Y %H:%M:%S %z)delimiter(')>operator(\)) - operator(.) string<delimiter(")content( )local_variable($service)content( )local_variable($message)char(\\n)delimiter(")>operator(\))operator(;) - predefined(fclose)operator(()local_variable($log)operator(\))operator(;) - operator(}) reserved(else) operator({) - predefined(trigger_error)operator(()string<delimiter(")content(Couldn't log )local_variable($service)content( )local_variable($message)content( to /var/log/login)char(\\n)delimiter(")>operator(,) - exception(E_USER_WARNING)operator(\))operator(;) - operator(}) - operator(}) -operator(}) - -comment(// @@PLEAC@@_16.12) -comment(// sharetest - test shared variables across forks) - -local_variable($SHM_KEY) operator(=) predefined(ftok)operator(()pre_constant(__FILE__)operator(,) predefined(chr)operator(()integer(1)operator(\))operator(\))operator(;) -local_variable($handle) operator(=) ident(sem_get)operator(()local_variable($SHM_KEY)operator(\))operator(;) -local_variable($buffer) operator(=) ident(shm_attach)operator(()local_variable($handle)operator(,) integer(1024)operator(\))operator(;) - -comment(// The original recipe has an INT signal handler here. However, it) -comment(// causes erratic behavior with PHP, and PHP seems to do the right) -comment(// thing without it.) - -reserved(for) operator(()local_variable($i) operator(=) integer(0)operator(;) local_variable($i) operator(<) integer(10)operator(;) local_variable($i)operator(++)operator(\)) operator({) - local_variable($child) operator(=) predefined(pcntl_fork)operator(()operator(\))operator(;) - reserved(if) operator(()local_variable($child) operator(==) operator(-)integer(1)operator(\)) operator({) - predefined(die)operator(()string<delimiter(')content(cannot fork)delimiter(')>operator(\))operator(;) - operator(}) reserved(elseif) operator(()local_variable($child)operator(\)) operator({) - local_variable($kids)operator([)operator(]) operator(=) local_variable($child)operator(;) comment(// in case we care about their pids) - operator(}) reserved(else) operator({) - ident(squabble)operator(()operator(\))operator(;) - predefined(exit)operator(()operator(\))operator(;) - operator(}) -operator(}) - -reserved(while) operator(()pre_constant(true)operator(\)) operator({) - predefined(print) string<delimiter(')content(Buffer is )delimiter(')> operator(.) ident(shm_get_var)operator(()local_variable($buffer)operator(,) integer(1)operator(\)) operator(.) string<delimiter(")char(\\n)delimiter(")>operator(;) - predefined(sleep)operator(()integer(1)operator(\))operator(;) -operator(}) -predefined(die)operator(()string<delimiter(')content(Not reached)delimiter(')>operator(\))operator(;) - -reserved(function) function(squabble)operator(()operator(\)) operator({) - reserved(global) local_variable($handle)operator(;) - reserved(global) local_variable($buffer)operator(;) - local_variable($i) operator(=) integer(0)operator(;) - local_variable($pid) operator(=) predefined(getmypid)operator(()operator(\))operator(;) - reserved(while) operator(()pre_constant(true)operator(\)) operator({) - reserved(if) operator(()predefined(preg_match)operator(()string<delimiter(")content(/^)local_variable($pid)char(\\\\)content(b/)delimiter(")>operator(,) ident(shm_get_var)operator(()local_variable($buffer)operator(,) integer(1)operator(\))operator(\))operator(\)) reserved(continue)operator(;) - ident(sem_acquire)operator(()local_variable($handle)operator(\))operator(;) - local_variable($i)operator(++)operator(;) - ident(shm_put_var)operator(()local_variable($buffer)operator(,) integer(1)operator(,) string<delimiter(")local_variable($pid)content( )local_variable($i)delimiter(")>operator(\))operator(;) - ident(sem_release)operator(()local_variable($handle)operator(\))operator(;) - operator(}) -operator(}) - -comment(// Buffer is 14357 1) -comment(// Buffer is 14355 3) -comment(// Buffer is 14355 4) -comment(// Buffer is 14354 5) -comment(// Buffer is 14353 6) -comment(// Buffer is 14351 8) -comment(// Buffer is 14351 9) -comment(// Buffer is 14350 10) -comment(// Buffer is 14348 11) -comment(// Buffer is 14348 12) -comment(// Buffer is 14357 10) -comment(// Buffer is 14357 11) -comment(// Buffer is 14355 13) -comment(// ...) - -comment(// @@PLEAC@@_16.13) -comment(// Available signal constants) -operator(%) ident(php) operator(-)ident(r) string<delimiter(')content(print_r(get_defined_constants(\)\);)delimiter(')> operator(|) ident(grep) string<delimiter(')content(\\[)content(SIG)delimiter(')> operator(|) ident(grep) operator(-)ident(v) ident(_) - operator([)constant(SIGHUP)operator(]) operator(=>) integer(1) - operator([)constant(SIGINT)operator(]) operator(=>) integer(2) - operator([)constant(SIGQUIT)operator(]) operator(=>) integer(3) - operator([)constant(SIGILL)operator(]) operator(=>) integer(4) - operator([)constant(SIGTRAP)operator(]) operator(=>) integer(5) - operator([)constant(SIGABRT)operator(]) operator(=>) integer(6) - operator([)constant(SIGIOT)operator(]) operator(=>) integer(6) - operator([)constant(SIGBUS)operator(]) operator(=>) integer(7) - operator([)constant(SIGFPE)operator(]) operator(=>) integer(8) - operator([)constant(SIGKILL)operator(]) operator(=>) integer(9) - operator([)constant(SIGUSR1)operator(]) operator(=>) integer(10) - operator([)constant(SIGSEGV)operator(]) operator(=>) integer(11) - operator([)constant(SIGUSR2)operator(]) operator(=>) integer(12) - operator([)constant(SIGPIPE)operator(]) operator(=>) integer(13) - operator([)constant(SIGALRM)operator(]) operator(=>) integer(14) - operator([)constant(SIGTERM)operator(]) operator(=>) integer(15) - operator([)constant(SIGSTKFLT)operator(]) operator(=>) integer(16) - operator([)constant(SIGCLD)operator(]) operator(=>) integer(17) - operator([)constant(SIGCHLD)operator(]) operator(=>) integer(17) - operator([)constant(SIGCONT)operator(]) operator(=>) integer(18) - operator([)constant(SIGSTOP)operator(]) operator(=>) integer(19) - operator([)constant(SIGTSTP)operator(]) operator(=>) integer(20) - operator([)constant(SIGTTIN)operator(]) operator(=>) integer(21) - operator([)constant(SIGTTOU)operator(]) operator(=>) integer(22) - operator([)constant(SIGURG)operator(]) operator(=>) integer(23) - operator([)constant(SIGXCPU)operator(]) operator(=>) integer(24) - operator([)constant(SIGXFSZ)operator(]) operator(=>) integer(25) - operator([)constant(SIGVTALRM)operator(]) operator(=>) integer(26) - operator([)constant(SIGPROF)operator(]) operator(=>) integer(27) - operator([)constant(SIGWINCH)operator(]) operator(=>) integer(28) - operator([)constant(SIGPOLL)operator(]) operator(=>) integer(29) - operator([)constant(SIGIO)operator(]) operator(=>) integer(29) - operator([)constant(SIGPWR)operator(]) operator(=>) integer(30) - operator([)constant(SIGSYS)operator(]) operator(=>) integer(31) - operator([)constant(SIGBABY)operator(]) operator(=>) integer(31) - -comment(// Predefined signal handler constants) -operator(%) ident(php) operator(-)ident(r) string<delimiter(')content(print_r(get_defined_constants(\)\);)delimiter(')> operator(|) ident(grep) string<delimiter(')content(\\[)content(SIG)delimiter(')> operator(|) ident(grep) ident(_) - operator([)constant(SIG_IGN)operator(]) operator(=>) integer(1) - operator([)constant(SIG_DFL)operator(]) operator(=>) integer(0) - operator([)constant(SIG_ERR)operator(]) operator(=>) operator(-)integer(1) - -comment(// @@PLEAC@@_16.14) -comment(// send pid a signal 9) -predefined(posix_kill)operator(()local_variable($pid)operator(,) integer(9)operator(\))operator(;) -comment(// send whole job a signal 1) -predefined(posix_kill)operator(()local_variable($pgrp)operator(,) operator(-)integer(1)operator(\))operator(;) -comment(// send myself a SIGUSR1) -predefined(posix_kill)operator(()predefined(getmypid)operator(()operator(\))operator(,) constant(SIGUSR1)operator(\))operator(;) -comment(// send a SIGHUP to processes in pids) -reserved(foreach) operator(()local_variable($pids) reserved(as) local_variable($pid)operator(\)) predefined(posix_kill)operator(()local_variable($pid)operator(,) constant(SIGHUP)operator(\))operator(;) - -comment(// -----------------------------) - -comment(// Use kill with pseudo-signal 0 to see if process is alive.) -reserved(if) operator(()predefined(posix_kill)operator(()local_variable($minion)operator(,) integer(0)operator(\))operator(\)) operator({) - predefined(echo) string<delimiter(")local_variable($minion)content( is alive!)char(\\n)delimiter(")>operator(;) -operator(}) reserved(else) operator({) - predefined(echo) string<delimiter(")local_variable($minion)content( is deceased.)char(\\n)delimiter(")>operator(;) -operator(}) - -comment(// @@PLEAC@@_16.15) -comment(// call got_sig_quit for every SIGQUIT) -predefined(pcntl_signal)operator(()constant(SIGQUIT)operator(,) string<delimiter(')content(got_sig_quit)delimiter(')>operator(\))operator(;) -comment(// call got_sig_pipe for every SIGPIPE) -predefined(pcntl_signal)operator(()constant(SIGPIPE)operator(,) string<delimiter(')content(got_sig_pipe)delimiter(')>operator(\))operator(;) -comment(// increment ouch for every SIGINT) -reserved(function) function(got_sig_int)operator(()local_variable($signal)operator(\)) operator({) reserved(global) local_variable($ouch)operator(;) local_variable($ouch)operator(++)operator(;) operator(}) -predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(got_sig_int)delimiter(')>operator(\))operator(;) -comment(// ignore the signal INT) -predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) constant(SIG_IGN)operator(\))operator(;) -comment(// restore default STOP signal handling) -predefined(pcntl_signal)operator(()constant(SIGSTOP)operator(,) constant(SIG_DFL)operator(\))operator(;) - -comment(// @@PLEAC@@_16.16) -comment(// the signal handler) -reserved(function) function(ding)operator(()local_variable($signal)operator(\)) operator({) - predefined(fwrite)operator(()constant(STDERR)operator(,) string<delimiter(")char(\\x07)content(Enter your name!)char(\\n)delimiter(")>operator(\))operator(;) -operator(}) - -comment(// prompt for name, overriding SIGINT) -reserved(function) function(get_name)operator(()operator(\)) operator({) - reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(ding)delimiter(')>operator(\))operator(;) - - predefined(echo) string<delimiter(")content(Kindly Stranger, please enter your name: )delimiter(")>operator(;) - reserved(while) operator(()operator(!)exception(@)ident(stream_select)operator(()local_variable($read)operator(=)predefined(array)operator(()constant(STDIN)operator(\))operator(,) - local_variable($write)operator(=)pre_constant(null)operator(,) - local_variable($except)operator(=)pre_constant(null)operator(,) - integer(1)operator(\))operator(\)) operator({) - comment(// allow signals to be observed) - operator(}) - local_variable($name) operator(=) predefined(fgets)operator(()constant(STDIN)operator(\))operator(;) - - comment(// Since pcntl_signal(\) doesn't return the old signal handler, the) - comment(// best we can do here is set it back to the default behavior.) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) constant(SIG_DFL)operator(\))operator(;) - - reserved(return) local_variable($name)operator(;) -operator(}) - -comment(// @@PLEAC@@_16.17) -reserved(function) function(got_int)operator(()local_variable($signal)operator(\)) operator({) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(got_int)delimiter(')>operator(\))operator(;) comment(// but not for SIGCHLD!) - comment(// ...) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(got_int)delimiter(')>operator(\))operator(;) - -comment(// -----------------------------) - -reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) -local_variable($interrupted) operator(=) pre_constant(false)operator(;) - -reserved(function) function(got_int)operator(()local_variable($signal)operator(\)) operator({) - reserved(global) local_variable($interrupted)operator(;) - local_variable($interrupted) operator(=) pre_constant(true)operator(;) - comment(// The third argument to pcntl_signal(\) determines if system calls) - comment(// should be restarted after a signal. It defaults to true.) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(got_int)delimiter(')>operator(,) pre_constant(false)operator(\))operator(;) comment(// or SIG_IGN) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(got_int)delimiter(')>operator(,) pre_constant(false)operator(\))operator(;) - -comment(// ... long-running code that you don't want to restart) - -reserved(if) operator(()local_variable($interrupted)operator(\)) operator({) - comment(// deal with the signal) -operator(}) - -comment(// @@PLEAC@@_16.18) -comment(// ignore signal INT) -predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) constant(SIG_IGN)operator(\))operator(;) - -comment(// install signal handler) -reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) -reserved(function) function(tsktsk)operator(()local_variable($signal)operator(\)) operator({) - predefined(fwrite)operator(()constant(STDERR)operator(,) string<delimiter(")char(\\x07)content(The long habit of living indisposeth us for dying.)delimiter(")>operator(\))operator(;) - predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(tsktsk)delimiter(')>operator(\))operator(;) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGINT)operator(,) string<delimiter(')content(tsktsk)delimiter(')>operator(\))operator(;) - -comment(// @@PLEAC@@_16.19) -predefined(pcntl_signal)operator(()constant(SIGCHLD)operator(,) constant(SIG_IGN)operator(\))operator(;) - -comment(// -----------------------------) - -reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) -reserved(function) function(reaper)operator(()local_variable($signal)operator(\)) operator({) - local_variable($pid) operator(=) predefined(pcntl_waitpid)operator(()operator(-)integer(1)operator(,) local_variable($status)operator(,) constant(WNOHANG)operator(\))operator(;) - reserved(if) operator(()local_variable($pid) operator(>) integer(0)operator(\)) operator({) - comment(// ...) - ident(reaper)operator(()local_variable($signal)operator(\))operator(;) - operator(}) - comment(// install *after* calling waitpid) - predefined(pcntl_signal)operator(()constant(SIGCHLD)operator(,) string<delimiter(')content(reaper)delimiter(')>operator(\))operator(;) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGCHLD)operator(,) string<delimiter(')content(reaper)delimiter(')>operator(\))operator(;) - -comment(// -----------------------------) - -reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) -reserved(function) function(reaper)operator(()local_variable($signal)operator(\)) operator({) - local_variable($pid) operator(=) predefined(pcntl_waitpid)operator(()operator(-)integer(1)operator(,) local_variable($status)operator(,) constant(WNOHANG)operator(\))operator(;) - reserved(if) operator(()local_variable($pid) operator(==) operator(-)integer(1)operator(\)) operator({) - comment(// No child waiting. Ignore it.) - operator(}) reserved(else) operator({) - reserved(if) operator(()predefined(pcntl_wifexited)operator(()local_variable($signal)operator(\))operator(\)) operator({) - predefined(echo) string<delimiter(")content(Process )local_variable($pid)content( exited.)char(\\n)delimiter(")>operator(;) - operator(}) reserved(else) operator({) - predefined(echo) string<delimiter(")content(False alarm on )local_variable($pid)char(\\n)delimiter(")>operator(;) - operator(}) - ident(reaper)operator(()local_variable($signal)operator(\))operator(;) - operator(}) - predefined(pcntl_signal)operator(()constant(SIGCHLD)operator(,) string<delimiter(')content(reaper)delimiter(')>operator(\))operator(;) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGCHLD)operator(,) string<delimiter(')content(reaper)delimiter(')>operator(\))operator(;) - -comment(// @@PLEAC@@_16.20) -comment(// PHP does not support sigprocmask(\).) - -comment(// @@PLEAC@@_16.21) -reserved(declare)operator(()ident(ticks) operator(=) integer(1)operator(\))operator(;) -local_variable($aborted) operator(=) pre_constant(false)operator(;) - -reserved(function) function(handle_alarm)operator(()local_variable($signal)operator(\)) operator({) - reserved(global) local_variable($aborted)operator(;) - local_variable($aborted) operator(=) pre_constant(true)operator(;) -operator(}) -predefined(pcntl_signal)operator(()constant(SIGALRM)operator(,) string<delimiter(')content(handle_alarm)delimiter(')>operator(\))operator(;) - -predefined(pcntl_alarm)operator(()integer(3600)operator(\))operator(;) -comment(// long-time operations here) -predefined(pcntl_alarm)operator(()integer(0)operator(\))operator(;) -reserved(if) operator(()local_variable($aborted)operator(\)) operator({) - comment(// timed out - do what you will here) -operator(}) diff --git a/test/scanners/php/pleac.in.php b/test/scanners/php/pleac.in.php deleted file mode 100644 index 4bd0300..0000000 --- a/test/scanners/php/pleac.in.php +++ /dev/null @@ -1,5516 +0,0 @@ -# -*- php -*- -# The examples are taken from the Perl Cookbook -# By Tom Christiansen & Nathan Torkington -# see http://www.oreilly.com/catalog/cookbook for more - -# @@PLEAC@@_NAME -# @@SKIP@@ PHP - -# @@PLEAC@@_WEB -# @@SKIP@@ http://php.net/ - -# @@PLEAC@@_1.0 -#----------------------------- -$string = '\n'; # two characters, \ and an n -$string = 'Jon \'Maddog\' Orwant'; # literal single quotes -$string = 'Jon "Maddog" Orwant'; # literal double quotes -#----------------------------- -$string = "\n"; # a "newline" character -$string = "Jon \"Maddog\" Orwant"; # literal double quotes -$string = "Jon 'Maddog' Orwant"; # literal single quotes -#----------------------------- -$a = -"This is a multiline -here document"; - -$a = <<<EOF -This is a multiline here document -terminated by EOF on a line by itself -EOF; -#----------------------------- - -# @@PLEAC@@_1.1 -#----------------------------- -$value = substr($string, $offset, $count); -$value = substr($string, $offset); - -$string = substr_replace($string, $newstring, $offset, $count); -$string = substr_replace($string, $newtail, $offset); -#----------------------------- -# get a 5-byte string, skip 3, then grab 2 8-byte strings, then the rest -list($leading, $s1, $s2, $trailing) = - array_values(unpack("A5a/x3/A8b/A8c/A*d", $data); - -# split at five byte boundaries -preg_match_all ("/.{5}/", $data, $f, PREG_PATTERN_ORDER); -$fivers = $f[0]; - -# chop string into individual characters -$chars = $string; -#----------------------------- -$string = "This is what you have"; -# +012345678901234567890 Indexing forwards (left to right) -# 109876543210987654321- Indexing backwards (right to left) -# note that 0 means 10 or 20, etc. above - -$first = substr($string, 0, 1); # "T" -$start = substr($string, 5, 2); # "is" -$rest = substr($string, 13); # "you have" -$last = substr($string, -1); # "e" -$end = substr($string, -4); # "have" -$piece = substr($string, -8, 3); # "you" -#----------------------------- -$string = "This is what you have"; -print $string; -#This is what you have - -$string = substr_replace($string, "wasn't", 5, 2); # change "is" to "wasn't" -#This wasn't what you have - -$string = substr_replace($string, "ondrous", -12); # "This wasn't wondrous" -#This wasn't wondrous - -$string = substr_replace($string, "", 0, 1); # delete first character -#his wasn't wondrous - -$string = substr_replace($string, "", -10); # delete last 10 characters -#his wasn' -#----------------------------- -if (preg_match("/pattern/", substr($string, -10)) { - print "Pattern matches in last 10 characters\n"; -} - -# substitute "at" for "is", restricted to first five characters -$string=(substr_replace(preg_replace("/is/", "at", substr($string,0,5)),0,5); -#----------------------------- -# exchange the first and last letters in a string -$a = "make a hat"; -list($a[0], $a[strlen($a)-1]) = Array(substr($a,-1), substr($a,0,1)); -print $a; - -#----------------------------- -# extract column with unpack -$a = "To be or not to be"; -$b = unpack("x6/A6a", $a); # skip 6, grab 6 -print $b['a']; - - -$b = unpack("x6/A2b/X5/A2c", $a); # forward 6, grab 2; backward 5, grab 2 -print $b['b']."\n".$b['c']."\n"; - -#----------------------------- -function cut2fmt() { - $positions = func_get_args(); - $template = ''; - $lastpos = 1; - foreach($positions as $place) { - $template .= "A" . ($place - $lastpos) . " "; - $lastpos = $place; - } - $template .= "A*"; - return $template; -} - -$fmt = cut2fmt(8, 14, 20, 26, 30); -print "$fmt\n"; -#A7 A6 A6 A6 A4 A* -#----------------------------- - -# @@PLEAC@@_1.2 -#----------------------------- -# use $b if $b is true, else $c -$a = $b?$b:$c; - -# set $x to $y unless $x is already true -$x || $x=$y; -#----------------------------- -# use $b if $b is defined, else $c -$a = defined($b) ? $b : $c; -#----------------------------- -$foo = $bar || $foo = "DEFAULT VALUE"; -#----------------------------- -$dir = array_shift($_SERVER['argv']) || $dir = "/tmp"; -#----------------------------- -$dir = $_SERVER['argv'][0] || $dir = "/tmp"; -#----------------------------- -$dir = defined($_SERVER['argv'][0]) ? array_shift($_SERVER['argv']) : "/tmp"; -#----------------------------- -$dir = count($_SERVER['argv']) ? $_SERVER['argv'][0] : "/tmp"; -#----------------------------- -$count[$shell?$shell:"/bin/sh"]++; -#----------------------------- -# find the user name on Unix systems -$user = $_ENV['USER'] - || $user = $_ENV['LOGNAME'] - || $user = posix_getlogin() - || $user = posix_getpwuid(posix_getuid())[0] - || $user = "Unknown uid number $<"; -#----------------------------- -$starting_point || $starting_point = "Greenwich"; -#----------------------------- -count($a) || $a = $b; # copy only if empty -$a = count($b) ? $b : $c; # assign @b if nonempty, else @c -#----------------------------- - -# @@PLEAC@@_1.3 -#----------------------------- -list($VAR1, $VAR2) = array($VAR2, $VAR1); -#----------------------------- -$temp = $a; -$a = $b; -$b = $temp; -#----------------------------- -$a = "alpha"; -$b = "omega"; -list($a, $b) = array($b, $a); # the first shall be last -- and versa vice -#----------------------------- -list($alpha, $beta, $production) = Array("January","March","August"); -# move beta to alpha, -# move production to beta, -# move alpha to production -list($alpha, $beta, $production) = array($beta, $production, $alpha); -#----------------------------- - -# @@PLEAC@@_1.4 -#----------------------------- -$num = ord($char); -$char = chr($num); -#----------------------------- -$char = sprintf("%c", $num); # slower than chr($num) -printf("Number %d is character %c\n", $num, $num); -#----------------------------- -$ASCII = unpack("C*", $string); -eval('$STRING = pack("C*", '.implode(',',$ASCII).');'); -#----------------------------- -$ascii_value = ord("e"); # now 101 -$character = chr(101); # now "e" -#----------------------------- -printf("Number %d is character %c\n", 101, 101); -#----------------------------- -$ascii_character_numbers = unpack("C*", "sample"); -print explode(" ",$ascii_character_numbers)."\n"; - -eval('$word = pack("C*", '.implode(',',$ascii_character_numbers).');'); -$word = pack("C*", 115, 97, 109, 112, 108, 101); # same -print "$word\n"; -#----------------------------- -$hal = "HAL"; -$ascii = unpack("C*", $hal); -foreach ($ascii as $val) { - $val++; # add one to each ASCII value -} -eval('$ibm = pack("C*", '.implode(',',$ascii).');'); -print "$ibm\n"; # prints "IBM" -#----------------------------- - -# @@PLEAC@@_1.5 -#----------------------------- -// using perl regexp -$array = preg_split('//', $string ,-1, PREG_SPLIT_NO_EMPTY); -// using PHP function: $array = str_split($string); - -// Cannot use unpack with a format of 'U*' in PHP. -#----------------------------- -for ($offset = 0; preg_match('/(.)/', $string, $matches, 0, $offset) > 0; $offset++) { - // $matches[1] has charcter, ord($matches[1]) its number -} -#----------------------------- -$seen = array(); -$string = "an apple a day"; -foreach (str_split($string) as $char) { - $seen[$char] = 1; -} -$keys = array_keys($seen); -sort($keys); -print "unique chars are: " . implode('', $keys)) . "\n"; -unique chars are: adelnpy -#----------------------------- -$seen = array(); -$string = "an apple a day"; -for ($offset = 0; preg_match('/(.)/', $string, $matches, 0, $offset) > 0; $offset++) { - $seen[$matches[1]] = 1; -} -$keys = array_keys($seen); -sort($keys); -print "unique chars are: " . implode('', $keys) . "\n"; -unique chars are: adelnpy -#----------------------------- -$sum = 0; -foreach (unpack("C*", $string) as $byteval) { - $sum += $byteval; -} -print "sum is $sum\n"; -// prints "1248" if $string was "an apple a day" -#----------------------------- -$sum = array_sum(unpack("C*", $string)); -#----------------------------- - -// sum - compute 16-bit checksum of all input files -$handle = @fopen($argv[1], 'r'); -$checksum = 0; -while (!feof($handle)) { - $checksum += (array_sum(unpack("C*", fgets($handle)))); -} -$checksum %= pow(2,16) - 1; -print "$checksum\n"; - -# @@INCLUDE@@ include/php/slowcat.php -#----------------------------- - -# @@PLEAC@@_1.6 -#----------------------------- -$revchars = strrev($string); -#----------------------------- -$revwords = implode(" ", array_reverse(explode(" ", $string))); -#----------------------------- -// reverse word order -$string = 'Yoda said, "can you see this?"'; -$allwords = explode(" ", $string); -$revwords = implode(" ", array_reverse($allwords)); -print $revwords . "\n"; -this?" see you "can said, Yoda -#----------------------------- -$revwords = implode(" ", array_reverse(explode(" ", $string))); -#----------------------------- -$revwords = implode(" ", array_reverse(preg_split("/(\s+)/", $string))); -#----------------------------- -$word = "reviver"; -$is_palindrome = ($word === strrev($word)); -#----------------------------- -// quite a one-liner since "php" does not have a -n switch -% php -r 'while (!feof(STDIN)) { $word = rtrim(fgets(STDIN)); if ($word == strrev($word) && strlen($word) > 5) print $word; }' < /usr/dict/words -#----------------------------- - -# @@PLEAC@@_1.8 -#----------------------------- -$text = preg_replace('/\$(\w+)/e', '$$1', $text); -#----------------------------- -list($rows, $cols) = Array(24, 80); -$text = 'I am $rows high and $cols long'; -$text = preg_replace('/\$(\w+)/e', '$$1', $text); -print $text; - -#----------------------------- -$text = "I am 17 years old"; -$text = preg_replace('/(\d+)/e', '2*$1', $text); -#----------------------------- -# expand variables in $text, but put an error message in -# if the variable isn't defined -$text = preg_replace('/\$(\w+)/e','isset($$1)?$$1:\'[NO VARIABLE: $$1]\'', $text); -#----------------------------- - -// As PHP arrays are used as hashes too, separation of section 4 -// and section 5 makes little sense. - -# @@PLEAC@@_1.9 -#----------------------------- -$big = strtoupper($little); -$little = strtolower($big); -// PHP does not have the\L and\U string escapes. -#----------------------------- -$big = ucfirst($little); -$little = strtolower(substr($big, 0, 1)) . substr($big, 1); -#----------------------------- -$beast = "dromedary"; -// capitalize various parts of $beast -$capit = ucfirst($beast); // Dromedar -// PHP does not have the\L and\U string escapes. -$capall = strtoupper($beast); // DROMEDAR -// PHP does not have the\L and\U string escapes. -$caprest = strtolower(substr($beast, 0, 1)) . substr(strtoupper($beast), 1); // dROMEDAR -// PHP does not have the\L and\U string escapes. -#----------------------------- -// titlecase each word's first character, lowercase the rest -$text = "thIS is a loNG liNE"; -$text = ucwords(strtolower($text)); -print $text; -This Is A Long Line -#----------------------------- -if (strtoupper($a) == strtoupper($b)) { // or strcasecmp($a, $b) == 0 - print "a and b are the same\n"; -} -#----------------------------- -# @@INCLUDE@@ include/php/randcap.php - -// % php randcap.php < genesis | head -9 -#----------------------------- - -# @@PLEAC@@_1.10 -#----------------------------- -echo $var1 . func() . $var2; // scalar only -#----------------------------- -// PHP can only handle variable expression without operators -$answer = "STRING ${[ VAR EXPR ]} MORE STRING"; -#----------------------------- -$phrase = "I have " . ($n + 1) . " guanacos."; -// PHP cannot handle the complex exression: ${\($n + 1)} -#----------------------------- -// Rest of Discussion is not applicable to PHP -#----------------------------- -// Interpolating functions not available in PHP -#----------------------------- - -# @@PLEAC@@_1.11 -# @@INCOMPLETE@@ -# @@INCOMPLETE@@ - -# @@PLEAC@@_1.12 -#----------------------------- -$output = wordwrap($str, $width, $break, $cut); -#----------------------------- -# @@INCLUDE@@ include/php/wrapdemo.php -#----------------------------- -// merge multiple lines into one, then wrap one long line -print wordwrap(str_replace("\n", " ", file_get_contents('php://stdin'))); -#----------------------------- -while(!feof(STDIN)) { - print wordwrap(str_replace("\n", " ", stream_get_line(STDIN, 0, "\n\n"))); - print "\n\n"; -} -#----------------------------- - -# @@PLEAC@@_1.13 -#----------------------------- -//backslash -$var = preg_replace('/([CHARLIST])/', '\\\$1', $var); -// double -$var = preg_replace('/([CHARLIST])/', '$1$1', $var); -#----------------------------- -$var = preg_replace('/%/', '%%', $var); -#----------------------------- -$string = 'Mom said, "Don\'t do that."'; -$string = preg_replace('/([\'"])/', '\\\$1', $string); -// in PHP you can also use the addslashes() function -#----------------------------- -$string = 'Mom said, "Don\'t do that."'; -$string = preg_replace('/([\'"])/', '$1$1', $string); -#----------------------------- -$string = preg_replace('/([^A-Z])/', '\\\$1', $string); -#----------------------------- -// PHP does not have the \Q and \E string metacharacters -$string = "this is\\ a\\ test\\!"; -// PHP's quotemeta() function is not the same as perl's quotemeta() function -$string = preg_replace('/(\W)/', '\\\$1', 'is a test!'); -#----------------------------- - -# @@PLEAC@@_1.14 -#----------------------------- -$string = trim($string); -#----------------------------- -// print what's typed, but surrounded by > < symbols -while (!feof(STDIN)) { - print ">" . substr(fgets(STDIN), 0, -1) . "<\n"; -} -#----------------------------- -$string = preg_replace('/\s+/', ' ', $string); // finally, collapse middle -#----------------------------- -$string = trim($string); -$string = preg_replace('/\s+/', ' ', $string); -#----------------------------- -// 1. trim leading and trailing white space -// 2. collapse internal whitespace to single space each -function sub_trim($string) { - $string = trim($string); - $string = preg_replace('/\s+/', ' ', $string); - return $string; -} -#----------------------------- - -# @@PLEAC@@_1.15 -# @@INCOMPLETE@@ -# @@INCOMPLETE@@ - -# @@PLEAC@@_1.16 -#----------------------------- -$code = soundex($string); -#----------------------------- -$phoned_words = metaphone("Schwern"); -#----------------------------- -// substitution function for getpwent(): -// returns an array of user entries, -// each entry contains the username and the full name -function getpwent() { - $pwents = array(); - $handle = fopen("passwd", "r"); - while (!feof($handle)) { - $line = fgets($handle); - if (preg_match("/^#/", $line)) continue; - $cols = explode(":", $line); - $pwents[$cols[0]] = $cols[4]; - } - return $pwents; -} - -print "Lookup user: "; -$user = rtrim(fgets(STDIN)); -if (empty($user)) exit; -$name_code = soundex($user); -$pwents = getpwent(); -foreach($pwents as $username => $fullname) { - preg_match("/(\w+)[^,]*\b(\w+)/", $fullname, $matches); - list(, $firstname, $lastname) = $matches; - - if ($name_code == soundex($username) || - $name_code == soundex($lastname) || - $name_code == soundex($firstname)) - { - printf("%s: %s %s\n", $username, $firstname, $lastname); - } -} -#----------------------------- - -# @@PLEAC@@_1.17 -#----------------------------- -# @@INCLUDE@@ include/php/fixstyle.php -#----------------------------- -# @@INCLUDE@@ include/php/fixstyle2.php -#----------------------------- -// very fast, but whitespace collapse -while (!feof($input)) { - $i = 0; - preg_match("/^(\s*)(.*)/", fgets($input), $matches); // emit leading whitespace - fwrite($output, $matches[1]); - foreach (preg_split("/(\s+)/", $matches[2]) as $token) { // preserve trailing whitespace - fwrite($output, (array_key_exists($token, $config) ? $config[$token] : $token) . " "); - } - fwrite($output, "\n"); -} -#----------------------------- - -// @@PLEAC@@_2.0 -// As is the case under so many other languages floating point use under PHP is fraught -// with dangers. Although the basic techniques shown below are valid, please refer to -// the official PHP documentation for known issues, bugs, and alternate approaches - -// @@PLEAC@@_2.1 -// Two basic approaches to numeric validation: -// * Built-in functions like 'is_numeric', 'is_int', 'is_float' etc -// * Regexes, as shown below - -$s = '12.345'; - -preg_match('/\D/', $s) && die("has nondigits\n"); -preg_match('/^\d+$/', $s) || die("not a natural number\n"); -preg_match('/^-?\d+$/', $s) || die("not an integer\n"); -preg_match('/^[+-]?\d+$/', $s) || die("not an integer\n"); -preg_match('/^-?\d+\.?\d*$/', $s) || die("not a decimal\n"); -preg_match('/^-?(?:\d+(?:\.\d*)?|\.\d+)$/', $s) || die("not a decimal\n"); -preg_match('/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/', $s) || die("not a C float\n"); - -// ---------------------------- - -function getnum($s) -{ - sscanf($s, "%D", $number); return isset($number) ? $number : 0; -} - -echo getnum(123) . "\n"; // ok -echo getnum(0xff) . "\n"; // .. -echo getnum(044) . "\n"; // .. - -echo getnum('x') . "\n"; // fail - -// @@PLEAC@@_2.2 -// In PHP floating point comparisions are 'safe' [meaning the '==' comparison operator -// can be used] as long as the value consists of 14 digits or less [total digits, either -// side of the decimal point e.g. xxxxxxx.xxxxxxx, xxxxxxxxxxxxxx., .xxxxxxxxxxxxxx]. If -// values with more digits must be compared, then: -// -// * Represent as strings, and take care to avoid any implicit conversions e.g. don't pass -// a float as a float to a function and expect all digits to be retained - they won't be - -// then use 'strcmp' to compare the strings -// -// * Avoid float use; perhaps use arbitrary precision arithmetic. In this case, the -// 'bccomp' function is relevant - -// Will work as long as each floating point value is 14 digits or less -if ($float_1 == $float_2) -{ - ; // ... -} - -// Compare as strings -$cmp = strcmp('123456789.123456789123456789', '123456789.123456789123456788'); - -// Use 'bccomp' -$precision = 5; // Number of significant comparison digits after decimal point -if (bccomp('1.111117', '1.111116', $precision)) -{ - ; // ... -} - -$precision = 6; -if (bccomp('1.111117', '1.111116', $precision)) -{ - ; // ... -} - -// ---------------------------- - -$wage = 536; -$week = $wage * 40; -printf("One week's wage is: $%.2f\n", $week / 100); - -// @@PLEAC@@_2.3 -// Preferred approach -$rounded = round($unrounded, $precision); - -// Possible alternate approach -$format = '%[width].[prec]f'; -$rounded = sprintf($format, $unrounded); - -// ------------ - -$a = 0.255; $b = round($a, 2); -echo "Unrounded: {$a}\nRounded: {$b}\n"; - -$a = 0.255; $b = sprintf('%.2f', $a); -echo "Unrounded: {$a}\nRounded: {$b}\n"; - -$a = 0.255; -printf("Unrounded: %.f\nRounded: %.2f\n", $a, $a); - -// ---------------------------- - -echo "number\tint\tfloor\tceil\n"; - -foreach(array(3.3, 3.5, 3.7, -3.3) as $number) -{ - printf("%.1f\t%.1f\t%.1f\t%.1f\n", $number, (int) $number, floor($number), ceil($number)); -} - -// @@PLEAC@@_2.4 -// PHP offers the 'bindec' and 'decbin' functions to converting between binary and decimal - -$num = bindec('0110110'); - -$binstr = decbin(54); - -// @@PLEAC@@_2.5 -foreach (range($X, $Y) as $i) -{ - ; // ... -} - -foreach (range($X, $Y, 7) as $i) -{ - ; // ... -} - -for ($i = $X; $i <= $Y; $i++) -{ - ; // ... -} - -for ($i = $X; $i <= $Y; $i += 7) -{ - ; // ... -} - -// ---------------------------- - -echo 'Infancy is:'; foreach(range(0, 2) as $i) echo " {$i}\n"; -echo 'Toddling is:'; foreach(range(3, 4) as $i) echo " {$i}\n"; -echo 'Childhood is:'; foreach(range(5, 12) as $i) echo " {$i}\n"; - -// @@PLEAC@@_2.6 -// PHP offers no native support for Roman Numerals. However, a 'Numbers_Roman' class -// is available for download from PEAR: [http://pear.php.net/package/Numbers_Roman]. -// Note the following 'include' directives are required: -// -// include_once('Numbers/Roman.php'); - -$roman = Numbers_Roman::toNumeral($arabic); -$arabic = Numbers_Roman::toNumber($roman); - -// ---------------------------- - -$roman_fifteen = Numbers_Roman::toNumeral(15); - -$arabic_fifteen = Numbers_Roman::toNumber($roman_fifteen); - -printf("Roman for fifteen is: %s\n", $roman_fifteen); -printf("Arabic for fifteen is: %d\n", $arabic_fifteen); - -// @@PLEAC@@_2.7 -// Techniques used here simply mirror Perl examples, and are not an endorsement -// of any particular RNG technique - -// In PHP do this ... -$random = rand($lowerbound, $upperbound); -$random = rand($x, $y); - -// ---------------------------- - -function make_password($chars, $reqlen) -{ - $len = strlen($chars); - for ($i = 0; $i < $reqlen; $i++) $password .= substr($chars, rand(0, $len), 1); - return $password; -} - -$chars = 'ABCDEfghijKLMNOpqrstUVWXYz'; $reqlen = 8; - -$password = make_password($chars, $reqlen); - -// @@PLEAC@@_2.8 -// PHP sports a large number of C Standard Library routines including the 'srand' -// function, used to re-seed the RNG used with calls to the 'rand' function. Thus, -// as per Perl example: - -while (TRUE) -{ - $seed = (int) fgets(STDIN); - if (!empty($seed)) break; -} - -srand($seed); - -// @@PLEAC@@_2.9 -// The above is considered - for many reasons - a poor way of seeding the RNG. PHP -// also offers alternate versions of the functions, 'mt_srand' and 'mt_rand', -// which are described as faster, and more 'random', though key to obtaining a -// more 'random' distribution of generated numbers seems to be through using -// a combination of a previously saved random value in combination with an -// unrepeatable value [like the current time in microseconds] that is multiplied -// by a large prime number, or perhaps as part of a hash [examples available in -// PHP documentation for 'srand' and 'mt_srand'] - -mt_srand($saved_random_value + microtime() * 1000003); - -// or - -mt_srand(($saved_random_value + hexdec(substr(md5(microtime()), -8))) & 0x7fffffff); - -// Use of 'mt_rand' together with an appropriate seeding approach should help better -// approximate the generation of a 'truly random value' -$truly_random_value = mt_rand(); - -// @@PLEAC@@_2.10 -function random() { return (float) rand() / (float) getrandmax(); } - -function gaussian_rand() -{ - $u1 = 0.0; $u2 = 0.0; $g1 = 0.0; $g2 = 0.0; $w = 0.0; - - do - { - $u1 = 2.0 * random() - 1.0; $u2 = 2.0 * random() - 1.0; - $w = $u1 * $u1 + $u2 * $u2; - } while ($w > 1.0); - - $w = sqrt((-2.0 * log($w)) / $w); $g2 = $u1 * $w; $g1 = $u2 * $w; - - return $g1; -} - -// ------------ - -$mean = 25.0; $sdev = 2.0; -$salary = gaussian_rand() * $mean + $sdev; - -printf("You have been hired at: %.2f\n", $salary); - -// @@PLEAC@@_2.11 -// 'deg2rad' and 'rad2deg' are actually PHP built-ins, but here is how you might implement -/ them if needed -function deg2rad_($deg) { return ($deg / 180.0) * M_PI; } -function rad2deg_($rad) { return ($rad / M_PI) * 180.0; } - -// ------------ - -printf("%f\n", deg2rad_(180.0)); -printf("%f\n", deg2rad(180.0)); - -// ---------------------------- - -function degree_sin($deg) { return sin(deg2rad($deg)); } - -// ------------ - -$rad = deg2rad(380.0); - -printf("%f\n", sin($rad)); -printf("%f\n", degree_sin(380.0)); - -// @@PLEAC@@_2.12 -function my_tan($theta) { return sin($theta) / cos($theta); } - -// ------------ - -$theta = 3.7; - -printf("%f\n", my_tan($theta)); -printf("%f\n", tan($theta)); - -// @@PLEAC@@_2.13 -$value = 100.0; - -$log_e = log($value); -$log_10 = log10($value); - -// ---------------------------- - -function log_base($base, $value) { return log($value) / log($base); } - -// ------------ - -$answer = log_base(10.0, 10000.0); - -printf("log(10, 10,000) = %f\n", $answer); - -// @@PLEAC@@_2.14 -// PHP offers no native support for matrices. However, a 'Math_Matrix' class -// is available for download from PEAR: [http://pear.php.net/package/Math_Matrix]. -// Note the following 'include' directives are required: -// -// include_once('Math/Matrix.php'); - -$a = new Math_Matrix(array(array(3, 2, 3), array(5, 9, 8))); -$b = new Math_Matrix(array(array(4, 7), array(9, 3), array(8, 1))); - -echo $a->toString() . "\n"; -echo $b->toString() . "\n"; - -// NOTE: When I installed this package I had to rename the 'clone' method else -// it would not load, so I chose to rename it to 'clone_', and this usage is -// shown below. This bug may well be fixed by the time you obtain this package - -$c = $a->clone_(); -$c->multiply($b); - -echo $c->toString() . "\n"; - -// @@PLEAC@@_2.15 -// PHP offers no native support for complex numbers. However, a 'Math_Complex' class -// is available for download from PEAR: [http://pear.php.net/package/Math_Complex]. -// Note the following 'include' directives are required: -// -// include_once('Math/Complex.php'); -// include_once('Math/TrigOp.php'); -// include_once('Math/ComplexOp.php'); - -$a = new Math_Complex(3, 5); -$b = new Math_Complex(2, -2); - -$c = Math_ComplexOp::mult($a, $b); - -echo $c->toString() . "\n"; - -// ---------------------------- - -$d = new Math_Complex(3, 4); -$r = Math_ComplexOp::sqrt($d); - -echo $r->toString() . "\n"; - -// @@PLEAC@@_2.16 -// Like C, PHP supports decimal-alternate notations. Thus, for example, the integer -// value, 867, is expressable in literal form as: -// -// Hexadecimal -> 0x363 -// Octal -> 01543 -// -// For effecting such conversions using strings there is 'sprintf' and 'sscanf'. - -$dec = 867; -$hex = sprintf('%x', $dec); -$oct = sprintf('%o', $dec); - -// ------------ - -$dec = 0; -$hex = '363'; - -sscanf($hex, '%x', $dec); - -// ------------ - -$dec = 0; -$oct = '1543'; - -sscanf($oct, '%o', $dec); - -// ---------------------------- - -$number = 0; - -printf('Gimme a number in decimal, octal, or hex: '); -sscanf(fgets(STDIN), '%D', $number); - -printf("%d %x %o\n", $number, $number, $number); - -// @@PLEAC@@_2.17 -// PHP offers the 'number_format' built-in function to, among many other format tasks, -// commify numbers. Perl-compatible [as well as extended] regexes are also available - -function commify_series($s) { return number_format($s, 0, '', ','); } - -// ------------ - -$hits = 3456789; - -printf("Your website received %s accesses last month\n", commify_series($hits)); - -// ---------------------------- - -function commify($s) -{ - return strrev(preg_replace('/(\d\d\d)(?=\d)(?!\d*\.)/', '${1},', strrev($s))); -} - -// ------------ - -$hits = 3456789; - -echo commify(sprintf("Your website received %d accesses last month\n", $hits)); - -// @@PLEAC@@_2.18 -function pluralise($value, $root, $singular='' , $plural='s') -{ - return $root . (($value > 1) ? $plural : $singular); -} - -// ------------ - -$duration = 1; -printf("It took %d %s\n", $duration, pluralise($duration, 'hour')); -printf("%d %s %s enough.\n", $duration, pluralise($duration, 'hour'), - pluralise($duration, '', 'is', 'are')); - -$duration = 5; -printf("It took %d %s\n", $duration, pluralise($duration, 'hour')); -printf("%d %s %s enough.\n", $duration, pluralise($duration, 'hour'), - pluralise($duration, '', 'is', 'are')); - -// ---------------------------- - -function plural($singular) -{ - $s2p = array('/ss$/' => 'sses', '/([psc]h)$/' => '${1}es', '/z$/' => 'zes', - '/ff$/' => 'ffs', '/f$/' => 'ves', '/ey$/' => 'eys', - '/y$/' => 'ies', '/ix$/' => 'ices', '/([sx])$/' => '$1es', - '$' => 's'); - - foreach($s2p as $s => $p) - { - if (preg_match($s, $singular)) return preg_replace($s, $p, $singular); - } -} - -// ------------ - -foreach(array('mess', 'index', 'leaf', 'puppy') as $word) -{ - printf("%6s -> %s\n", $word, plural($word)); -} - -// @@PLEAC@@_2.19 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_3.0 -// PHP's date / time suport is quite extensive, and appears grouped into three areas of -// functionality: -// -// * UNIX / C Library [libc]-based routines, which include [among others]: -// - localtime, gmtime -// - strftime, strptime, mktime -// - time, getdate, gettimeofday, -// -// * PHP 'native' functions, those date / time routines released in earlier versions, -// and which otherwise provide 'convenience' functionality; these include: -// - date -// - strtotime -// -// * 'DateTime' class-based. This facility appears [according to the PHP documentation] -// to be extremely new / experimental, so whilst usage examples will be provided, they -// should not be taken to be 'official' examples, and obviously, subject to change. -// My own impression is that this facility is currently only partially implemented, -// so there is limited use for these functions. The functions included in this group -// are some of the 'date_'-prefixed functions; they are, however, not used standalone, -// but as methods in conjunction with an object. Typical usage: -// -// $today = new DateTime(); // actually calls: date_create($today, ...); -// echo $today->format('U') . "\n"; // actually calls: date_format($today, ...); -// -// Also worth mentioning is the PEAR [PHP Extension and Repository] package, 'Calendar', -// which offers a rich set of date / time manipulation facilities. However, since it is -// not currently shipped with PHP, no examples appear - -// Helper functions for performing date arithmetic - -function dateOffset() -{ - static $tbl = array('sec' => 1, 'min' => 60, 'hou' => 3600, 'day' => 86400, 'wee' => 604800); - $delta = 0; - - foreach (func_get_args() as $arg) - { - $kv = explode('=', $arg); - $delta += $kv[1] * $tbl[strtolower(substr($kv[0], 0, 3))]; - } - - return $delta; -} - -function dateInterval($intvltype, $timevalue) -{ - static $tbl = array('sec' => 1, 'min' => 60, 'hou' => 3600, 'day' => 86400, 'wee' => 604800); - return (int) round($timevalue / $tbl[strtolower(substr($intvltype, 0, 3))]); -} - -// ---------------------------- - -// Extract indexed array from 'getdate' -$today = getdate(); -printf("Today is day %d of the current year\n", $today['yday']); - -// Extract indexed, and associative arrays, respectively, from 'localtime' -$today = localtime(); -printf("Today is day %d of the current year\n", $today[7]); - -$today = localtime(time(), TRUE); -printf("Today is day %d of the current year\n", $today['tm_yday']); - -// @@PLEAC@@_3.1 -define(SEP, '-'); - -// ------------ - -$today = getdate(); - -$day = $today['mday']; -$month = $today['mon']; -$year = $today['year']; - -// Either do this to use interpolation: -$sep = SEP; -echo "Current date is: {$year}{$sep}{$month}{$sep}{$day}\n"; - -// or simply concatenate: -echo 'Current date is: ' . $year . SEP . $month . SEP . $day . "\n"; - -// ------------ - -$today = localtime(time(), TRUE); - -$day = $today['tm_mday']; -$month = $today['tm_mon'] + 1; -$year = $today['tm_year'] + 1900; - -printf("Current date is: %4d%s%2d%s%2d\n", $year, SEP, $month, SEP, $day); - -// ------------ - -$format = 'Y' . SEP . 'n' . SEP . 'd'; - -$today = date($format); - -echo "Current date is: {$today}\n"; - -// ------------ - -$sep = SEP; - -$today = strftime("%Y$sep%m$sep%d"); - -echo "Current date is: {$today}\n"; - -// @@PLEAC@@_3.2 -$timestamp = mktime($hour, $min, $sec, $month, $day, $year); - -$timestamp = gmmktime($hour, $min, $sec, $month, $day, $year); - -// @@PLEAC@@_3.3 -$dmyhms = getdate(); // timestamp: current date / time - -$dmyhms = getdate($timestamp); // timestamp: arbitrary - -$day = $dmyhms['mday']; -$month = $dmyhms['mon']; -$year = $dmyhms['year']; - -$hours = $dmyhms['hours']; -$minutes = $dmyhms['minutes']; -$seconds = $dmyhms['seconds']; - -// @@PLEAC@@_3.4 -// Date arithmetic is probably most easily performed using timestamps [i.e. *NIX Epoch -// Seconds]. Dates - in whatever form - are converted to timestamps, these are -// arithmetically manipulated, and the result converted to whatever form required. -// Note: use 'mktime' to create timestamps properly adjusted for daylight saving; whilst -// 'strtotime' is more convenient to use, it does not, AFAIK, include this adjustment - -$when = $now + $difference; -$then = $now - $difference; - -// ------------ - -$now = mktime(0, 0, 0, 8, 6, 2003); - -$diff1 = dateOffset('day=1'); $diff2 = dateOffset('weeks=2'); - -echo 'Today is: ' . date('Y-m-d', $now) . "\n"; -echo 'One day in the future is: ' . date('Y-m-d', $now + $diff1) . "\n"; -echo 'Two weeks in the past is: ' . date('Y-m-d', $now - $diff2) . "\n"; - -// ---------------------------- - -// Date arithmetic performed using a custom function, 'dateOffset'. Internally, offset may -// be computed in one of several ways: -// * Direct timestamp manipulation - fastest, but no daylight saving adjustment -// * Via 'date' built-in function - slower [?], needs a base time from which to -// compute values, but has daylight saving adjustment -// * Via 'strtotime' built-in function - as for 'date' -// * Via 'DateTime' class -// -// Approach used here is to utilise direct timestamp manipulation in 'dateOffset' [it's -// performance can also be improved by replacing $tbl with a global definition etc], -// and to illustrate how the other approaches might be used - -// 1. 'dateOffset' - -$birthtime = mktime(3, 45, 50, 1, 18, 1973); - -$interval = dateOffset('day=55', 'hours=2', 'min=17', 'sec=5'); - -$then = $birthtime + $interval; - -printf("Birthtime is: %s\nthen is: %s\n", date(DATE_RFC1123, $birthtime), date(DATE_RFC1123, $then)); - -// ------------ - -// 2. 'date' - -// Base values, and offsets, respectively -$hr = 3; $min = 45; $sec = 50; $mon = 1; $day = 18; $year = 1973; - -$yroff = 0; $monoff = 0; $dayoff = 55; $hroff = 2; $minoff = 17; $secoff = 5; - -// Base date -$birthtime = mktime($hr, $min, $sec, $mon, $day, $year, TRUE); - -$year = date('Y', $birthtime) + $yroff; -$mon = date('m', $birthtime) + $monoff; -$day = date('d', $birthtime) + $dayoff; - -$hr = date('H', $birthtime) + $hroff; -$min = date('i', $birthtime) + $minoff; -$sec = date('s', $birthtime) + $secoff; - -// Offset date -$then = mktime($hr, $min, $sec, $mon, $day, $year, TRUE); - -printf("Birthtime is: %s\nthen is: %s\n", date(DATE_RFC1123, $birthtime), date(DATE_RFC1123, $then)); - -// ------------ - -// 3. 'strtotime' - -// Generate timestamp whatever way is preferable -$birthtime = mktime(3, 45, 50, 1, 18, 1973); -$birthtime = strtotime('1/18/1973 03:45:50'); - -$then = strtotime('+55 days 2 hours 17 minutes 2 seconds', $birthtime); - -printf("Birthtime is: %s\nthen is: %s\n", date(DATE_RFC1123, $birthtime), date(DATE_RFC1123, $then)); - -// ------------ - -// 4. 'DateTime' class - -$birthtime = new DateTime('1/18/1973 03:45:50'); -$then = new DateTime('1/18/1973 03:45:50'); -$then->modify('+55 days 2 hours 17 minutes 2 seconds'); - -printf("Birthtime is: %s\nthen is: %s\n", $birthtime->format(DATE_RFC1123), $then->format(DATE_RFC1123)); - -// @@PLEAC@@_3.5 -// Date intervals are most easily computed using timestamps [i.e. *NIX Epoch -// Seconds] which, of course, gives the interval result is seconds from which -// all other interval measures [days, weeks, months, years] may be derived. -// Refer to previous section for discussion of daylight saving and other related -// problems - -$interval_seconds = $recent - $earlier; - -// ---------------------------- - -// Conventional approach ... -$bree = strtotime('16 Jun 1981, 4:35:25'); -$nat = strtotime('18 Jan 1973, 3:45:50'); - -// ... or, with daylight saving adjustment -$bree = mktime(4, 35, 25, 6, 16, 1981, TRUE); -$nat = mktime(3, 45, 50, 1, 18, 1973, TRUE); - -$difference = $bree - $nat; - -// 'dateInterval' custom function computes intervals in several measures given an -// interval in seconds. Note, 'month' and 'year' measures not provided -printf("There were %d seconds between Nat and Bree\n", $difference); -printf("There were %d weeks between Nat and Bree\n", dateInterval('weeks', $difference)); -printf("There were %d days between Nat and Bree\n", dateInterval('days', $difference)); -printf("There were %d hours between Nat and Bree\n", dateInterval('hours', $difference)); -printf("There were %d minutes between Nat and Bree\n", dateInterval('mins', $difference)); - -// @@PLEAC@@_3.6 -// 'getdate' accepts a timestamp [or implicitly calls 'time'] and returns an array of -// date components. It returns much the same information as 'strptime' except that -// the component names are different - -$today = getdate(); - -$weekday = $today['wday']; -$monthday = $today['mday']; -$yearday = $today['yday']; - -$weeknumber = (int) round($yearday / 7.0); - -// Safter method of obtaining week number -$weeknumber = strftime('%U') + 1; - -// ---------------------------- - -define(SEP, '/'); - -$day = 16; -$month = 6; -$year = 1981; - -$timestamp = mktime(0, 0, 0, $month, $day, $year); - -$date = getdate($timestamp); - -$weekday = $date['wday']; -$monthday = $date['mday']; -$yearday = $date['yday']; - -$weeknumber = (int) round($yearday / 7.0); - -$weeknumber = strftime('%U', $timestamp) + 1; - -// Interpolate ... -$sep = SEP; -echo "{$month}{$sep}{$day}{$sep}{$year} was a {$date['weekday']} in week {$weeknumber}\n"; - -// ... or, concatenate -echo $month . SEP . $day . SEP . $year . ' was a ' . $date['weekday'] - . ' in week ' . $weeknumber . "\n"; - -// @@PLEAC@@_3.7 -// 'strtotime' parses a textual date expression by attempting a 'best guess' at -// the format, and either fails, or generates a timestamp. Timestamp could be fed -// into any one of the various functions; example: -$timestamp = strtotime('1998-06-03'); echo strftime('%Y-%m-%d', $timestamp) . "\n"; - -// 'strptime' parses a textual date expression according to a specified format, -// and returns an array of date components; components can be easily dumped -print_r(strptime('1998-06-03', '%Y-%m-%d')); - -// ---------------------------- - -// Parse date string according to format -$darr = strptime('1998-06-03', '%Y-%m-%d'); - -if (!empty($darr)) -{ - // Show date components in 'debug' form - print_r($darr); - - // Check whether there was a parse error i.e. one or more components could not - // be extracted from the string - if (empty($darr['unparsed'])) - { - // Properly parsed date, so validate required components using, 'checkdate' - if (checkdate($darr['tm_mon'] + 1, $darr['tm_mday'], $darr['tm_year'] + 1900)) - echo "Parsed date verified as correct\n"; - else - echo "Parsed date failed verification\n"; - } - else - { - echo "Date string parse not complete; failed components: {$darr['unparsed']}\n"; - } -} -else -{ - echo "Date string could not be parsed\n"; -} - -// @@PLEAC@@_3.8 -// 'date' and 'strftime' both print a date string based on: -// * Format String, describing layout of date components -// * Timestamp [*NIX Epoch Seconds], either given explicitly, or implictly -// via a call to 'time' which retrieves current time value - -$ts = 1234567890; - -date('Y/m/d', $ts); -date('Y/m/d', mktime($h, $m, $s, $mth, $d, $y, $is_dst)); - -date('Y/m/d'); // same as: date('Y/m/d', time()); - -// ------------ - -$ts = 1234567890; - -strftime('%Y/%m/%d', $ts); -strftime('%Y/%m/%d', mktime($h, $m, $s, $mth, $d, $y, $is_dst)); - -strftime('%Y/%m/%d'); // same as: strftime('%Y/%m/%d', time()); - -// ---------------------------- - -// 'mktime' creates a local time timestamp -$t = strftime('%a %b %e %H:%M:%S %z %Y', mktime(3, 45, 50, 1, 18, 73, TRUE)); -echo "{$t}\n"; - -// 'gmmktime' creates a GMT time timestamp -$t = strftime('%a %b %e %H:%M:%S %z %Y', gmmktime(3, 45, 50, 1, 18, 73)); -echo "{$t}\n"; - -// ---------------------------- - -// 'strtotime' parses a textual date expression, and generates a timestamp -$t = strftime('%A %D', strtotime('18 Jan 1973, 3:45:50')); -echo "{$t}\n"; - -// This should generate output identical to previous example -$t = strftime('%A %D', mktime(3, 45, 50, 1, 18, 73, TRUE)); -echo "{$t}\n"; - -// @@PLEAC@@_3.9 -// PHP 5 and above can use the built-in, 'microtime'. Crude implementation for ealier versions: -// function microtime() { $t = gettimeofday(); return (float) ($t['sec'] + $t['usec'] / 1000000.0); } - -// ------------ - -$before = microtime(); - -$line = fgets(STDIN); - -$elapsed = microtime() - $before; - -printf("You took %.3f seconds\n", $elapsed); - -// ------------ - -define(NUMBER_OF_TIMES, 100); -define(SIZE, 500); - -for($i = 0; $i < NUMBER_OF_TIMES; $i++) -{ - $arr = array(); - for($j = 0; $j < SIZE; $j++) $arr[] = rand(); - - $begin = microtime(); - sort($arr); - $elapsed = microtime() - $begin; - - $total_time += $elapsed; -} - -printf("On average, sorting %d random numbers takes %.5f seconds\n", SIZE, $total_time / (float) NUMBER_OF_TIMES); - -// @@PLEAC@@_3.10 -// Low-resolution: sleep time specified in seconds -sleep(1); - -// High-resolution: sleep time specified in microseconds [not reliable under Windows] -usleep(250000); - -// @@PLEAC@@_3.11 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_4.0 -// Nested arrays are supported, and may be easily printed using 'print_r' - -$nested = array('this', 'that', 'the', 'other'); - -$nested = array('this', 'that', array('the', 'other')); print_r($nested); - -$tune = array('The', 'Star-Spangled', 'Banner'); - -// @@PLEAC@@_4.1 -// PHP offers only the 'array' type which is actually an associative array, though -// may be numerically indexed, to mimic vectors and matrices; there is no separate -// 'list' type - -$a = array('quick', 'brown', 'fox'); - -// ------------ - -$a = escapeshellarg('Why are you teasing me?'); - -// ------------ - -$lines = <<<END_OF_HERE_DOC - The boy stood on the burning deck, - it was as hot as glass. -END_OF_HERE_DOC; - -// ------------ - -$bigarray = array_map('rtrim', file('mydatafile')); - -// ------------ - -$banner = 'The mines of Moria'; - -$banner = escapeshellarg('The mines of Moria'); - -// ------------ - -$name = 'Gandalf'; - -$banner = "Speak {$name}, and enter!"; - -$banner = 'Speak ' . escapeshellarg($name) . ' and welcome!'; - -// ------------ - -$his_host = 'www.perl.com'; - -$host_info = `nslookup $his_host`; - -$cmd = 'ps ' . posix_getpid(); $perl_info = `$cmd`; - -$shell_info = `ps $$`; - -// ------------ - -$banner = array('Costs', 'only', '$4.95'); - -$banner = array_map('escapeshellarg', split(' ', 'Costs only $4.95')); - -// ------------ - -// AFAIK PHP doesn't support non-quoted strings ala Perl's 'q', 'qq' and 'qw', so arrays -// created from strings must use quoted strings, and make use of 'split' [or equivalent]. -// A slew of functions exist for performing string quoting, including 'escapeshellarg', -// 'quotemeta', and 'preg_quote' - -$brax = split(' ', '( ) < > { } [ ]'); - -// Do this to quote each element within '..' -// $brax = array_map('escapeshellarg', split(' ', '( ) < > { } [ ]')); - -$rings = split(' ', 'Nenya Narya Vilya'); - -$tags = split(' ', 'LI TABLE TR TD A IMG H1 P'); - -$sample = split(' ', 'The vertical bar | looks and behaves like a pipe.'); - -// @@PLEAC@@_4.2 -function commify_series($list) -{ - $n = str_word_count($list); $series = str_word_count($list, 1); - - if ($n == 0) return NULL; - if ($n == 1) return $series[0]; - if ($n == 2) return $series[0] . ' and ' . $series[1]; - - return join(', ', array_slice($series, 0, -1)) . ', and ' . $series[$n - 1]; -} - -// ------------ - -echo commify_series('red') . "\n"; -echo commify_series('red yellow') . "\n"; -echo commify_series('red yellow green') . "\n"; - -$mylist = 'red yellow green'; -echo 'I have ' . commify_series($mylist) . " marbles.\n"; - -// ---------------------------- - -function commify_series($arr) -{ - $n = count($arr); $sepchar = ','; - - foreach($arr as $str) - { - if (strpos($str, ',') === false) continue; - $sepchar = ';'; break; - } - - if ($n == 0) return NULL; - if ($n == 1) return $arr[0]; - if ($n == 2) return $arr[0] . ' and ' . $arr[1]; - - return join("{$sepchar} ", array_slice($arr, 0, -1)) . "{$sepchar} and " . $arr[$n - 1]; -} - -// ------------ - -$lists = array( - array('just one thing'), - split(' ', 'Mutt Jeff'), - split(' ', 'Peter Paul Mary'), - array('To our parents', 'Mother Theresa', 'God'), - array('pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna'), - array('recycle tired, old phrases', 'ponder big, happy thoughts'), - array('recycle tired, old phrases', 'ponder big, happy thoughts', 'sleep and dream peacefully')); - -foreach($lists as $arr) -{ - echo 'The list is: ' . commify_series($arr) . ".\n"; -} - -// @@PLEAC@@_4.3 -// AFAICT you cannot grow / shrink an array to an arbitrary size. However, you can: -// * Grow an array by appending an element using subscrip notation, or using -// either 'array_unshift' or 'array_push' to add one or more elements - -$arr[] = 'one'; -array_unshift($arr, 'one', 'two', 'three'); -array_push($arr, 'one', 'two', 'three'); - -// * Shrink an array by using 'unset' to remove one or more specific elements, or -// either 'array_shift' or 'array_pop' to remove an element from the ends - -unset($arr[$idx1], $arr[$idx2], $arr[$idx3]); -$item = array_shift($arr); -$item = array_pop($arr); - -// ---------------------------- - -function what_about_the_array() -{ - global $people; - - echo 'The array now has ' . count($people) . " elements\n"; - echo 'The index value of the last element is ' . (count($people) - 1) . "\n"; - echo 'Element #3 is ' . $people[3] . "\n"; -} - -$people = array('Crosby', 'Stills', 'Nash', 'Young'); -what_about_the_array(); - -array_pop($people); -what_about_the_array(); - -// Cannot, AFAICT, resize the array to an arbitrary size - -# @@PLEAC@@_4.4 -foreach ($list as $item) { - // do something with $item -} - -// Environment listing example - -// PHP defines a superglobal $_ENV to provide access to environment -// variables. - -// Beware, array assignment means copying in PHP. You need to use -// the reference operator to avoid copying. But we want a copy here. -$env = $_ENV; - -// PHP can sort an array by key, so you don't need to get keys, -// and then sort. -ksort($env); - -foreach ($env as $key => $value) { - echo "{$key}={$value}\n"; -} - -// Literal translation of Perl example would be: -$keys = array_keys($_ENV); -sort($keys); -foreach ($keys as $key) { - echo "{$key}={$_ENV[$key]}\n"; -} - -// This assumes that MAX_QUOTA is a named constant. -foreach ($all_users as $user) { - $disk_space = get_usage($user); - if ($disk_space > MAX_QUOTA) { - complain($user); - } -} - -// You can't modify array's elements in-place. -$array = array(1, 2, 3); -$newarray = array(); -foreach ($array as $item) { - $newarray[] = $item - 1; -} -print_r($newarray); - -// Before PHP 5, that is. You can precede the reference operator -// before $item to get reference instead of copy. -$array = array(1, 2, 3); -foreach ($array as &$item) { - $item--; -} -print_r($array); - -// TODO: explain the old each() and list() iteration construct. -// foreach is new in PHP 4, and there are subtle differences. - -// @@PLEAC@@_4.5 -// Conventional 'read-only' access -foreach($array as $item) -{ - ; // Can access, but not update, array element referred to by '$item' -} - -// ---- - -// '&' makes '$item' a reference -foreach($array as &$item) -{ - ; // Update array element referred to by '$item' -} - -// ------------ - -$arraylen = count($array); - -for($i = 0; $i < $arraylen; $i++) -{ - ; // '$array' is updateable via subscript notation -} - -// ---------------------------- - -$fruits = array('Apple', 'Raspberry'); - -foreach($fruits as &$fruit) -{ - echo "{$fruit} tastes good in a pie.\n"; -} - -$fruitlen = count($fruits); - -for($i = 0; $i < $fruitlen; $i++) -{ - echo "{$fruits[$i]} tastes good in a pie.\n"; -} - -// ---------------------------- - -$rogue_cats = array('Blackie', 'Goldie', 'Silkie'); - -// Take care to assign reference to '$rogue_cats' array via '=&' -$namelist['felines'] =& $rogue_cats; - -// Take care to make '$cat' a reference via '&$' to allow updating -foreach($namelist['felines'] as &$cat) -{ - $cat .= ' [meow]'; -} - -// Via array reference -foreach($namelist['felines'] as $cat) -{ - echo "{$cat} purrs hypnotically.\n"; -} - -echo "---\n"; - -// Original array -foreach($rogue_cats as $cat) -{ - echo "{$cat} purrs hypnotically.\n"; -} - -// @@PLEAC@@_4.6 -// PHP offers the 'array_unique' function to perform this task. It works with both keyed, -// and numerically-indexed arrays; keys / indexes are preserved; use of 'array_values' -// is recommended to reindex numerically-indexed arrays since there will likely be missing -// indexes - -// Remove duplicate values -$unique = array_unique($array); - -// Remove duplicates, and reindex [for numerically-indexed arrays only] -$unique = array_values(array_unique($array)); - -// or use: -$unique = array_keys(array_flip($array)); - -// ---------------------------- - -// Selected Perl 'seen' examples -foreach($list as $item) -{ - if (!isset($seen[$item])) - { - $seen[$item] = TRUE; - $unique[] = $item; - } -} - -// ------------ - -foreach($list as $item) -{ - $seen[$item] || (++$seen[$item] && ($unique[] = $item)); -} - -// ------------ - -function some_func($item) -{ - ; // Do something with '$item' -} - -foreach($list as $item) -{ - $seen[$item] || (++$seen[$item] && some_func($item)); -} - -// ---------------------------- - -foreach(array_slice(preg_split('/\n/', `who`), 0, -1) as $user_entry) -{ - $user = preg_split('/\s/', $user_entry); - $ucnt[$user[0]]++; -} - -ksort($ucnt); - -echo "users logged in:\n"; - -foreach($ucnt as $user => $cnt) -{ - echo "\t{$user} => {$cnt}\n"; -} - -// @@PLEAC@@_4.7 -// PHP offers the 'array_diff' and 'array_diff_assoc' functions to perform this task. Same -// points as made about 'array_unique' apply here also - -$a = array('c', 'a', 'b', 'd'); -$b = array('c', 'a', 'b', 'e'); - -$diff = array_diff($a, $b); // $diff -> [3] 'd' -$diff = array_diff($b, $a); // $diff -> [3] 'e' - -// Numerically-indexed array, reindexed -$diff = array_values(array_diff($a, $b)); // $diff -> [0] 'd' -$diff = array_values(array_diff($b, $a)); // $diff -> [0] 'e' - -// ---------------------------- - -// 1st Perl 'seen' example only - -$a = array('k1' => 11, 'k2' => 12, 'k4' => 14); -$b = array('k1' => 11, 'k2' => 12, 'k3' => 13); - -foreach($b as $item => $value) { $seen[$item] = 1; } - -// Stores key only e.g. $aonly[0] contains 'k4', same as Perl example -foreach($a as $item => $value) { if (!$seen[$item]) $aonly[] = $item; } - -// Stores key and value e.g. $aonly['k4'] contains 14, same entry as in $a -foreach($a as $item => $value) { if (!$seen[$item]) $aonly[$item] = $value; } - -// ---------------------------- - -// Conventional way: $hash = array('key1' => 1, 'key2' => 2); - -$hash['key1'] = 1; -$hash['key2'] = 2; - -$hash = array_combine(array('key1', 'key2'), array(1, 2)); - -// ------------ - -$seen = array_slice($b, 0); - -$seen = array_combine(array_keys($b), array_fill(0, count($b), 1)); - -// @@PLEAC@@_4.8 -// PHP offers a number of array-based 'set operation' functions: -// * union: array_unique(array_merge(...)) -// * intersection: array_intersect and family -// * difference: array_diff and family -// which may be used for this type of task. Also, if returned arrays need to be -// reindexed, 'array_slice($array, 0)', or 'array_values($array)' are useful - -$a = array(1, 3, 5, 6, 7, 8); -$b = array(2, 3, 5, 7, 9); - -$union = array_values(array_unique(array_merge($a, $b))); // 1, 2, 3, 5, 6, 7, 8, 9 -$isect = array_values(array_intersect($a, $b)); // 3, 5, 7 -$diff = array_values(array_diff($a, $b)); // 1, 8 - -// @@PLEAC@@_4.9 -// PHP offers the 'array_merge' function to perform this task. Duplicate values are retained, -// but if arrays are numerically-indexed, resulting array is reindexed - -$arr1 = array('c', 'a', 'b', 'd'); -$arr2 = array('c', 'a', 'b', 'e'); - -$new = array_merge($arr1, $arr2); // $new -> 'c', 'a', 'b', 'd', 'c', 'a', 'b', 'd' - -// ---------------------------- - -$members = array('Time', 'Flies'); -$initiates = array('An', 'Arrow'); - -$members = array_merge($members, $initiates); - -// ------------ - -$members = array('Time', 'Flies'); -$initiates = array('An', 'Arrow'); - -// 'array_splice' is the PHP equivalent to Perl's 'splice' -array_splice($members, 2, 0, array_merge(array('Like'), $initiates)); -echo join(' ', $members) . "\n"; - -array_splice($members, 0, 1, array('Fruit')); -array_splice($members, -2, 2, array('A', 'Banana')); -echo join(' ', $members) . "\n"; - -// @@PLEAC@@_4.10 -$reversed = array_reverse($array); - -// ---------------------------- - -foreach(array_reverse($array) as $item) -{ - ; // ... do something with '$item' ... -} - -// ------------ - -for($i = count($array) - 1; $i >= 0; $i--) -{ - ; // ... do something with '$array[$i]' ... -} - -// ---------------------------- - -sort($array); -$array = array_reverse($array); - -// ------------ - -rsort($array); - -// @@PLEAC@@_4.11 -// Array elements can be deleted using 'unset'; removing several elements would require applying -// 'unset' several times, probably in a loop. However, they would most likely also need to be -// reindexed, so a better approach would be to use 'array_slice' which avoids explicit looping. -// Where elements need to be removed, and those elements also returned, it is probably best to -// combine both operations in a function. This is the approach taken here in implementing both -// 'shiftN' and 'popN', and it is these functions that are used in the examples - -function popN(&$arr, $n) -{ - $ret = array_slice($arr, -($n), $n); - $arr = array_slice($arr, 0, count($arr) - $n); - return $ret; -} - -function shiftN(&$arr, $n) -{ - $ret = array_slice($arr, 0, $n); - $arr = array_slice($arr, $n); - return $ret; -} - -// ------------ - -// Remove $n elements from the front of $array; return them in $fron -$front = shiftN($array, $n); - -// Remove $n elements from the end of $array; return them in $end -$end = popN($array, $n); - -// ------------ - -$friends = array('Peter', 'Paul', 'Mary', 'Jim', 'Tim'); - -list($this_, $that) = shiftN($friends, 2); - -echo "{$this_} {$that}\n"; - -// ------------ - -$beverages = array('Dew', 'Jolt', 'Cola', 'Sprite', 'Fresca'); - -$pair = popN($beverages, 2); - -echo join(' ', $pair) . "\n"; - -// @@PLEAC@@_4.12 -// This section illustrates various 'find first' techniques. The Perl examples all use an -// explicit loop and condition testing [also repeated here]. This is the simplest, and -// [potentially] most efficient approach because the search can be terminated as soon as a -// match is found. However, it is worth mentioning a few alternatives: -// * 'array_search' performs a 'find first' using the element value rather than a condition -// check, so isn't really applicable here -// * 'array_filter', whilst using a condition check, actually performs a 'find all', though -// all but the first returned element can be discarded. This approach is actually less error -// prone than using a loop, but the disadvantage is that each element is visited: there is no -// means of terminating the search once a match has been found. It would be nice if this -// function were to have a third parameter, a Boolean flag indicating whether to traverse -// the whole array, or quit after first match [next version, maybe :) ?] - -$found = FALSE; - -foreach($array as $item) -{ - // Not found - skip to next item - if (!$criterion) continue; - - // Found - save and leave - $match = $item; - $found = TRUE; - break; -} - -if ($found) -{ - ; // do something with $match -} -else -{ - ; // not found -} - -// ------------ - -function predicate($element) -{ - if (criterion) return TRUE; - return FALSE; -} - -$match = array_slice(array_filter($array, 'predicate'), 0, 1); - -if ($match) -{ - ; // do something with $match[0] -} -else -{ - ; // $match is empty - not found -} - -// ---------------------------- - -class Employee -{ - public $name, $age, $ssn, $salary; - - public function __construct($name, $age, $ssn, $salary, $category) - { - $this->name = $name; - $this->age = $age; - $this->ssn = $ssn; - $this->salary = $salary; - $this->category = $category; - } -} - -// ------------ - -$employees = array( - new Employee('sdf', 27, 12345, 47000, 'Engineer'), - new Employee('ajb', 32, 12376, 51000, 'Programmer'), - new Employee('dgh', 31, 12355, 45000, 'Engineer')); - -// ------------ - -function array_update($arr, $lambda, $updarr) -{ - foreach($arr as $key) $lambda($updarr, $key); - return $updarr; -} - -function highest_salaried_engineer(&$arr, $employee) -{ - static $highest_salary = 0; - - if ($employee->category == 'Engineer') - { - if ($employee->salary > $highest_salary) - { - $highest_salary = $employee->salary; - $arr[0] = $employee; - } - } -} - -// ------------ - -// 'array_update' custom function is modelled on 'array_reduce' except that it allows the -// return of an array, contents and length of which are entirely dependant on what the -// callback function does. Here, it is logically working in a 'find first' capacity -$highest_salaried_engineer = array_update($employees, 'highest_salaried_engineer', array()); - -echo 'Highest paid engineer is: ' . $highest_salaried_engineer[0]->name . "\n"; - -// @@PLEAC@@_4.13 -// PHP implements 'grep' functionality [as embodied in the current section] in the 'array_filter' -// function - -function predicate($element) -{ - if (criterion) return TRUE; - return FALSE; -} - -$matching = array_filter($list, 'predicate'); - -// ------------ - -$bigs = array_filter($nums, create_function('$n', 'return $n > 1000000;')); - -// ------------ - -function is_pig($user) -{ - $user_details = preg_split('/(\s)+/', $user); - // Assuming field 5 is the resource being compared - return $user_details[5] > 1e7; -} - -$pigs = array_filter(array_slice(preg_split('/\n/', `who -u`), 0, -1), 'is_pig'); - -// ------------ - -$matching = array_filter(array_slice(preg_split('/\n/', `who`), 0, -1), - create_function('$user', 'return preg_match(\'/^gnat /\', $user);')); - -// ------------ - -class Employee -{ - public $name, $age, $ssn, $salary; - - public function __construct($name, $age, $ssn, $salary, $category) - { - $this->name = $name; - $this->age = $age; - $this->ssn = $ssn; - $this->salary = $salary; - $this->category = $category; - } -} - -// ------------ - -$employees = array( - new Employee('sdf', 27, 12345, 47000, 'Engineer'), - new Employee('ajb', 32, 12376, 51000, 'Programmer'), - new Employee('dgh', 31, 12355, 45000, 'Engineer')); - -// ------------ - -$engineers = array_filter($employees, - create_function('$employee', 'return $employee->category == "Engineer";')); - -// @@PLEAC@@_4.14 -// PHP offers a rich set of sorting functions. Key features: -// * Inplace sorts; the original array, not a a copy, is sorted -// * Separate functions exist for sorting [both ascending and descending order]: -// - By value, assign new keys / indices [sort, rsort] -// - By key [ksort, krsort] (for non-numerically indexed arrays) -// - By value [asort, arsort] -// - As above, but using a user-defined comparator [i.e. callback function] -// [usort, uasort, uksort] -// - Natural order sort [natsort] -// * Significantly, if sorting digit-only elements, whether strings or numbers, -// 'natural order' [i.e. 1 before 10 before 100 (ascending)] is retained. If -// the elements are alphanumeric e.g. 'z1', 'z10' then 'natsort' should be -// used [note: beware of 'natsort' with negative numbers; prefer 'sort' or 'asort'] - -$unsorted = array(7, 12, -13, 2, 100, 5, 1, -2, 23, 3, 6, 4); - -sort($unsorted); // -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100 -rsort($unsorted); // 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13 - -asort($unsorted); // -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100 -arsort($unsorted); // 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13 - -natsort($unsorted); // -2, -13, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100 - -// ------------ - -function ascend($left, $right) { return $left > $right; } -function descend($left, $right) { return $left < $right; } - -// ------------ - -usort($unsorted, 'ascend'); // -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100 -usort($unsorted, 'descend'); // 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13 - -uasort($unsorted, 'ascend'); // -13, -2, 1, 2, 3, 4, 5, 6, 7, 12, 23, 100 -uasort($unsorted, 'descend'); // 100, 23, 12, 7, 6, 5, 4, 3, 2, 1, -2, -13 - -// ---------------------------- - -function kill_process($pid) -{ - // Is 'killable' ? - if (!posix_kill($pid, 0)) return; - - // Ok, so kill in two stages - posix_kill($pid, 15); // SIGTERM - sleep(1); - posix_kill($pid, 9); // SIGKILL -} - -function pid($pentry) -{ - $p = preg_split('/\s/', trim($pentry)); - return $p[0]; -} - -$processes = array_map('pid', array_slice(preg_split('/\n/', `ps ax`), 1, -1)); -sort($processes); - -echo join(' ,', $processes) . "\n"; - -echo 'Enter a pid to kill: '; -if (($pid = trim(fgets(STDIN)))) - kill_process($pid); - -// @@PLEAC@@_4.15 -// Tasks in this section would typically use the PHP 'usort' family of functions -// which are used with a comparator function so as to perform custom comparisions. -// A significant difference from the Perl examples is that these functions are -// inplace sorters, so it is the original array that is modified. Where this must -// be prevented a copy of the array can be made and sorted - -function comparator($left, $right) -{ - ; // Compare '$left' with '$right' returning result -} - -// ------------ - -$ordered = array_slice($unordered); -usort($ordered, 'comparator'); - -// ---------------------------- - -// The Perl example looks like it is creating a hash using computed values as the key, -// array values as the value, sorting on the computed key, then extracting the sorted -// values and placing them back into an array - -function compute($value) -{ - ; // Return computed value utilising '$value' -} - -// ------------ - -// Original numerically-indexed array [sample data used] -$unordered = array(5, 3, 7, 1, 4, 2, 6); - -// Create hash using 'compute' function to generate the keys. This example assumes that -// each value in the '$unordered' array is used in generating the corresponding '$key' -foreach($unordered as $value) -{ - $precomputed[compute($value)] = $value; -} - -// Copy the hash, and sort it by key -$ordered_precomputed = array_slice($precomputed, 0); ksort($ordered_precomputed); - -// Extract the values of the hash in current order placing them in a new numerically-indexed -// array -$ordered = array_values($ordered_precomputed); - -// ---------------------------- - -// As above, except uses 'array_update' and 'accum' to help create hash - -function array_update($arr, $lambda, $updarr) -{ - foreach($arr as $key) $lambda($updarr, $key); - return $updarr; -} - -function accum(&$arr, $value) -{ - $arr[compute($value)] = $value; -} - -// ------------ - -function compute($value) -{ - ; // Return computed value utilising '$value' -} - -// ------------ - -// Original numerically-indexed array [sample data used] -$unordered = array(5, 3, 7, 1, 4, 2, 6); - -// Create hash -$precomputed = array_update($unordered, 'accum', array()); - -// Copy the hash, and sort it by key -$ordered_precomputed = array_slice($precomputed, 0); ksort($ordered_precomputed); - -// Extract the values of the hash in current order placing them in a new numerically-indexed -// array -$ordered = array_values($ordered_precomputed); - -// ---------------------------- - -class Employee -{ - public $name, $age, $ssn, $salary; - - public function __construct($name, $age, $ssn, $salary) - { - $this->name = $name; - $this->age = $age; - $this->ssn = $ssn; - $this->salary = $salary; - } -} - -// ------------ - -$employees = array( - new Employee('sdf', 27, 12345, 47000), - new Employee('ajb', 32, 12376, 51000), - new Employee('dgh', 31, 12355, 45000)); - -// ------------ - -$ordered = array_slice($employees, 0); -usort($ordered, create_function('$left, $right', 'return $left->name > $right->name;')); - -// ------------ - -$sorted_employees = array_slice($employees, 0); -usort($sorted_employees, create_function('$left, $right', 'return $left->name > $right->name;')); - -$bonus = array(12376 => 5000, 12345 => 6000, 12355 => 0); - -foreach($sorted_employees as $employee) -{ - echo "{$employee->name} earns \${$employee->salary}\n"; -} - -foreach($sorted_employees as $employee) -{ - if (($amount = $bonus[$employee->ssn])) - echo "{$employee->name} got a bonus of: \${$amount}\n"; -} - -// ------------ - -$sorted = array_slice($employees, 0); -usort($sorted, create_function('$left, $right', 'return $left->name > $right->name || $left->age != $right->age;')); - -// ---------------------------- - -// PHP offers a swag of POSIX functions for obtaining user information [i.e. they all read -// the '/etc/passwd' file for the relevant infroamtion], and it is these that should rightly -// be used for this purpose. However, since the intent of this section is to illustrate array -// manipulation, these functions won't be used. Instead a custom function mimicing Perl's -// 'getpwent' function will be implemented so the code presented here can more faithfully match -// the Perl code - -function get_pw_entries() -{ - function normal_users_only($e) - { - $entry = split(':', $e); return $entry[2] > 100 && $entry[2] < 32768; - } - - foreach(array_filter(file('/etc/passwd'), 'normal_users_only') as $entry) - $users[] = split(':', trim($entry)); - - return $users; -} - -// ------------ - -$users = get_pw_entries(); - -usort($users, create_function('$left, $right', 'return $left[0] > $right[0];')); -foreach($users as $user) echo "{$user[0]}\n"; - -// ---------------------------- - -$names = array('sdf', 'ajb', 'dgh'); - -$sorted = array_slice($names, 0); -usort($sorted, create_function('$left, $right', 'return substr($left, 1, 1) > substr($right, 1, 1);')); - -// ------------ - -$strings = array('bbb', 'aa', 'c'); - -$sorted = array_slice($strings, 0); -usort($sorted, create_function('$left, $right', 'return strlen($left) > strlen($right);')); - -// ---------------------------- - -function array_update($arr, $lambda, $updarr) -{ - foreach($arr as $key) $lambda($updarr, $key); - return $updarr; -} - -function accum(&$arr, $value) -{ - $arr[strlen($value)] = $value; -} - -// ---- - -$strings = array('bbb', 'aa', 'c'); - -$temp = array_update($strings, 'accum', array()); -ksort($temp); -$sorted = array_values($temp); - -// ---------------------------- - -function array_update($arr, $lambda, $updarr) -{ - foreach($arr as $key) $lambda($updarr, $key); - return $updarr; -} - -function accum(&$arr, $value) -{ - if (preg_match('/(\d+)/', $value, $matches)) - $arr[$matches[1]] = $value; -} - -// ---- - -$fields = array('b1b2b', 'a4a', 'c9', 'ddd', 'a'); - -$temp = array_update($fields, 'accum', array()); -ksort($temp); -$sorted_fields = array_values($temp); - -// @@PLEAC@@_4.16 -array_unshift($a1, array_pop($a1)); // last -> first -array_push($a1, array_shift($a1)); // first -> last - -// ---------------------------- - -function grab_and_rotate(&$arr) -{ - $item = $arr[0]; - array_push($arr, array_shift($arr)); - return $item; -} - -// ------------ - -$processes = array(1, 2, 3, 4, 5); - -while (TRUE) -{ - $process = grab_and_rotate($processes); - echo "Handling process {$process}\n"; - sleep(1); -} - -// @@PLEAC@@_4.17 -// PHP offers the 'shuffle' function to perform this task - -$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9); - -shuffle($arr); - -echo join(' ', $arr) . "\n"; - -// ---------------------------- - -// Perl example equivalents -function fisher_yates_shuffle(&$a) -{ - $size = count($a) - 1; - - for($i = $size; $i >= 0; $i--) - { - if (($j = rand(0, $i)) != $i) - list($a[$i], $a[$j]) = array($a[$j], $a[$i]); - } -} - -function naive_shuffle(&$a) -{ - $size = count($a); - - for($i = 0; $i < $size; $i++) - { - $j = rand(0, $size - 1); - list($a[$i], $a[$j]) = array($a[$j], $a[$i]); - } -} - -// ------------ - -$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9); - -fisher_yates_shuffle($arr); -echo join(' ', $arr) . "\n"; - -naive_shuffle($arr); -echo join(' ', $arr) . "\n"; - -// @@PLEAC@@_4.18 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_4.19 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_5.0 -// PHP uses the term 'array' to refer to associative arrays - referred to in Perl -// as 'hashes' - and for the sake of avoiding confusion, the Perl terminology will -// be used. As a matter of interest, PHP does not sport a vector, matrix, or list -// type: the 'array' [Perl 'hash'] serves all these roles - -$age = array('Nat' => 24, 'Jules' => 25, 'Josh' => 17); - -$age['Nat'] = 24; -$age['Jules'] = 25; -$age['Josh'] = 17; - -$age = array_combine(array('Nat', 'Jules', 'Josh'), array(24, 25, 17)); - -// ------------ - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -$food_colour['Apple'] = 'red'; $food_colour['Banana'] = 'yellow'; -$food_colour['Lemon'] = 'yellow'; $food_colour['Carrot'] = 'orange'; - -$food_colour = array_combine(array('Apple', 'Banana', 'Lemon', 'Carrot'), - array('red', 'yellow', 'yellow', 'orange')); - -// @@PLEAC@@_5.1 -$hash[$key] = $value; - -// ------------ - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -$food_colour['Raspberry'] = 'pink'; - -echo "Known foods:\n"; -foreach($food_colour as $food => $colour) echo "{$food}\n"; - -// @@PLEAC@@_5.2 -// Returns TRUE on all existing entries with non-NULL values -if (isset($hash[$key])) - ; // entry exists -else - ; // no such entry - -// ------------ - -// Returns TRUE on all existing entries regardless of attached value -if (array_key_exists($key, $hash)) - ; // entry exists -else - ; // no such entry - -// ---------------------------- - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -foreach(array('Banana', 'Martini') as $name) -{ - if (isset($food_colour[$name])) - echo "{$name} is a food.\n"; - else - echo "{$name} is a drink.\n"; -} - -// ---------------------------- - -$age = array('Toddler' => 3, 'Unborn' => 0, 'Phantasm' => NULL); - -foreach(array('Toddler', 'Unborn', 'Phantasm', 'Relic') as $thing) -{ - echo "{$thing}:"; - if (array_key_exists($thing, $age)) echo ' exists'; - if (isset($age[$thing])) echo ' non-NULL'; - if ($age[$thing]) echo ' TRUE'; - echo "\n"; -} - -// @@PLEAC@@_5.3 -// Remove one, or more, hash entries -unset($hash[$key]); - -unset($hash[$key1], $hash[$key2], $hash[$key3]); - -// Remove entire hash -unset($hash); - -// ---------------------------- - -function print_foods() -{ - // Perl example uses a global variable - global $food_colour; - - $foods = array_keys($food_colour); - - echo 'Foods:'; - foreach($foods as $food) echo " {$food}"; - - echo "\nValues:\n"; - foreach($foods as $food) - { - $colour = $food_colour[$food]; - - if (isset($colour)) - echo " {$colour}\n"; - else - echo " nullified or removed\n"; - } -} - -// ------------ - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -echo "Initially:\n"; print_foods(); - -// Nullify an entry -$food_colour['Banana'] = NULL; -echo "\nWith 'Banana' nullified\n"; -print_foods(); - -// Remove an entry -unset($food_colour['Banana']); -echo "\nWith 'Banana' removed\n"; -print_foods(); - -// Destroy the hash -unset($food_colour); - -// @@PLEAC@@_5.4 -// Access keys and values -foreach($hash as $key => $value) -{ - ; // ... -} - -// Access keys only -foreach(array_keys($hash) as $key) -{ - ; // ... -} - -// Access values only -foreach($hash as $value) -{ - ; // ... -} - -// ---------------------------- - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -foreach($food_colour as $food => $colour) -{ - echo "{$food} is {$colour}\n"; -} - -foreach(array_keys($food_colour) as $food) -{ - echo "{$food} is {$food_colour[$food]}\n"; -} - -// ---------------------------- - -// 'countfrom' - count number of messages from each sender - -$line = fgets(STDIN); - -while (!feof(STDIN)) -{ - if (preg_match('/^From: (.*)/', $line, $matches)) - { - if (isset($from[$matches[1]])) - $from[$matches[1]] += 1; - else - $from[$matches[1]] = 1; - } - - $line = fgets(STDIN); -} - -if (isset($from)) -{ - echo "Senders:\n"; - foreach($from as $sender => $count) echo "{$sender} : {$count}\n"; -} -else -{ - echo "No valid data entered\n"; -} - -// @@PLEAC@@_5.5 -// PHP offers, 'print_r', which prints hash contents in 'debug' form; it also -// works recursively, printing any contained arrays in similar form -// Array -// ( -// [key1] => value1 -// [key2] => value2 -// ... -// ) - -print_r($hash); - -// ------------ - -// Based on Perl example; non-recursive, so contained arrays not printed correctly -foreach($hash as $key => $value) -{ - echo "{$key} => $value\n"; -} - -// ---------------------------- - -// Sorted by keys - -// 1. Sort the original hash -ksort($hash); - -// 2. Extract keys, sort, traverse original in key order -$keys = array_keys($hash); sort($keys); - -foreach($keys as $key) -{ - echo "{$key} => {$hash[$key]}\n"; -} - -// Sorted by values - -// 1. Sort the original hash -asort($hash); - -// 2. Extract values, sort, traverse original in value order [warning: finds -// only first matching key in the case where duplicate values exist] -$values = array_values($hash); sort($values); - -foreach($values as $value) -{ - echo $value . ' <= ' . array_search($value, $hash) . "\n"; -} - -// @@PLEAC@@_5.6 -// Unless sorted, hash elements remain in the order of insertion. If care is taken to -// always add a new element to the end of the hash, then element order is the order -// of insertion. The following function, 'array_push_associative' [modified from original -// found at 'array_push' section of PHP documentation], does just that -function array_push_associative(&$arr) -{ - foreach (func_get_args() as $arg) - { - if (is_array($arg)) - foreach ($arg as $key => $value) { $arr[$key] = $value; $ret++; } - else - $arr[$arg] = ''; - } - - return $ret; -} - -// ------------ - -$food_colour = array(); - -// Individual calls, or ... -array_push_associative($food_colour, array('Banana' => 'Yellow')); -array_push_associative($food_colour, array('Apple' => 'Green')); -array_push_associative($food_colour, array('Lemon' => 'Yellow')); - -// ... one call, one array; physical order retained -// array_push_associative($food_colour, array('Banana' => 'Yellow', 'Apple' => 'Green', 'Lemon' => 'Yellow')); - -print_r($food_colour); - -echo "\nIn insertion order:\n"; -foreach($food_colour as $food => $colour) echo " {$food} => {$colour}\n"; - -$foods = array_keys($food_colour); - -echo "\nStill in insertion order:\n"; -foreach($foods as $food) echo " {$food} => {$food_colour[$food]}\n"; - -// @@PLEAC@@_5.7 -foreach(array_slice(preg_split('/\n/', `who`), 0, -1) as $entry) -{ - list($user, $tty) = preg_split('/\s/', $entry); - $ttys[$user][] = $tty; - - // Could instead do this: - // $user = array_slice(preg_split('/\s/', $entry), 0, 2); - // $ttys[$user[0]][] = $user[1]; -} - -ksort($ttys); - -// ------------ - -foreach($ttys as $user => $all_ttys) -{ - echo "{$user}: " . join(' ', $all_ttys) . "\n"; -} - -// ------------ - -foreach($ttys as $user => $all_ttys) -{ - echo "{$user}: " . join(' ', $all_ttys) . "\n"; - - foreach($all_ttys as $tty) - { - $stat = stat('/dev/$tty'); - $pwent = posix_getpwuid($stat['uid']); - $user = isset($pwent['name']) ? $pwent['name'] : 'Not available'; - echo "{$tty} owned by: {$user}\n"; - } -} - -// @@PLEAC@@_5.8 -// PHP offers the 'array_flip' function to perform the task of exchanging the keys / values -// of a hash i.e. invert or 'flip' a hash - -$reverse = array_flip($hash); - -// ---------------------------- - -$surname = array('Babe' => 'Ruth', 'Mickey' => 'Mantle'); -$first_name = array_flip($surname); - -echo "{$first_name['Mantle']}\n"; - -// ---------------------------- - -$argc == 2 || die("usage: {$argv[0]} food|colour\n"); - -$given = $argv[1]; - -$colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -$food = array_flip($colour); - -if (isset($colour[$given])) - echo "{$given} is a food with colour: {$colour[$given]}\n"; - -if (isset($food[$given])) - echo "{$food[$given]} is a food with colour: {$given}\n"; - -// ---------------------------- - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -foreach($food_colour as $food => $colour) -{ - $foods_with_colour[$colour][] = $food; -} - -$colour = 'yellow'; -echo "foods with colour {$colour} were: " . join(' ', $foods_with_colour[$colour]) . "\n"; - -// @@PLEAC@@_5.9 -// PHP implements a swag of sorting functions, most designed to work with numerically-indexed -// arrays. For sorting hashes, the 'key' sorting functions are required: -// * 'ksort', 'krsort', 'uksort' - -// Ascending order -ksort($hash); - -// Descending order [i.e. reverse sort] -krsort($hash); - -// Comparator-based sort - -function comparator($left, $right) -{ - // Compare left key with right key - return $left > $right; -} - -uksort($hash, 'comparator'); - -// ---------------------------- - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -// ------------ - -ksort($food_colour); - -foreach($food_colour as $food => $colour) -{ - echo "{$food} is {$colour}\n"; -} - -// ------------ - -uksort($food_colour, create_function('$left, $right', 'return $left > $right;')); - -foreach($food_colour as $food => $colour) -{ - echo "{$food} is {$colour}\n"; -} - -// @@PLEAC@@_5.10 -// PHP offers the 'array_merge' function for this task [a related function, 'array_combine', -// may be used to create a hash from an array of keys, and one of values, respectively] - -// Merge two, or more, arrays -$merged = array_merge($a, $b, $c); - -// Create a hash from array of keys, and of values, respectively -$hash = array_combine($keys, $values); - -// ------------ - -// Can always merge arrays manually -foreach(array($h1, $h2, $h3) as $hash) -{ - foreach($hash as $key => $value) - { - // If same-key values differ, only latest retained - $merged[$key] = $value; - - // Do this to append values for that key - // $merged[$key][] = $value; - } -} - -// ---------------------------- - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -$drink_colour = array('Galliano' => 'yellow', 'Mai Tai' => 'blue'); - -// ------------ - -$ingested_colour = array_merge($food_colour, $drink_colour); - -// ------------ - -$substance_colour = array(); - -foreach(array($food_colour, $drink_colour) as $hash) -{ - foreach($hash as $substance => $colour) - { - if (array_key_exists($substance, $substance_colour)) - { - echo "Warning {$substance_colour[$substance]} seen twice. Using first definition.\n"; - continue; - } - $substance_colour[$substance] = $colour; - } -} - -// @@PLEAC@@_5.11 -// PHP offers a number of array-based 'set operation' functions: -// * union: array_merge -// * intersection: array_intersect and family -// * difference: array_diff and family -// which may be used for this type of task - -// Keys occurring in both hashes -$common = array_intersect_key($h1, $h2); - -// Keys occurring in the first hash [left side], but not in the second hash -$this_not_that = array_diff_key($h1, $h2); - -// ---------------------------- - -$food_colour = array('Apple' => 'red', 'Banana' => 'yellow', - 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -$citrus_colour = array('Lemon' => 'yellow', 'Orange' => 'orange', 'Lime' => 'green'); - -$non_citrus = array_diff_key($food_colour, $citrus_colour); - -// @@PLEAC@@_5.12 -// PHP implements a special type known as a 'resource' that encompasses things like file handles, -// sockets, database connections, and many others. The 'resource' type is, essentially, a -// reference variable that is not readily serialisable. That is to say: -// * A 'resource' may be converted to a string representation via the 'var_export' function -// * That same string cannot be converted back into a 'resource' -// So, in terms of array handling, 'resource' types may be stored as array reference values, -// but cannot be used as keys. -// -// I suspect it is this type of problem that the Perl::Tie package helps resolve. However, since -// PHP doesn't, AFAIK, sport a similar facility, the examples in this section cannot be -// implemented using file handles as keys - -$filenames = array('/etc/termcap', '/vmlinux', '/bin/cat'); - -foreach($filenames as $filename) -{ - if (!($fh = fopen($filename, 'r'))) continue; - - // Cannot do this as required by the Perl code: - // $name[$fh] = $filename; - - // Ok - $name[$filename] = $fh; -} - -// Would traverse array via: -// -// foreach(array_keys($name) as $fh) -// ... -// or -// -// foreach($name as $fh => $filename) -// ... -// but since '$fh' cannot be a key, either of these will work: -// -// foreach($name as $filename => $fh) -// or -foreach(array_values($name) as $fh) -{ - fclose($fh); -} - -// @@PLEAC@@_5.13 -// PHP hashes are dynamic expanding and contracting as entries are added, and removed, -// respectively. Thus, there is no need to presize a hash, nor is there, AFAIK, any -// means of doing so except by the number of datums used when defining the hash - -// zero elements -$hash = array(); - -// ------------ - -// three elements -$hash = array('Apple' => 'red', 'Lemon' => 'yellow', 'Carrot' => 'orange'); - -// @@PLEAC@@_5.14 -foreach($array as $element) $count[$element] += 1; - -// @@PLEAC@@_5.15 -$father = array('Cain' => 'Adam', 'Abel' => 'Adam', 'Seth' => 'Adam', 'Enoch' => 'Cain', - 'Irad' => 'Enoch', 'Mehujael' => 'Irad', 'Methusael'=> 'Mehujael', - 'Lamech' => 'Methusael', 'Jabal' => 'Lamech', 'Jubal' => 'Lamech', - 'Tubalcain' => 'Lamech', 'Enos' => 'Seth'); - -// ------------ - -$name = trim(fgets(STDIN)); - -while (!feof(STDIN)) -{ - while (TRUE) - { - echo "$name\n"; - - // Can use either: - if (!isset($father[$name])) break; - $name = $father[$name]; - - // or: - // if (!key_exists($name, $father)) break; - // $name = $father[$name]; - - // or combine the two lines: - // if (!($name = $father[$name])) break; - } - - echo "\n"; - $name = trim(fgets(STDIN)); -} - -// ---------------------------- - -define(SEP, ' '); - -foreach($father as $child => $parent) -{ - if (!$children[$parent]) - $children[$parent] = $child; - else - $children[$parent] .= SEP . $child; -} - -$name = trim(fgets(STDIN)); - -while (!feof(STDIN)) -{ - echo $name . ' begat '; - - if (!$children[$name]) - echo "Nothing\n" - else - echo str_replace(SEP, ', ', $children[$name]) . "\n"; - - $name = trim(fgets(STDIN)); -} - -// ---------------------------- - -define(SEP, ' '); - -$files = array('/tmp/a', '/tmp/b', '/tmp/c'); - -foreach($files as $file) -{ - if (!is_file($file)) { echo "Skipping {$file}\n"; continue; } - if (!($fh = fopen($file, 'r'))) { echo "Skipping {$file}\n"; continue; } - - $line = fgets($fh); - - while (!feof($fh)) - { - if (preg_match('/^\s*#\s*include\s*<([^>]+)>/', $line, $matches)) - { - if (isset($includes[$matches[1]])) - $includes[$matches[1]] .= SEP . $file; - else - $includes[$matches[1]] = $file; - } - - $line = fgets($fh); - } - - fclose($fh); -} - -print_r($includes); - -// @@PLEAC@@_5.16 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_9.0 -$entry = stat('/bin/vi'); -$entry = stat('/usr/bin'); -$entry = stat($argv[1]); - -// ------------ - -$entry = stat('/bin/vi'); - -$ctime = $entry['ctime']; -$size = $entry['size']; - -// ---------------------------- - -// For the simple task of determining whether a file contains, text', a simple -// function that searches for a newline could be implemented. Not exactly -// foolproof, but very simple, low overhead, no installation headaches ... -function containsText($file) -{ - $status = FALSE; - - if (($fp = fopen($file, 'r'))) - { - while (FALSE !== ($char = fgetc($fp))) - { - if ($char == "\n") { $status = TRUE; break; } - } - - fclose($fp); - } - - return $status; -} - -// PHP offers the [currently experimental] Fileinfo group of functions to -// determine file types based on their contents / 'magic numbers'. This -// is functionality similar to the *NIX, 'file' utility. Note that it must -// first be installed using the PEAR utility [see PHP documentation] -function isTextFile($file) -{ - // Note: untested code, but I believe this is how it is supposed to work - $finfo = finfo_open(FILEINFO_NONE); - $status = (finfo_file($finfo, $file) == 'ASCII text'); - finfo_close($finfo); - return $status; -} - -// Alternatively, use the *NIX utility, 'file', directly -function isTextFile($file) -{ - return exec(trim('file -bN ' . escapeshellarg($file))) == 'ASCII text'; -} - -// ---- - -containsText($argv[1]) || die("File {$argv[1]} doesn't have any text in it\n"); - -isTextFile($argv[1]) || die("File {$argv[1]} doesn't have any text in it\n"); - -// ---------------------------- - -$dirname = '/usr/bin/'; - -($dirhdl = opendir($dirname)) || die("Couldn't open {$dirname}\n"); - -while (($file = readdir($dirhdl)) !== FALSE) -{ - printf("Inside %s is something called: %s\n", $dirname, $file); -} - -closedir($dirhdl); - -// @@PLEAC@@_9.1 -$filename = 'example.txt'; - -// Get the file's current access and modification time, respectively -$fs = stat($filename); - -$readtime = $fs['atime']; -$writetime = $fs['mtime']; - -// Alter $writetime, and $readtime ... - -// Update file timestamp -touch($filename, $writetime, $readtime); - -// ---------------------------- - -$filename = 'example.txt'; - -// Get the file's current access and modification time, respectively -$fs = stat($filename); - -$atime = $fs['atime']; -$mtime = $fs['mtime']; - -// Dedicated functions also exist to retrieve this information: -// -// $atime = $fileatime($filename); -// $mtime = $filemtime($filename); -// - -// Perform date arithmetic. Traditional approach where arithmetic is performed -// directly with Epoch Seconds [i.e. the *NIX time stamp value] will work ... - -define('SECONDS_PER_DAY', 60 * 60 * 24); - -// Set file's access and modification times to 1 week ago -$atime -= 7 * SECONDS_PER_DAY; -$mtime -= 7 * SECONDS_PER_DAY; - -// ... but care must be taken to account for daylight saving. Therefore, the -// recommended approach is to use library functions to perform such tasks: - -$atime = strtotime('-7 days', $atime); -$mtime = strtotime('-7 days', $mtime); - -// Update file timestamp -touch($filename, $mtime, $atime); - -// Good idea to clear the cache after such updates have occurred so fresh -// values will be retrieved on next access -clearstatcache(); - -// ---------------------------- - -$argc == 2 || die("usage: {$argv[0]} filename\n"); - -$filename = $argv[1]; -$fs = stat($filename); - -$atime = $fs['atime']; -$mtime = $fs['mtime']; - -// Careful here: since interactive, use, 'system', not 'exec', to launch [latter -// does not work under *NIX - at least, not for me :)] -system(trim(getenv('EDITOR') . ' vi ' . escapeshellarg($filename)), $retcode); - -touch($filename, $mtime, $atime) || die("Error updating timestamp on file, {$filename}!\n"); - -// @@PLEAC@@_9.2 -// The 'unlink' function is used to delete regular files, whilst the 'rmdir' function -// does the same on non-empty directories. AFAIK, no recursive-deletion facility -// exists, and must be manually programmed - -$filename = '...'; - -@unlink($filename) || die("Can't delete, {$filename}!\n"); - -// ------------ - -$files = glob('...'); -$problem = FALSE; - -// Could simply use a foreach loop -foreach($files as $filename) { @unlink($filename) || $problem = TRUE; } - -// -// Alternatively, an applicative approach could be used, one closer in spirit to -// largely-functional languages like Scheme -// -// function is_all_deleted($deleted, $filename) { return @unlink($filename) && $deleted; } -// $problem = !array_reduce($files, 'is_all_deleted', TRUE); -// - -if ($problem) -{ - fwrite(STDERR, 'Could not delete all of:'); - foreach($files as $filename) { fwrite(STDERR, ' ' . $filename); } - fwrite(STDERR, "\n"); exit(1); -} - -// ------------ - -function rmAll($files) -{ - $count = 0; - - foreach($files as $filename) { @unlink($filename) && $count++; }; - - return $count; - -// An applicative alternative using 'create_function', PHP's rough equivalent of 'lambda' ... -// -// return array_reduce($files, -// create_function('$count, $filename', 'return @unlink($filename) && $count++;'), 0); -} - -// ---- - -$files = glob('...'); -$toBeDeleted = sizeof($files); -$count = rmAll($files); - -($count == $toBeDeleted) || die("Could only delete {$count} of {$toBeDeleted} files\n"); - -// @@PLEAC@@_9.3 -$oldfile = '/tmp/old'; $newfile = '/tmp/new'; - -copy($oldfile, $newfile) || die("Error copying file\n"); - -// ---------------------------- - -// All the following copy a file by copying its contents. Examples do so in a single -// operation, but it is also possible to copy arbitrary blocks, or, line-by-line in -// the case of 'text' files -$oldfile = '/tmp/old'; $newfile = '/tmp/new'; - -if (is_file($oldfile)) - file_put_contents($newfile, file_get_contents($oldfile)); -else - die("Problem copying file {$oldfile} to file {$newfile}\n"); - -// ------------ - -$oldfile = '/tmp/old'; $newfile = '/tmp/new'; - -fwrite(($nh = fopen($newfile, 'wb')), fread(($oh = fopen($oldfile, 'rb')), filesize($oldfile))); -fclose($oh); -fclose($nh); - -// ------------ - -// As above, but with some error checking / handling -$oldfile = '/tmp/old'; $newfile = '/tmp/new'; - -($oh = fopen($oldfile, 'rb')) || die("Problem opening input file {$oldfile}\n"); -($nh = fopen($newfile, 'wb')) || die("Problem opening output file {$newfile}\n"); - -if (($filesize = filesize($oldfile)) > 0) -{ - fwrite($nh, fread($oh, $filesize)) || die("Problem reading / writing file data\n"); -} - -fclose($oh); -fclose($nh); - -// ---------------------------- - -// Should there be platform-specfic problems copying 'very large' files, it is -// a simple matter to call a system command utility via, 'exec' - -// *NIX-specific example. Could check whether, 'exec', succeeded, but checking whether -// a file exists after the operation might be a better approach -$oldfile = '/tmp/old'; $newfile = '/tmp/new'; - -is_file($newfile) && unlink($newfile); - -exec(trim('cp --force ' . escapeshellarg($oldfile) . ' ' . escapeshellarg($newfile))); - -is_file($newfile) || die("Problem copying file {$oldfile} to file {$newfile}\n"); - -// For other operating systems just change: -// * filenames -// * command being 'exec'ed -// as the rest of the code is platform independant - -// @@PLEAC@@_9.4 -function makeDevInodePair($filename) -{ - if (!($fs = @stat($filename))) return FALSE; - return strval($fs['dev'] . $fs['ino']); -} - -// ------------ - -function do_my_thing($filename) -{ - // Using a global variable to mimic Perl example, but could easily have passed - // '$seen' as an argument - global $seen; - - $devino = makeDevInodePair($filename); - - // Process $filename if it has not previously been seen, else just increment - if (!isset($seen[$devino])) - { - // ... process $filename ... - - // Set initial count - $seen[$devino] = 1; - } - else - { - // Otherwise, just increment the count - $seen[$devino] += 1; - } -} - -// ---- - -// Simple example -$seen = array(); - -do_my_thing('/tmp/old'); -do_my_thing('/tmp/old'); -do_my_thing('/tmp/old'); -do_my_thing('/tmp/new'); - -foreach($seen as $devino => $count) -{ - echo "{$devino} -> {$count}\n"; -} - -// ------------ - -// A variation on the above avoiding use of global variables, and illustrating use of -// easily-implemented 'higher order' techniques - -// Helper function loosely modelled on, 'array_reduce', but using an array as -// 'accumulator', which is returned on completion -function array_update($arr, $lambda, $updarr) -{ - foreach($arr as $key) $lambda($updarr, $key); - return $updarr; -} - -function do_my_thing(&$seen, $filename) -{ - if (!array_key_exists(($devino = makeDevInodePair($filename)), $seen)) - { - // ... processing $filename ... - - // Update $seen - $seen[$devino] = 1; - } - else - { - // Update $seen - $seen[$devino] += 1; - } -} - -// ---- - -// Simple example -$files = array('/tmp/old', '/tmp/old', '/tmp/old', '/tmp/new'); - -// Could do this ... -$seen = array(); -array_update($files, 'do_my_thing', &$seen); - -// or this: -$seen = array_update($files, 'do_my_thing', array()); - -// or a 'lambda' could be used: -array_update($files, - create_function('$seen, $filename', '... code not shown ...'), - &$seen); - -foreach($seen as $devino => $count) -{ - echo "{$devino} -> {$count}\n"; -} - -// ---------------------------- - -$files = glob('/tmp/*'); - -define(SEP, ';'); -$seen = array(); - -foreach($files as $filename) -{ - if (!array_key_exists(($devino = makeDevInodePair($filename)), $seen)) - $seen[$devino] = $filename; - else - $seen[$devino] = $seen[$devino] . SEP . $filename; -} - -$devino = array_keys($seen); -sort($devino); - -foreach($devino as $key) -{ - echo $key . ':'; - foreach(split(SEP, $seen[$key]) as $filename) echo ' ' . $filename; - echo "\n"; -} - -// @@PLEAC@@_9.5 -// Conventional POSIX-like approach to directory traversal -$dirname = '/usr/bin/'; - -($dirhdl = opendir($dirname)) || die("Couldn't open {$dirname}\n"); - -while (($file = readdir($dirhdl)) !== FALSE) -{ - ; // ... do something with $dirname/$file - // ... -} - -closedir($dirhdl); - -// ------------ - -// Newer [post PHP 4], 'applicative' approach - an array of filenames is -// generated that may be processed via external loop ... - -$dirname = '/usr/bin/'; - -foreach(scandir($dirname) as $file) -{ - ; // ... do something with $dirname/$file - // ... -} - -// .. or, via callback application, perhaps after massaging by one of the -// 'array' family of functions [also uses, 'array_update', from earlier section] - -$newlist = array_update(array_reverse(scandir($dirname)), - create_function('$filelist, $file', ' ; '), - array()); - -// And don't forget that the old standby, 'glob', that returns an array of -// paths filtered using the Bourne Shell-based wildcards, '?' and '*', is -// also available - -foreach(glob($dirname . '*') as $path) -{ - ; // ... do something with $path - // ... -} - -// ---------------------------- - -// Uses, 'isTextFile', from an earlier section -$dirname = '/usr/bin/'; - -echo "Text files in {$dirname}:\n"; - -foreach(scandir($dirname) as $file) -{ - // Take care when constructing paths to ensure cross-platform operability - $path = $dirname . $file; - - if (is_file($path) && isTextFile($path)) echo $path . "\n"; -} - -// ---------------------------- - -function plain_files($dirname) -{ - ($dirlist = glob($dirname . '*')) || die("Couldn't glob {$dirname}\n"); - - // Pass function name directly if only a single function performs filter test - return array_filter($dirlist, 'is_file'); - - // Use, 'create_function', if a multi-function test is needed - // - // return array_filter($dirlist, create_function('$path', 'return is_file($path);')); - // -} - -// ------------ - -foreach(plain_files('/tmp/') as $path) -{ - echo $path . "\n"; -} - -// @@PLEAC@@_9.6 -$dirname = '/tmp/'; - -// Full paths -$pathlist = glob($dirname . '*.c'); - -// File names only - glob-based matching -$filelist = array_filter(scandir($dirname), - create_function('$file', 'return fnmatch("*.c", $file);')); - -// ---------------------------- - -$dirname = '/tmp/'; - -// File names only - regex-based matching [case-insensitive] -$filelist = array_filter(scandir($dirname), - create_function('$file', 'return eregi("\.[ch]$", $file);')); - -// ---------------------------- - -$dirname = '/tmp/'; - -// Directory names - all-digit names -$dirs = array_filter(glob($dirname . '*', GLOB_ONLYDIR), - create_function('$path', 'return ereg("^[0-9]+$", basename($path));')); - -// @@PLEAC@@_9.7 -// Recursive directory traversal function and helper: traverses a directory tree -// applying a function [and a variable number of accompanying arguments] to each -// file - -class Accumulator -{ - public $value; - public function __construct($start_value) { $this->value = $start_value; } -} - -// ------------ - -function process_directory_($op, $func_args) -{ - if (is_dir($func_args[0])) - { - $current = $func_args[0]; - foreach(scandir($current) as $entry) - { - if ($entry == '.' || $entry == '..') continue; - $func_args[0] = $current . '/' . $entry; - process_directory_($op, $func_args); - } - } - else - { - call_user_func_array($op, $func_args); - } -} - -function process_directory($op, $dir) -{ - if (!is_dir($dir)) return FALSE; - $func_args = array_slice(func_get_args(), 1); - process_directory_($op, $func_args); - return TRUE; -} - -// ---------------------------- - -$dirlist = array('/tmp/d1', '/tmp/d2', '/tmp/d3'); - -// Do something with each directory in the list -foreach($dirlist as $dir) -{ - ; - // Delete directory [if empty] -> rmdir($dir); - // Make it the 'current directory' -> chdir($dir); - // Get list of files it contains -> $filelist = scandir($dir); - // Get directory metadata -> $ds = stat($dir); -} - -// ------------ - -$dirlist = array('/tmp/d1', '/tmp/d2', '/tmp/d3'); - -function pf($path) -{ - // ... do something to the file or directory ... - printf("%s\n", $path); -} - -// For each directory in the list ... -foreach($dirlist as $dir) -{ - // Is this a valid directory ? - if (!is_dir($dir)) { printf("%s does not exist\n", $dir); continue; } - - // Ok, so get all the directory's entries - $filelist = scandir($dir); - - // An 'empty' directory will contain at least two entries: '..' and '.' - if (count($filelist) == 2) { printf("%s is empty\n", $dir); continue; } - - // For each file / directory in the directory ... - foreach($filelist as $file) - { - // Ignore '..' and '.' entries - if ($file == '.' || $file == '..') continue; - - // Apply function to process the file / directory - pf($dir . '/' . $file); - } -} - -// ---------------------------- - -function accum_filesize($file, $accum) -{ - is_file($file) && ($accum->value += filesize($file)); -} - -// ------------ - -// Verify arguments ... -$argc == 2 || die("usage: {$argv[0]} dir\n"); -$dir = $argv[1]; - -is_dir($dir) || die("{$dir} does not exist / not a directory\n"); - -// Collect data [use an object to accumulate results] -$dirsize = new Accumulator(0); -process_directory('accum_filesize', $dir, $dirsize); - -// Report results -printf("%s contains %d bytes\n", $dir, $dirsize->value); - -// ---------------------------- - -function biggest_file($file, $accum) -{ - if (is_file($file)) - { - $fs = filesize($file); - if ($accum->value[1] < $fs) { $accum->value[0] = $file; $accum->value[1] = $fs; } - } -} - -// ------------ - -// Verify arguments ... -$argc == 2 || die("usage: {$argv[0]} dir\n"); -$dir = $argv[1]; - -is_dir($dir) || die("{$dir} does not exist / not a directory\n"); - -// Collect data [use an object to accumulate results] -$biggest = new Accumulator(array('', 0)); -process_directory('biggest_file', $dir, $biggest); - -// Report results -printf("Biggest file is %s containing %d bytes\n", $biggest->value[0], $biggest->value[1]); - -// ---------------------------- - -function youngest_file($file, $accum) -{ - if (is_file($file)) - { - $fct = filectime($file); - if ($accum->value[1] > $fct) { $accum->value[0] = $file; $accum->value[1] = $fct; } - } -} - -// ------------ - -// Verify arguments ... -$argc == 2 || die("usage: {$argv[0]} dir\n"); -$dir = $argv[1]; - -is_dir($dir) || die("{$dir} does not exist / not a directory\n"); - -// Collect data [use an object to accumulate results] -$youngest = new Accumulator(array('', 2147483647)); -process_directory('youngest_file', $dir, $youngest); - -// Report results -printf("Youngest file is %s dating %s\n", $youngest->value[0], date(DATE_ATOM, $youngest->value[1])); - -// @@PLEAC@@_9.8 -// AFAICT, there is currently no library function that recursively removes a -// directory tree [i.e. a directory, it's subdirectories, and any other files] -// with a single call. Such a function needs to be custom built. PHP tools -// with which to do this: -// * 'unlink', 'rmdir', 'is_dir', and 'is_file' functions, will all take care -// of the file testing and deletion -// * Actual directory traversal requires obtaining directory / subdirectory -// lists, and here there is much choice available, though care must be taken -// as each has it's own quirks -// - 'opendir', 'readdir', 'closedir' -// - 'scandir' -// - 'glob' -// - SPL 'directory iterator' classes [newish / experimental - not shown here] -// -// The PHP documentation for 'rmdir' contains several examples, each illustrating -// one of each approach; the example shown here is loosely based on one of these -// examples - -// Recursor - recursively traverses directory tree -function rmtree_($dir) -{ - $dir = "$dir"; - - if ($dh = opendir($dir)) - { - while (FALSE !== ($item = readdir($dh))) - { - if ($item != '.' && $item != '..') - { - $subdir = $dir . '/' . "$item"; - - if (is_dir($subdir)) rmtree_($subdir); - else @unlink($subdir); - } - } - - closedir($dh); @rmdir($dir); - } -} - -// Launcher - performs validation then starts recursive routine -function rmtree($dir) -{ - if (is_dir($dir)) - { - (substr($dir, -1, 1) == '/') && ($dir = substr($dir, 0, -1)); - rmtree_($dir); return !is_dir($dir); - } - - return FALSE; -} - -// ------------ - -$argc == 2 || die("usage: rmtree dir\n"); - -rmtree($argv[1]) || die("Could not remove directory {$argv[1]}\n"); - -// @@PLEAC@@_9.9 -$filepairs = array('x.txt' => 'x2.txt', 'y.txt' => 'y.doc', 'zxc.txt' => 'cxz.txt'); - -foreach($filepairs as $oldfile => $newfile) -{ - @rename($oldfile, $newfile) || fwrite(STDERR, sprintf("Could not rename %s to %s\n", $oldfile, $newfile)); -} - -// ---------------------------- - -// Call a system command utility via, 'exec'. *NIX-specific example. Could check whether, -// 'exec', succeeded, but checking whether a renamed file exists after the operation might -// be a better approach - -$oldfile = '/tmp/old'; $newfile = '/tmp/new'; - -is_file($newfile) && unlink($newfile); - -exec(trim('mv --force ' . escapeshellarg($oldfile) . ' ' . escapeshellarg($newfile))); - -is_file($oldfile) || die("Problem renaming file {$oldfile} to file {$newfile}\n"); - -// For other operating systems just change: -// * filenames -// * command being 'exec'ed -// as the rest of the code is platform independant - -// ---------------------------- - -// A modified implementation of Larry's Filename Fixer. Rather than passing -// a single expression, a 'from' regexp is passed; each match in the file -// name(s) is changed to the value of 'to'. It otherwise behaves the same -// - -$argc > 2 || die("usage: rename from to [file ...]\n"); - -$from = $argv[1]; -$to = $argv[2]; - -if (count(($argv = array_slice($argv, 3))) < 1) - while (!feof(STDIN)) $argv[] = substr(fgets(STDIN), 0, -1); - -foreach($argv as $file) -{ - $was = $file; - $file = ereg_replace($from, $to, $file); - - if (strcmp($was, $file) != 0) - @rename($was, $file) || fwrite(STDERR, sprintf("Could not rename %s to %s\n", $was, $file)); -} - -// @@PLEAC@@_9.10 -$base = basename($path); -$dir = dirname($path); - -// PHP's equivalent to Perl's 'fileparse' -$pathinfo = pathinfo($path); - -$base = $pathinfo['basename']; -$dir = $pathinfo['dirname']; -$ext = $pathinfo['extension']; - -// ---------------------------- - -$path = '/usr/lib/libc.a'; - -printf("dir is %s, file is %s\n", dirname($path), basename($path)); - -// ------------ - -$path = '/usr/lib/libc.a'; - -$pathinfo = pathinfo($path); - -printf("dir is %s, name is %s, extension is %s\n", $pathinfo['dirname'], $pathinfo['basename'], $pathinfo['extension']); - -// ---------------------------- - -// Handle Mac example as a simple parse task. However, AFAIK, 'pathinfo' is cross-platform, -// so should handle file path format differences transparently -$path = 'Hard%20Drive:System%20Folder:README.txt'; - -$macp = array_combine(array('drive', 'folder', 'filename'), split("\:", str_replace('%20', ' ', $path))); -$macf = array_combine(array('name', 'extension'), split("\.", $macp['filename'])); - -printf("dir is %s, name is %s, extension is %s\n", ($macp['drive'] . ':' . $macp['folder']), $macf['name'], ('.' . $macf['extension'])); - -// ---------------------------- - -// Not really necessary since we have, 'pathinfo', but better matches Perl example -function file_extension($filename, $separator = '.') -{ - return end(split(("\\" . $separator), $filename)); -} - -// ---- - -echo file_extension('readme.txt') . "\n"; - -// @@PLEAC@@_9.11 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_9.12 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_10.0 -// Since defined at outermost scope, $greeted may be considered a global variable -$greeted = 0; - -// ------------ - -// Must use, 'global', keyword to inform functions that $greeted already exists as -// a global variable. If this is not done, a local variable of that name is implicitly -// defined -function howManyGreetings() -{ - global $greeted; - return $greeted; -} - -function hello() -{ - global $greeted; - $greeted++; - echo "high there!, this procedure has been called {$greeted} times\n"; -} - -// ------------ - -hello(); -$greetings = howManyGreetings(); -echo "bye there!, there have been {$greetings} greetings so far\n"; - -// @@PLEAC@@_10.1 -// Conventionally-defined function together with parameter list -function hypotenuse($side1, $side2) -{ - return sqrt(pow($side1, 2) + pow($side2, 2)); -} - -// ---- - -// Alternative is to define the function without parameter list, then use -// 'func_get_arg' to extract arguments -function hypotenuse() -{ - // Could check number of arguments passed with: 'func_num_args', which - // would be the approach used if dealing with variable number of arguments - $side1 = func_get_arg(0); $side2 = func_get_arg(1); - - return sqrt(pow($side1, 2) + pow($side2, 2)); -} - -// ------------ - -// 1. Conventional function call -$diag = hypotenuse(3, 4); - -// ------------ - -// 2. Function call using, 'call_user_func' library routines -$funcname = 'hypotenuse'; - -// a. Pass function name, and variable number of arguments -$diag = call_user_func($funcname, 3, 4); - -// b. Package arguments as array, pass together with function name -$args = array(3, 4); -$diag = call_user_func_array($funcname, $args); - -// ---------------------------- - -$nums = array(1.4, 3.5, 6.7); - -// ------------ - -// Pass-by-value -function int_all($arr) -{ - return array_map(create_function('$n', 'return (int) $n;'), $arr); -} - -// Pass-by-reference -function trunc_em(&$n) -{ - foreach ($n as &$value) $value = (int) $value; -} - -// ------------ - -// $nums untouched; $ints is new array -$ints = int_all($nums); - -// $nums updated -trunc_em($nums); - -// @@PLEAC@@_10.2 -// Strictly-speaking, PHP is neither lexically [no environment capture] nor -// dynamically [no variable shadowing] scoped. A script in which several -// functions have been defined has two, entirely separate, scopes: -// -// * A 'top-level' scope i.e. everything outside each function -// -// * A 'local scope' within each function; each function is a self-contained -// entity and cannot [via conventional means] access variables outside its -// local scope. Accessing a variable that has not been locally defined -// serves to define it i.e. accessing a variable assumed to be global -// sees a local variable of that name defined -// -// The way 'global' variables are provided is via a predefined array of -// variable names, $GLOBALS [it is one of a special set of variables known -// as 'superglobals'; such variables *are* accessable in all scopes]. Each -// entry in this array is a 'global' variable name, and may be freely -// accessed / updated. A more convenient means of accessing such variables -// is via the 'global' keyword: one or more variables within a function is -// declared 'global', and those names are then taken to refer to entries -// in the $GLOBALS array rather than seeing local variables of that name -// accessed or defined - -function some_func() -{ - // Variables declared within a function are local to that function - $variable = 'something'; -} - -// ---------------------------- - -// Top-level declared variables -$name = $argv[1]; $age = $argv[2]; - -$c = fetch_time(); - -$condition = 0; - -// ------------ - -function run_check() -{ - // The globally-declared variable, '$condition', is not accessable within - // the function unless it declared as 'global. Had this not been done then - // attempts to access, '$condition', would have seen a local variable - // of that name declared and updated. Same applies to other variables - global $condition, $name, $age, $c; - - $condition = 1; - // ... -} - -function check_x($x) -{ - $y = 'whatever'; - - // This function only has access to the parameter, '$x', and the locally - // declared variable, '$y'. - - // Whilst 'run_check' has access to several global variables, the current - // function does not. For it to access the global variable, '$condition', - // it must be declared 'global' - run_check(); - - global $condition; - - // 'run_check' will have updated, '$condition', and since it has been - // declared 'global' here, it is accessable - - if ($condition) - { - ; // ... - } -} - -// @@PLEAC@@_10.3 -// Local scopes are not created in the same way as in Perl [by simply enclosing -// within braces]: only via the creation of functions are local scopes created - -// Doesn't create a local scope; '$myvariable' is created at top-level -{ - $myvariable = 7; -} - -// '$myvariable' is accessable here -echo $myvariable . "\n"; - -// ------------ - -{ - $counter = 0; - - // Local scope created within function, but not within surrounding braces - // so: - // * '$counter' is actually a top-level variable, so globally accessable - // * 'next_counter' has no implict access to '$counter'; must be granted - // via 'global' keyword - - function next_counter() { global $counter; $counter++; } -} - -// ---------------------------- - -// PHP doesn't, AFAIK, offer an equivalent to Perl's BEGIN block. Similar -// behaviour may be obtained by defining a class, and including such code -// in its constructor - -class BEGIN -{ - private $myvariable; - - function __construct() - { - $this->myvariable = 5; - } - - function othersub() - { - echo $this->myvariable . "\n"; - } -} - -// ------------ - -$b = new BEGIN(); - -$b->othersub(); - -// ---------------------------- - -// PHP, like C, supports 'static' local variables, that is, those that upon -// first access are initialised, and thence retain their value between function -// calls. However, the 'counter' example is better implemented as a class - -class Counter -{ - private $counter; - - function __construct($counter_init) - { - $this->counter = $counter_init; - } - - function next_counter() { $this->counter++; return $this->counter; } - function prev_counter() { $this->counter; return $this->counter; } -} - -// ------------ - -$counter = new Counter(42); -echo $counter->next_counter() . "\n"; -echo $counter->next_counter() . "\n"; -echo $counter->prev_counter() . "\n"; - -// @@PLEAC@@_10.4 -// AFAICT there is no means of obtaining the name of the currently executing -// function, or, for that matter, perform any stack / activation record, -// inspection. It *is* possible to: -// -// * Obtain a list of the currently-defined functions ['get_defined_functions'] -// * Check whether a specific function exists ['function_exists'] -// * Use the 'Reflection API' -// -// So, to solve this problem would seem to require adopting a convention where -// a string representing the function name is passed as an argument, or a local -// variable [perhaps called, '$name'] is so set [contrived, and of limited use] - -function whoami() -{ - $name = 'whoami'; - echo "I am: {$name}\n"; -} - -// ------------ - -whoami(); - -// @@PLEAC@@_10.5 -// In PHP all items exist as 'memory references' [i.e. non-modifiable pointers], -// so when passing an item as a function argument, or returning an item from -// a function, it is this 'memory reference' that is passed, and *not* the -// contents of that item. Should several references to an item exist [e.g. if -// passed to a function then at least two such references would exist in -// different scopes] they would all be refering to the same copy of the item. -// However, if an attempt is made to alter the item is made, a copy is made -// and it is the copy that is altered, leaving the original intact. -// -// The PHP reference mechanism is used to selectively prevent this behaviour, -// and ensure that if a change is made to an item that no copy is made, and that -// it is the original item that is changed. Importantly, there is no efficiency -// gain from passing function parameters using references if the parameter item -// is not altered. - -// A copy of the item referred to by, '$arr', is made, and altered; original -// remains intact -function array_by_value($arr) -{ - $arr[0] = 7; - echo $arr[0] . "\n"; -} - -// No copy is made; original item referred to by, '$arr', is altered -function array_by_ref(&$arr) -{ - $arr[0] = 7; - echo $arr[0] . "\n"; -} - -// ------------ - -$arr = array(1, 2, 3); - -echo $arr[0] . "\n"; // output: 1 -array_by_value($arr); // output: 7 -echo $arr[0] . "\n"; // output: 1 - -$arr = array(1, 2, 3); - -echo $arr[0] . "\n"; // output: 1 -array_by_ref($arr); // output: 7 -echo $arr[0] . "\n"; // output: 7 - -// ---------------------------- - -// Since, 'add_vecpair', does not attempt to alter either, '$x' or '$y', it makes -// no difference whether they are 'passed by value', or 'passed by reference' -function add_vecpair($x, $y) -{ - $r = array(); - $length = count($x); - for($i = 0; $i < $length; $i++) $r[$i] = $x[$i] + $y[$i]; - return $r; -} - -// ... -count($arr1) == count($arr2) || die("usage: add_vecpair ARR1 ARR2\n"); - -// 'passed by value' -$arr3 = add_vecpair($arr1, $arr2); - -// 'passed by reference' [also possible to override default 'passed by value' -// if required] -$arr3 = add_vecpair(&$arr1, &$arr2); - -// @@PLEAC@@_10.6 -// PHP can be described as a dynamically typed language because variables serve -// as identifiers, and the same variable may refer to data of various types. -// As such, the set of arguments passed to a function may vary in type between -// calls, as can the type of return value. Where this is likely to occur type -// checking should be performed either / both within the function body, and -// when obtaining it's return value. As for Perl-style 'return context', I -// don't believe it is supported by PHP - -// Can return any type -function mysub() -{ - // ... - return 5; - // ... - return array(5); - // ... - return '5'; -} - -// Throw away return type [i.e. returns a 'void' type ?] -mysub(); - -// Check return type. Can do via: -// * gettype($var) -// * is_xxx e.g. is_array($var), is_muneric($var), ... -$ret = mysub(); - -if (is_numeric($ret)) -{ - ; // ... -} - -if (is_array($ret)) -{ - ; // ... -} - -if (is_string($ret)) -{ - ; // ... -} - -// @@PLEAC@@_10.7 -// PHP doesn't directly support named / keyword parameters, but these can be -// easily mimiced using a class of key / value pairs, and passing a variable -// number of arguments - -class KeyedValue -{ - public $key, $value; - public function __construct($key, $value) { $this->key = $key; $this->value = $value; } -} - -function the_func() -{ - foreach (func_get_args() as $arg) - { - printf("Key: %10s|Value:%10s\n", $arg->key, $arg->value); - } -} - -// ---- - -the_func(new KeyedValue('name', 'Bob'), - new KeyedValue('age', 36), - new KeyedValue('income', 51000)); - -// ---------------------------- - -// Alternatively, an associative array of key / value pairs may be constructed. -// With the aid of the 'extract' built-in function, the key part of this array -// may be intsntiated to a variable name, thus more closely approximating the -// behaviour of true named parameters - -function the_func($var_array) -{ - extract($var_array); - - if (isset($name)) printf("Name: %s\n", $name); - if (isset($age)) printf("Age: %s\n", $age); - if (isset($income)) printf("Income: %s\n", $income); -} - -// ---- - -the_func(array('name' => 'Bob', 'age' => 36, 'income' => 51000)); - -// ---------------------------- - -class RaceTime -{ - public $time, $dim; - public function __construct($time, $dim) { $this->time = $time; $this->dim = $dim; } -} - -function the_func($var_array) -{ - extract($var_array); - - if (isset($start_time)) printf("start: %d - %s\n", $start_time->time, $start_time->dim); - if (isset($finish_time)) printf("finish: %d - %s\n", $finish_time->time, $finish_time->dim); - if (isset($incr_time)) printf("incr: %d - %s\n", $incr_time->time, $incr_time->dim); -} - -// ---- - -the_func(array('start_time' => new RaceTime(20, 's'), - 'finish_time' => new RaceTime(5, 'm'), - 'incr_time' => new RaceTime(3, 'm'))); - -the_func(array('start_time' => new RaceTime(5, 'm'), - 'finish_time' => new RaceTime(30, 'm'))); - -the_func(array('start_time' => new RaceTime(30, 'm'))); - -// @@PLEAC@@_10.8 -// The 'list' keyword [looks like a function but is actually a special language -// construct] may be used to perform multiple assignments from a numerically -// indexed array of values, and offers the added bonus of being able to skip -// assignment of one, or more, of those values - -function func() { return array(3, 6, 9); } - -// ------------ - -list($a, $b, $c) = array(6, 7, 8); - -// Provided 'func' returns an numerically-indexed array, the following -// multiple assignment will work -list($a, $b, $c) = func(); - -// Any existing variables no longer wanted would need to be 'unset' -unset($b); - -// As above, but second element of return array discarded -list($a,,$c) = func(); - -// ---------------------------- - -// Care needed to ensure returned array is numerically-indexed -list($dev, $ino,,,$uid) = array_slice(array_values(stat($filename)), 0, 13); - -// @@PLEAC@@_10.9 -// Multiple return values are possible via packing a set of values within a -// numerically-indexed array and using 'list' to extract them - -function some_func() { return array(array(1, 2, 3), array('a' => 1, 'b' => 2)); } - -// ------------ - -list($arr, $hash) = some_func(); - -// ---------------------------- - -function some_func(&$arr, &$hash) { return array($arr, $hash); } - -// ------------ - -$arrin = array(1, 2, 3); $hashin = array('a' => 1, 'b' => 2); - -list($arr, $hash) = some_func($arrin, $hashin); - -// @@PLEAC@@_10.10 -// AFAICT, most of the PHP library functions are designed to return some required -// value on success, and FALSE on exit. Whilst it is possible to return NULL, or -// one of the recognised 'empty' values [e.g. '' or 0 or an empty array etc], -// FALSE actually seems to be the preferred means of indicating failure - -function a_func() { return FALSE; } - -a_func() || die("Function failed\n"); - -if (!a_func()) die("Function failed\n"); - -// @@PLEAC@@_10.11 -// Whether PHP is seen to support prototyping depends on the accepted -// definition of this term: -// -// * Prototyping along the lines used in Ada, Modula X, and even C / C++, -// in which a function's interface is declared separately from its -// implementation, is *not* supported -// -// * Prototyping in which, as part of the function definition, parameter -// information must be supplied. In PHP a function definition neither -// parameter, nor return type, information needs to be supplied, though -// it is usual to see a parameter list supplied [indicates the number, -// positional order, and optionally, whether a parameter is passed by -// reference; no type information is present]. In short, prototyping in -// PHP is optional, and limited - -function func_with_one_arg($arg1) -{ - ; // ... -} - -function func_with_two_arg($arg1, $arg2) -{ - ; // ... -} - -function func_with_three_arg($arg1, $arg2, $arg3) -{ - ; // ... -} - -// The following may be interpreted as meaning a function accepting no -// arguments: -function func_with_no_arg() -{ - ; // ... -} - -// whilst the following may mean a function taking zero or more arguments -function func_with_no_arg_information() -{ - ; // ... -} - -// @@PLEAC@@_10.12 -// Unlike in Perl, PHP's 'die' [actually an alias for 'exit'] doesn't throw -// an exception, but instead terminates the script, optionally either -// returning an integer value to the operating system, or printing a message. -// So, the following, does not exhibit the same behaviour as the Perl example - -die("some message\n"); - -// Instead, like so many modern languages, PHP implements exception handling -// via the 'catch' and 'throw' keywords. Furthermore, a C++ or Java programmer -// would find PHP's exception handling facility remarkably similar to those -// of their respective languages. A simple, canonical example follows: - -// Usual to derive new exception classes from the built-in, 'Exception', -// class -class MyException extends Exception -{ - // ... -} - -// ... - -try -{ - // ... - if ($some_problem_detected) throw new MyException('some message', $some_error_code); - // .. -} - -catch (MyException $e) -{ - ; // ... handle the problem ... -} - -// ---------------------------- - -class FullMoonException extends Exception -{ - // ... -} - -// ... - -try -{ - // ... - if ($some_problem_detected) throw new FullMoonException('...', $full_moon_error_code); - // .. -} - -catch (FullMoonException $e) -{ - // ... rethrow the exception - will propagate to higher level ... - throw $e; -} - -// @@PLEAC@@_10.13 -// Please refer to discussion about PHP scope in section two of this chapter. -// Briefly, PHP assumes a variable name within a function to be local unless -// it has been specifically declared with the, 'global', keyword, in which -// case it refers to a variable in the 'superglobal' array, '$GLOBALS'. Thus, -// inadvertant variable name shadowing cannot occur since it is it not possible -// to use the same name to refer to both a local and a global variable. If -// accessing a global variable care should be taken to not accidentally update -// it. The techniques used in this section are simply not required. - -// *** NOT TRANSLATED *** - -// @@PLEAC@@_10.14 -// In PHP once a function has been defined it remains defined. In other words, -// it cannot be undefined / deleted, nor can that particular function name be -// reused to reference another function body. Even the lambda-like functions -// created via the 'create_function' built-in, cannot be undefined [they exist -// until script termination, thus creating too many of these can actually -// exhaust memory !]. However, since the latter can be assigned to variables, -// the same variable name can be used to reference difference functions [and -// when this is done the reference to the previous function is lost (unless -// deliberately saved), though the function itself continues to exist]. -// -// If, however, all that is needed is a simple function aliasing facility, -// then just assign the function name to a variable, and execute using the -// variable name - -// Original function -function expand() { echo "expand\n"; } - -// Prove that function exists -echo (function_exists('expand') ? 'yes' : 'no') . "\n"; - -// Use a variable to alias it -$grow = 'expand'; - -// Call function via original name, and variable, respectively -expand(); - -$grow(); - -// Remove alias variable -unset($grow); - -// ---------------------------- - -function fred() { echo "fred\n"; } - -$barney = 'fred'; - -$barney(); - -unset($barney); - -fred(); - -// ------------ - -$fred = create_function('', 'echo "fred\n";'); - -$barney = $fred; - -$barney(); - -unset($barney); - -$fred(); - -// ---------------------------- - -function red($text) { return "<FONT COLOR='red'>$text</FONT>"; } - -echo red('careful here') . "\n"; - -// ------------ - -$colour = 'red'; - -$$colour = create_function('$text', 'global $colour; -return "<FONT COLOR=\'$colour\'>$text</FONT>";'); - -echo $$colour('careful here') . "\n"; - -unset($$colour); - -// ---- - -$colours = split(' ', 'red blue green yellow orange purple violet'); - -foreach ($colours as $colour) -{ - $$colour = create_function('$text', 'global $colour; - return "<FONT COLOR=\'$colour\'>$text</FONT>";'); -} - -foreach ($colours as $colour) { echo $$colour("Careful with this $colour, James") . "\n"; } - -foreach ($colours as $colour) { unset($$colour); } - -// @@PLEAC@@_10.15 -// PHP sports an AUTOLOAD facility that is quite easy to use, but, AFAICT, is geared -// towards the detection of unavailable classes rather than for individual functions. -// Here is a rudimentary example: - -function __autoload($classname) -{ - if (!file_exists($classname)) - { - // Class file does not exist, so handle situation; in this case, - // issue error message, and exit program - die("File for class: {$classname} not found - aborting\n"); - } - else - { - // Class file exists, so load it - require_once $classname; - } -} - -// ------------ - -// Attempt to instantiate object of undefined class -new UnknownClassObject(); - -// Execution continues here if class exists -// ... - -// ---------------------------- - -// It is also possible to perform [quite extensive] introspection on functions, -// variables etc, so it is possible to check whether a function exists before -// executing it, thus allowing a non-existent functions to be searched for and -// loaded from a source file, or perhaps dynamically defined. An example of what -// could be described as a custom autoload facility appears below. - -$colours = array('red', 'blue', 'green', 'yellow', 'orange', 'purple', 'violet'); - -foreach ($colours as $colour) -{ - $$colour = create_function('$text', 'global $colour; - return "<FONT COLOR=\'$colour\'>$text</FONT>";'); -} - -// Let's add a new colour to the list -array_push($colours, 'chartreuse'); - -foreach ($colours as $colour) -{ - // Checking whether function is defined - if (!function_exists($$colour)) - { - // Doesn't exist, so dynamically define it - $$colour = create_function('$text', 'global $colour; - return "<FONT COLOR=\'$colour\'>$text</FONT>";'); - - // Alternatively, if it exists in a source file, 'include' the file: - // include 'newcolours.php' - } - - echo $$colour("Careful with this $colour, James") . "\n"; -} - -foreach ($colours as $colour) unset($$colour); - -// @@PLEAC@@_10.16 -// *** Warning *** Whilst PHP *does* allow functions to be defined within other -// functions it needs to be clearly understood that these 'inner' functions: -// * Do not exist until the outer function is called a first time, at which time -// they then remain defined -// * Are global in scope, so are accessable outside the function by their name; -// the fact that they are nested within another function has, AFAICT, no bearing -// on name resolution -// * Do not form a closure: the inner function is merely 'parked' within the -// outer function, and has no implicit access to the outer function's variables -// or other inner functions - -function outer($arg) -{ - $x = $arg + 35; - function inner() { return $x * 19; } - - // *** wrong *** 'inner' returns 0 * 19, not ($arg + 35) * 19 - return $x + inner(); -} - -// ---------------------------- - -function outer($arg) -{ - $x = $arg + 35; - - // No implicit access to outer function scope; any required data must be - // explicity passed - function inner($x) { return $x * 19; } - - return $x + inner($x); -} - -// ------------ - -// Equivalent to previously-shown code -function inner($x) -{ - return $x * 19; -} - -function outer($arg) -{ - $x = $arg + 35; - return $x + inner($x); -} - -// @@PLEAC@@_10.17 -// @@INCOMPLETE@@ -// @@INCOMPLETE@@ - -// @@PLEAC@@_16.1 -// Run a command and return its results as a string. -$output_string = shell_exec('program args'); - -// Same as above, using backtick operator. -$output_string = `program args`; - -// Run a command and return its results as a list of strings, -// one per line. -$output_lines = array(); -exec('program args', $output_lines); - -// ----------------------------- - -// The only way to execute a program without using the shell is to -// use pcntl_exec(). However, there is no way to do redirection, so -// you can't capture its output. - -$pid = pcntl_fork(); -if ($pid == -1) { - die('cannot fork'); -} elseif ($pid) { - pcntl_waitpid($pid, $status); -} else { - // Note that pcntl_exec() automatically prepends the program name - // to the array of arguments; the program name cannot be spoofed. - pcntl_exec($program, array($arg1, $arg2)); -} - -// @@PLEAC@@_16.2 -// Run a simple command and retrieve its result code. -exec("vi $myfile", $output, $result_code); - -// ----------------------------- - -// Use the shell to perform redirection. -exec('cmd1 args | cmd2 | cmd3 >outfile'); -exec('cmd args <infile >outfile 2>errfile'); - -// ----------------------------- - -// Run a command, handling its result code or signal. -$pid = pcntl_fork(); -if ($pid == -1) { - die('cannot fork'); -} elseif ($pid) { - pcntl_waitpid($pid, $status); - if (pcntl_wifexited($status)) { - $status = pcntl_wexitstatus($status); - echo "program exited with status $status\n"; - } elseif (pcntl_wifsignaled($status)) { - $signal = pcntl_wtermsig($status); - echo "program killed by signal $signal\n"; - } elseif (pcntl_wifstopped($status)) { - $signal = pcntl_wstopsig($status); - echo "program stopped by signal $signal\n"; - } -} else { - pcntl_exec($program, $args); -} - -// ----------------------------- - -// Run a command while blocking interrupt signals. -$pid = pcntl_fork(); -if ($pid == -1) { - die('cannot fork'); -} elseif ($pid) { - // parent catches INT and berates user - declare(ticks = 1); - function handle_sigint($signal) { - echo "Tsk tsk, no process interruptus\n"; - } - pcntl_signal(SIGINT, 'handle_sigint'); - while (!pcntl_waitpid($pid, $status, WNOHANG)) {} -} else { - // child ignores INT and does its thing - pcntl_signal(SIGINT, SIG_IGN); - pcntl_exec('/bin/sleep', array('10')); -} - -// ----------------------------- - -// Since there is no direct access to execv() and friends, and -// pcntl_exec() won't let us supply an alternate program name -// in the argument list, there is no way to run a command with -// a different name in the process table. - -// @@PLEAC@@_16.3 -// Transfer control to the shell to run another program. -pcntl_exec('/bin/sh', array('-c', 'archive *.data')); -// Transfer control directly to another program. -pcntl_exec('/path/to/archive', array('accounting.data')); - -// @@PLEAC@@_16.4 -// Handle each line in the output of a process. -$readme = popen('program arguments', 'r'); -while (!feof($readme)) { - $line = fgets($readme); - if ($line === false) break; - // ... -} -pclose($readme); - -// ----------------------------- - -// Write to the input of a process. -$writeme = popen('program arguments', 'w'); -fwrite($writeme, 'data'); -pclose($writeme); - -// ----------------------------- - -// Wait for a process to complete. -$f = popen('sleep 1000000', 'r'); // child goes to sleep -pclose($f); // and parent goes to lala land - -// ----------------------------- - -$writeme = popen('program arguments', 'w'); -fwrite($writeme, "hello\n"); // program will get hello\n on STDIN -pclose($writeme); // program will get EOF on STDIN - -// ----------------------------- - -// Output buffering callback that sends output to the pager. -function ob_pager($output, $mode) { - static $pipe; - if ($mode & PHP_OUTPUT_HANDLER_START) { - $pager = getenv('PAGER'); - if (!$pager) $pager = '/usr/bin/less'; // XXX: might not exist - $pipe = popen($pager, 'w'); - } - fwrite($pipe, $output); - if ($mode & PHP_OUTPUT_HANDLER_END) { - pclose($pipe); - } -} - -// Redirect standard output to the pager. -ob_start('ob_pager'); - -// Do something useful that writes to standard output, then -// close the output buffer. -// ... -ob_end_flush(); - -// @@PLEAC@@_16.5 -// Output buffering: Only display a certain number of lines of output. -class Head { - function Head($lines=20) { - $this->lines = $lines; - } - - function filter($output, $mode) { - $result = array(); - $newline = ''; - if (strlen($output) > 0 && $output[strlen($output) - 1] == "\n") { - $newline = "\n"; - $output = substr($output, 0, -1); - } - foreach (explode("\n", $output) as $i => $line) { - if ($this->lines > 0) { - $this->lines--; - $result[] = $line; - } - } - return $result ? implode("\n", $result) . $newline : ''; - } -} - -// Output buffering: Prepend line numbers to each line of output. -class Number { - function Number() { - $this->line_number = 0; - } - - function filter($output, $mode) { - $result = array(); - $newline = ''; - if (strlen($output) > 0 && $output[strlen($output) - 1] == "\n") { - $newline = "\n"; - $output = substr($output, 0, -1); - } - foreach (explode("\n", $output) as $i => $line) { - $this->line_number++; - $result[] = $this->line_number . ': ' . $line; - } - return implode("\n", $result) . $newline; - } -} - -// Output buffering: Prepend "> " to each line of output. -class Quote { - function Quote() { - } - - function filter($output, $mode) { - $result = array(); - $newline = ''; - if (strlen($output) > 0 && $output[strlen($output) - 1] == "\n") { - $newline = "\n"; - $output = substr($output, 0, -1); - } - foreach (explode("\n", $output) as $i => $line) { - $result[] = "> $line"; - } - return implode("\n", $result) . $newline; - } -} - -// Use arrays as callbacks to register filter methods. -ob_start(array(new Head(100), 'filter')); -ob_start(array(new Number(), 'filter')); -ob_start(array(new Quote(), 'filter')); - -// Act like /bin/cat. -while (!feof(STDIN)) { - $line = fgets(STDIN); - if ($line === false) break; - echo $line; -} - -// Should match number of calls to ob_start(). -ob_end_flush(); -ob_end_flush(); -ob_end_flush(); - -// @@PLEAC@@_16.6 -// Process command-line arguments using fopen(). PHP supports URLs for -// filenames as long as the "allow_url_fopen" configuration option is set. -// -// Valid URL protocols include: -// - http://www.myserver.com/myfile.html -// - ftp://ftp.myserver.com/myfile.txt -// - compress.zlib://myfile.gz -// - php://stdin -// -// See http://www.php.net/manual/en/wrappers.php for details. -// -$filenames = array_slice($argv, 1); -if (!$filenames) $filenames = array('php://stdin'); -foreach ($filenames as $filename) { - $handle = @fopen($filename, 'r'); - if ($handle) { - while (!feof($handle)) { - $line = fgets($handle); - if ($line === false) break; - // ... - } - fclose($handle); - } else { - die("can't open $filename\n"); - } -} - -// @@PLEAC@@_16.7 -$output = `cmd 2>&1`; // with backticks -// or -$ph = popen('cmd 2>&1'); // with an open pipe -while (!feof($ph)) { $line = fgets($ph); } // plus a read -// ----------------------------- -$output = `cmd 2>/dev/null`; // with backticks -// or -$ph = popen('cmd 2>/dev/null'); // with an open pipe -while (!feof($ph)) { $line = fgets($ph); } // plus a read -// ----------------------------- -$output = `cmd 2>&1 1>/dev/null`; // with backticks -// or -$ph = popen('cmd 2>&1 1>/dev/null'); // with an open pipe -while (!feof($ph)) { $line = fgets($ph); } // plus a read -// ----------------------------- -$output = `cmd 3>&1 1>&2 2>&3 3>&-`; // with backticks -// or -$ph = popen('cmd 3>&1 1>&2 2>&3 3>&-|'); // with an open pipe -while (!feof($ph)) { $line = fgets($ph); } // plus a read -// ----------------------------- -exec('program args 1>/tmp/program.stdout 2>/tmp/program.stderr'); -// ----------------------------- -$output = `cmd 3>&1 1>&2 2>&3 3>&-`; -// ----------------------------- -$fd3 = $fd1; -$fd1 = $fd2; -$fd2 = $fd3; -$fd3 = null; -// ----------------------------- -exec('prog args 1>tmpfile 2>&1'); -exec('prog args 2>&1 1>tmpfile'); -// ----------------------------- -// exec('prog args 1>tmpfile 2>&1'); -$fd1 = "tmpfile"; // change stdout destination first -$fd2 = $fd1; // now point stderr there, too -// ----------------------------- -// exec('prog args 2>&1 1>tmpfile'); -$fd2 = $fd1; // stderr same destination as stdout -$fd1 = "tmpfile"; // but change stdout destination - -// @@PLEAC@@_16.8 -// Connect to input and output of a process. -$proc = proc_open($program, - array(0 => array('pipe', 'r'), - 1 => array('pipe', 'w')), - $pipes); -if (is_resource($proc)) { - fwrite($pipes[0], "here's your input\n"); - fclose($pipes[0]); - echo stream_get_contents($pipes[1]); - fclose($pipes[1]); - $result_code = proc_close($proc); - echo "$result_code\n"; -} - -// ----------------------------- - -$all = array(); -$outlines = array(); -$errlines = array(); -exec("( $cmd | sed -e 's/^/stdout: /' ) 2>&1", $all); -foreach ($all as $line) { - $pos = strpos($line, 'stdout: '); - if ($pos !== false && $pos == 0) { - $outlines[] = substr($line, 8); - } else { - $errlines[] = $line; - } -} -print("STDOUT:\n"); -print_r($outlines); -print("\n"); -print("STDERR:\n"); -print_r($errlines); -print("\n"); - -// @@PLEAC@@_16.9 -$proc = proc_open($cmd, - array(0 => array('pipe', 'r'), - 1 => array('pipe', 'w'), - 2 => array('pipe', 'w')), - $pipes); - -if (is_resource($proc)) { - // give end of file to kid, or feed him - fclose($pipes[0]); - - // read till EOF - $outlines = array(); - while (!feof($pipes[1])) { - $line = fgets($pipes[1]); - if ($line === false) break; - $outlines[] = rtrim($line); - } - - // XXX: block potential if massive - $errlines = array(); - while (!feof($pipes[2])) { - $line = fgets($pipes[2]); - if ($line === false) break; - $errlines[] = rtrim($line); - } - - fclose($pipes[1]); - fclose($pipes[2]); - proc_close($proc); - - print("STDOUT:\n"); - print_r($outlines); - print("\n"); - print("STDERR:\n"); - print_r($errlines); - print("\n"); -} - -// ----------------------------- - -// cmd3sel - control all three of kids in, out, and error. -$cmd = "grep vt33 /none/such - /etc/termcap"; -$proc = proc_open($cmd, - array(0 => array('pipe', 'r'), - 1 => array('pipe', 'w'), - 2 => array('pipe', 'w')), - $pipes); - -if (is_resource($proc)) { - fwrite($pipes[0], "This line has a vt33 lurking in it\n"); - fclose($pipes[0]); - - $readers = array($pipes[1], $pipes[2]); - while (stream_select($read=$readers, - $write=null, - $except=null, - 0, 200000) > 0) { - foreach ($read as $stream) { - $line = fgets($stream); - if ($line !== false) { - if ($stream === $pipes[1]) { - print "STDOUT: $line"; - } else { - print "STDERR: $line"; - } - } - if (feof($stream)) { - $readers = array_diff($readers, array($stream)); - } - } - } - - fclose($pipes[1]); - fclose($pipes[2]); - proc_close($proc); -} - -// @@PLEAC@@_16.10 -// PHP supports fork/exec/wait but not pipe. However, it does -// support socketpair, which can do everything pipes can as well -// as bidirectional communication. The original recipes have been -// modified here to use socketpair only. - -// ----------------------------- - -// pipe1 - use socketpair and fork so parent can send to child -$sockets = array(); -if (!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets)) { - die(socket_strerror(socket_last_error())); -} -list($reader, $writer) = $sockets; - -$pid = pcntl_fork(); -if ($pid == -1) { - die('cannot fork'); -} elseif ($pid) { - socket_close($reader); - $line = sprintf("Parent Pid %d is sending this\n", getmypid()); - if (!socket_write($writer, $line, strlen($line))) { - socket_close($writer); - die(socket_strerror(socket_last_error())); - } - socket_close($writer); - pcntl_waitpid($pid, $status); -} else { - socket_close($writer); - $line = socket_read($reader, 1024, PHP_NORMAL_READ); - printf("Child Pid %d just read this: `%s'\n", getmypid(), rtrim($line)); - socket_close($reader); // this will happen anyway - exit(0); -} - -// ----------------------------- - -// pipe2 - use socketpair and fork so child can send to parent -$sockets = array(); -if (!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets)) { - die(socket_strerror(socket_last_error())); -} -list($reader, $writer) = $sockets; - -$pid = pcntl_fork(); -if ($pid == -1) { - die('cannot fork'); -} elseif ($pid) { - socket_close($writer); - $line = socket_read($reader, 1024, PHP_NORMAL_READ); - printf("Parent Pid %d just read this: `%s'\n", getmypid(), rtrim($line)); - socket_close($reader); - pcntl_waitpid($pid, $status); -} else { - socket_close($reader); - $line = sprintf("Child Pid %d is sending this\n", getmypid()); - if (!socket_write($writer, $line, strlen($line))) { - socket_close($writer); - die(socket_strerror(socket_last_error())); - } - socket_close($writer); // this will happen anyway - exit(0); -} - -// ----------------------------- - -// pipe3 and pipe4 demonstrate the use of perl's "forking open" -// feature to reimplement pipe1 and pipe2. pipe5 uses two pipes -// to simulate socketpair. Since PHP supports socketpair but not -// pipe, and does not have a "forking open" feature, these -// examples are skipped here. - -// ----------------------------- - -// pipe6 - bidirectional communication using socketpair -$sockets = array(); -if (!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets)) { - die(socket_strerror(socket_last_error())); -} -list($child, $parent) = $sockets; - -$pid = pcntl_fork(); -if ($pid == -1) { - die('cannot fork'); -} elseif ($pid) { - socket_close($parent); - $line = sprintf("Parent Pid %d is sending this\n", getmypid()); - if (!socket_write($child, $line, strlen($line))) { - socket_close($child); - die(socket_strerror(socket_last_error())); - } - $line = socket_read($child, 1024, PHP_NORMAL_READ); - printf("Parent Pid %d just read this: `%s'\n", getmypid(), rtrim($line)); - socket_close($child); - pcntl_waitpid($pid, $status); -} else { - socket_close($child); - $line = socket_read($parent, 1024, PHP_NORMAL_READ); - printf("Child Pid %d just read this: `%s'\n", getmypid(), rtrim($line)); - $line = sprintf("Child Pid %d is sending this\n", getmypid()); - if (!socket_write($parent, $line, strlen($line))) { - socket_close($parent); - die(socket_strerror(socket_last_error())); - } - socket_close($parent); - exit(0); -} - -// @@PLEAC@@_16.11 -// ----------------------------- -// % mkfifo /path/to/named.pipe -// ----------------------------- - -$fifo = fopen('/path/to/named.pipe', 'r'); -if ($fifo !== false) { - while (!feof($fifo)) { - $line = fgets($fifo); - if ($line === false) break; - echo "Got: $line"; - } - fclose($fifo); -} else { - die('could not open fifo for read'); -} - -// ----------------------------- - -$fifo = fopen('/path/to/named.pipe', 'w'); -if ($fifo !== false) { - fwrite($fifo, "Smoke this.\n"); - fclose($fifo); -} else { - die('could not open fifo for write'); -} - -// ----------------------------- -// % mkfifo ~/.plan # isn't this everywhere yet? -// % mknod ~/.plan p # in case you don't have mkfifo -// ----------------------------- - -// dateplan - place current date and time in .plan file -while (true) { - $home = getenv('HOME'); - $fifo = fopen("$home/.plan", 'w'); - if ($fifo === false) { - die("Couldn't open $home/.plan for writing.\n"); - } - fwrite($fifo, - 'The current time is ' - . strftime('%a, %d %b %Y %H:%M:%S %z') - . "\n"); - fclose($fifo); - sleep(1); -} - -// ----------------------------- - -// fifolog - read and record log msgs from fifo - -$fifo = null; - -declare(ticks = 1); -function handle_alarm($signal) { - global $fifo; - if ($fifo) fclose($fifo); // move on to the next queued process -} -pcntl_signal(SIGALRM, 'handle_alarm'); - -while (true) { - pcntl_alarm(0); // turn off alarm for blocking open - $fifo = fopen('/tmp/log', 'r'); - if ($fifo === false) { - die("can't open /tmp/log"); - } - pcntl_alarm(1); // you have 1 second to log - - $service = fgets($fifo); - if ($service === false) continue; // interrupt or nothing logged - $service = rtrim($service); - - $message = fgets($fifo); - if ($message === false) continue; // interrupt or nothing logged - $message = rtrim($message); - - pcntl_alarm(0); // turn off alarms for message processing - - if ($service == 'http') { - // ignoring - } elseif ($service == 'login') { - // log to /var/log/login - $log = fopen('/var/log/login', 'a'); - if ($log !== false) { - fwrite($log, - strftime('%a, %d %b %Y %H:%M:%S %z') - . " $service $message\n"); - fclose($log); - } else { - trigger_error("Couldn't log $service $message to /var/log/login\n", - E_USER_WARNING); - } - } -} - -// @@PLEAC@@_16.12 -// sharetest - test shared variables across forks - -$SHM_KEY = ftok(__FILE__, chr(1)); -$handle = sem_get($SHM_KEY); -$buffer = shm_attach($handle, 1024); - -// The original recipe has an INT signal handler here. However, it -// causes erratic behavior with PHP, and PHP seems to do the right -// thing without it. - -for ($i = 0; $i < 10; $i++) { - $child = pcntl_fork(); - if ($child == -1) { - die('cannot fork'); - } elseif ($child) { - $kids[] = $child; // in case we care about their pids - } else { - squabble(); - exit(); - } -} - -while (true) { - print 'Buffer is ' . shm_get_var($buffer, 1) . "\n"; - sleep(1); -} -die('Not reached'); - -function squabble() { - global $handle; - global $buffer; - $i = 0; - $pid = getmypid(); - while (true) { - if (preg_match("/^$pid\\b/", shm_get_var($buffer, 1))) continue; - sem_acquire($handle); - $i++; - shm_put_var($buffer, 1, "$pid $i"); - sem_release($handle); - } -} - -// Buffer is 14357 1 -// Buffer is 14355 3 -// Buffer is 14355 4 -// Buffer is 14354 5 -// Buffer is 14353 6 -// Buffer is 14351 8 -// Buffer is 14351 9 -// Buffer is 14350 10 -// Buffer is 14348 11 -// Buffer is 14348 12 -// Buffer is 14357 10 -// Buffer is 14357 11 -// Buffer is 14355 13 -// ... - -// @@PLEAC@@_16.13 -// Available signal constants -% php -r 'print_r(get_defined_constants());' | grep '\[SIG' | grep -v _ - [SIGHUP] => 1 - [SIGINT] => 2 - [SIGQUIT] => 3 - [SIGILL] => 4 - [SIGTRAP] => 5 - [SIGABRT] => 6 - [SIGIOT] => 6 - [SIGBUS] => 7 - [SIGFPE] => 8 - [SIGKILL] => 9 - [SIGUSR1] => 10 - [SIGSEGV] => 11 - [SIGUSR2] => 12 - [SIGPIPE] => 13 - [SIGALRM] => 14 - [SIGTERM] => 15 - [SIGSTKFLT] => 16 - [SIGCLD] => 17 - [SIGCHLD] => 17 - [SIGCONT] => 18 - [SIGSTOP] => 19 - [SIGTSTP] => 20 - [SIGTTIN] => 21 - [SIGTTOU] => 22 - [SIGURG] => 23 - [SIGXCPU] => 24 - [SIGXFSZ] => 25 - [SIGVTALRM] => 26 - [SIGPROF] => 27 - [SIGWINCH] => 28 - [SIGPOLL] => 29 - [SIGIO] => 29 - [SIGPWR] => 30 - [SIGSYS] => 31 - [SIGBABY] => 31 - -// Predefined signal handler constants -% php -r 'print_r(get_defined_constants());' | grep '\[SIG' | grep _ - [SIG_IGN] => 1 - [SIG_DFL] => 0 - [SIG_ERR] => -1 - -// @@PLEAC@@_16.14 -// send pid a signal 9 -posix_kill($pid, 9); -// send whole job a signal 1 -posix_kill($pgrp, -1); -// send myself a SIGUSR1 -posix_kill(getmypid(), SIGUSR1); -// send a SIGHUP to processes in pids -foreach ($pids as $pid) posix_kill($pid, SIGHUP); - -// ----------------------------- - -// Use kill with pseudo-signal 0 to see if process is alive. -if (posix_kill($minion, 0)) { - echo "$minion is alive!\n"; -} else { - echo "$minion is deceased.\n"; -} - -// @@PLEAC@@_16.15 -// call got_sig_quit for every SIGQUIT -pcntl_signal(SIGQUIT, 'got_sig_quit'); -// call got_sig_pipe for every SIGPIPE -pcntl_signal(SIGPIPE, 'got_sig_pipe'); -// increment ouch for every SIGINT -function got_sig_int($signal) { global $ouch; $ouch++; } -pcntl_signal(SIGINT, 'got_sig_int'); -// ignore the signal INT -pcntl_signal(SIGINT, SIG_IGN); -// restore default STOP signal handling -pcntl_signal(SIGSTOP, SIG_DFL); - -// @@PLEAC@@_16.16 -// the signal handler -function ding($signal) { - fwrite(STDERR, "\x07Enter your name!\n"); -} - -// prompt for name, overriding SIGINT -function get_name() { - declare(ticks = 1); - pcntl_signal(SIGINT, 'ding'); - - echo "Kindly Stranger, please enter your name: "; - while (!@stream_select($read=array(STDIN), - $write=null, - $except=null, - 1)) { - // allow signals to be observed - } - $name = fgets(STDIN); - - // Since pcntl_signal() doesn't return the old signal handler, the - // best we can do here is set it back to the default behavior. - pcntl_signal(SIGINT, SIG_DFL); - - return $name; -} - -// @@PLEAC@@_16.17 -function got_int($signal) { - pcntl_signal(SIGINT, 'got_int'); // but not for SIGCHLD! - // ... -} -pcntl_signal(SIGINT, 'got_int'); - -// ----------------------------- - -declare(ticks = 1); -$interrupted = false; - -function got_int($signal) { - global $interrupted; - $interrupted = true; - // The third argument to pcntl_signal() determines if system calls - // should be restarted after a signal. It defaults to true. - pcntl_signal(SIGINT, 'got_int', false); // or SIG_IGN -} -pcntl_signal(SIGINT, 'got_int', false); - -// ... long-running code that you don't want to restart - -if ($interrupted) { - // deal with the signal -} - -// @@PLEAC@@_16.18 -// ignore signal INT -pcntl_signal(SIGINT, SIG_IGN); - -// install signal handler -declare(ticks = 1); -function tsktsk($signal) { - fwrite(STDERR, "\x07The long habit of living indisposeth us for dying."); - pcntl_signal(SIGINT, 'tsktsk'); -} -pcntl_signal(SIGINT, 'tsktsk'); - -// @@PLEAC@@_16.19 -pcntl_signal(SIGCHLD, SIG_IGN); - -// ----------------------------- - -declare(ticks = 1); -function reaper($signal) { - $pid = pcntl_waitpid(-1, $status, WNOHANG); - if ($pid > 0) { - // ... - reaper($signal); - } - // install *after* calling waitpid - pcntl_signal(SIGCHLD, 'reaper'); -} -pcntl_signal(SIGCHLD, 'reaper'); - -// ----------------------------- - -declare(ticks = 1); -function reaper($signal) { - $pid = pcntl_waitpid(-1, $status, WNOHANG); - if ($pid == -1) { - // No child waiting. Ignore it. - } else { - if (pcntl_wifexited($signal)) { - echo "Process $pid exited.\n"; - } else { - echo "False alarm on $pid\n"; - } - reaper($signal); - } - pcntl_signal(SIGCHLD, 'reaper'); -} -pcntl_signal(SIGCHLD, 'reaper'); - -// @@PLEAC@@_16.20 -// PHP does not support sigprocmask(). - -// @@PLEAC@@_16.21 -declare(ticks = 1); -$aborted = false; - -function handle_alarm($signal) { - global $aborted; - $aborted = true; -} -pcntl_signal(SIGALRM, 'handle_alarm'); - -pcntl_alarm(3600); -// long-time operations here -pcntl_alarm(0); -if ($aborted) { - // timed out - do what you will here -} diff --git a/test/scanners/php/strings.expected.raydebug b/test/scanners/php/strings.expected.raydebug deleted file mode 100644 index c8e3e84..0000000 --- a/test/scanners/php/strings.expected.raydebug +++ /dev/null @@ -1,360 +0,0 @@ -plain(Single quoted -)inline_delimiter(<?php) -predefined(echo) string<delimiter(')content(this is a simple string)delimiter(')>operator(;) - -predefined(echo) string<delimiter(')content(You can also have embedded newlines in -strings this way as it is -okay to do)delimiter(')>operator(;) - -comment(// Outputs: Arnold once said: "I'll be back") -predefined(echo) string<delimiter(')content(Arnold once said: "I)char(\\')content(ll be back")delimiter(')>operator(;) - -comment(// Outputs: You deleted C:\\*.*?) -predefined(echo) string<delimiter(')content(You deleted C:)char(\\\\)content(*.*?)delimiter(')>operator(;) - -comment(// Outputs: You deleted C:\\*.*?) -predefined(echo) string<delimiter(')content(You deleted C:)content(\\*)content(.*?)delimiter(')>operator(;) - -comment(// Outputs: This will not expand: \\n a newline) -predefined(echo) string<delimiter(')content(This will not expand: )content(\\n)content( a newline)delimiter(')>operator(;) - -comment(// Outputs: Variables do not $expand $either) -predefined(echo) string<delimiter(')content(Variables do not $expand $either)delimiter(')>operator(;) -inline_delimiter(?>) - -plain(Double quoted - -)inline_delimiter(<?php) -local_variable($escape_sequences) operator(=) string<delimiter(")char(\\n)char(\\r)char(\\t)char(\\v)char(\\f)char(\\\\)char(\\$)char(\\")char(\\000)content(-)char(\\777)char(\\x0)content(-)char(\\xFF)delimiter(")>operator(;) -inline_delimiter(?>) - -plain(Heredoc - -Example #1 Invalid example -)inline_delimiter(<?php) -reserved(class) class(foo) operator({) - reserved(public) local_variable($bar) operator(=) string<delimiter(<<<EOT)content( -)content(bar)content( -)delimiter(EOT)>operator(;) -operator(}) -inline_delimiter(?>) - -plain(Example #2 Heredoc string quoting example - -)inline_delimiter(<?php) -local_variable($str) operator(=) string<delimiter(<<<EOD)content( -)content(Example of string)content( -)content(spanning multiple lines)content( -)content(using heredoc syntax.)content( -)delimiter(EOD)>operator(;) - -comment(/* More complex example, with variables. */) -reserved(class) class(foo) -operator({) - reserved(var) local_variable($foo)operator(;) - reserved(var) local_variable($bar)operator(;) - - reserved(function) function(foo)operator(()operator(\)) - operator({) - local_variable($this)operator(->)ident(foo) operator(=) string<delimiter(')content(Foo)delimiter(')>operator(;) - local_variable($this)operator(->)ident(bar) operator(=) predefined(array)operator(()string<delimiter(')content(Bar1)delimiter(')>operator(,) string<delimiter(')content(Bar2)delimiter(')>operator(,) string<delimiter(')content(Bar3)delimiter(')>operator(\))operator(;) - operator(}) -operator(}) - -local_variable($foo) operator(=) reserved(new) ident(foo)operator(()operator(\))operator(;) -local_variable($name) operator(=) string<delimiter(')content(MyName)delimiter(')>operator(;) - -predefined(echo) string<delimiter(<<<EOT)content( -)content(My name is ")local_variable($name)content(". I am printing some )inline<local_variable($foo)operator(->)ident(foo)>content(.)content( -)content(Now, I am printing some )inline<delimiter({)local_variable($foo)operator(->)ident(bar)operator([)integer(1)operator(])delimiter(})>content(.)content( -)content(This should print a capital 'A': )char(\\x41)content( -)delimiter(EOT)>operator(;) -inline_delimiter(?>) - -plain(The above example will output: - -My name is "MyName". I am printing some Foo. -Now, I am printing some Bar2. -This should print a capital 'A': A - -Example #3 Heredoc in arguments example - -)inline_delimiter(<?php) -predefined(var_dump)operator(()predefined(array)operator(()string<delimiter(<<<EOD)content( -)content(foobar!)content( -)delimiter(EOD)> -operator(\))operator(\))operator(;) -inline_delimiter(?>) - -plain(Example #4 Using Heredoc to initialize static values - -)inline_delimiter(<?php) -comment(// Static variables) -reserved(function) function(foo)operator(()operator(\)) -operator({) - reserved(static) local_variable($bar) operator(=) string<delimiter(<<<LABEL)content( -)content(Nothing in here...)content( -)delimiter(LABEL)>operator(;) -operator(}) - -comment(// Class properties/constants) -reserved(class) class(foo) -operator({) - reserved(const) constant(BAR) operator(=) string<delimiter(<<<FOOBAR)content( -)content(Constant example)content( -)delimiter(FOOBAR)>operator(;) - - reserved(public) local_variable($baz) operator(=) string<delimiter(<<<FOOBAR)content( -)content(Property example)content( -)delimiter(FOOBAR)>operator(;) -operator(}) -inline_delimiter(?>) - -plain(Example #5 Using double quotes in Heredoc - -)inline_delimiter(<?php) -predefined(echo) string<delimiter(<<<"FOOBAR")content( -)content(Hello )local_variable($World)content(!)content( -)delimiter(FOOBAR)>operator(;) -inline_delimiter(?>) - -plain(Nowdoc - -Example #6 Nowdoc string quoting example - -)inline_delimiter(<?php) -local_variable($str) operator(=) string<delimiter(<<<'EOD')content( -)content(Example of string)content( -)content(spanning multiple lines)content( -)content(using nowdoc syntax.)content( -)delimiter(EOD)>operator(;) - -comment(/* More complex example, with variables. */) -reserved(class) class(foo) -operator({) - reserved(public) local_variable($foo)operator(;) - reserved(public) local_variable($bar)operator(;) - - reserved(function) function(foo)operator(()operator(\)) - operator({) - local_variable($this)operator(->)ident(foo) operator(=) string<delimiter(')content(Foo)delimiter(')>operator(;) - local_variable($this)operator(->)ident(bar) operator(=) predefined(array)operator(()string<delimiter(')content(Bar1)delimiter(')>operator(,) string<delimiter(')content(Bar2)delimiter(')>operator(,) string<delimiter(')content(Bar3)delimiter(')>operator(\))operator(;) - operator(}) -operator(}) - -local_variable($foo) operator(=) reserved(new) ident(foo)operator(()operator(\))operator(;) -local_variable($name) operator(=) string<delimiter(')content(MyName)delimiter(')>operator(;) - -predefined(echo) string<delimiter(<<<'EOT')content( -)content(My name is "$name". I am printing some $foo->foo.)content( -)content(Now, I am printing some {$foo->bar[1]}.)content( -)content(This should not print a capital 'A': )content(\\x)content(41)content( -)delimiter(EOT)>operator(;) -inline_delimiter(?>) -plain(The above example will output: - -My name is "$name". I am printing some $foo-)error(>)plain(foo. -Now, I am printing some {$foo-)error(>)plain(bar[1]}. -This should not print a capital 'A': \\x41 - -Example #7 Static data example - -)inline_delimiter(<?php) -reserved(class) class(foo) operator({) - reserved(public) local_variable($bar) operator(=) string<delimiter(<<<'EOT')content( -)content(bar)content( -)delimiter(EOT)>operator(;) -operator(}) -inline_delimiter(?>) - -plain(Variable parsing - -When a string is specified in double quotes or with heredoc, variables are parsed within it. - -There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort. - -The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. - -Simple syntax - -If a dollar sign ($\) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. - -)inline_delimiter(<?php) -local_variable($beer) operator(=) string<delimiter(')content(Heineken)delimiter(')>operator(;) -predefined(echo) string<delimiter(")local_variable($beer)content('s taste is great)delimiter(")>operator(;) comment(// works; "'" is an invalid character for variable names) -predefined(echo) string<delimiter(")content(He drank some )local_variable($beers)delimiter(")>operator(;) comment(// won't work; 's' is a valid character for variable names but the variable is "$beer") -predefined(echo) string<delimiter(")content(He drank some )local_variable(${beer})content(s)delimiter(")>operator(;) comment(// works) -predefined(echo) string<delimiter(")content(He drank some )inline<delimiter({)local_variable($beer)delimiter(})>content(s)delimiter(")>operator(;) comment(// works) -inline_delimiter(?>) -plain(Similarly, an array index or an object property can be parsed. With array indices, the closing square bracket (]\) marks the end of the index. The same rules apply to object properties as to simple variables. - -)inline_delimiter(<?php) -comment(// These examples are specific to using arrays inside of strings.) -comment(// When outside of a string, always quote array string keys and do not use) -comment(// {braces}.) - -comment(// Show all errors) -predefined(error_reporting)operator(()exception(E_ALL)operator(\))operator(;) - -local_variable($fruits) operator(=) predefined(array)operator(()string<delimiter(')content(strawberry)delimiter(')> operator(=>) string<delimiter(')content(red)delimiter(')>operator(,) string<delimiter(')content(banana)delimiter(')> operator(=>) string<delimiter(')content(yellow)delimiter(')>operator(\))operator(;) - -comment(// Works, but note that this works differently outside a string) -predefined(echo) string<delimiter(")content(A banana is )inline<local_variable($fruits)operator([)ident(banana)operator(])>content(.)delimiter(")>operator(;) - -comment(// Works) -predefined(echo) string<delimiter(")content(A banana is )inline<delimiter({)local_variable($fruits)operator([)string<delimiter(')content(banana)delimiter(')>operator(])delimiter(})>content(.)delimiter(")>operator(;) - -comment(// Works, but PHP looks for a constant named banana first, as described below.) -predefined(echo) string<delimiter(")content(A banana is )inline<delimiter({)local_variable($fruits)operator([)ident(banana)operator(])delimiter(})>content(.)delimiter(")>operator(;) - -comment(// Won't work, use braces. This results in a parse error.) -predefined(echo) string<delimiter(")content(A banana is )error($fruits['banana'])content(.)delimiter(")>operator(;) - -comment(// Works) -predefined(echo) string<delimiter(")content(A banana is )delimiter(")> operator(.) local_variable($fruits)operator([)string<delimiter(')content(banana)delimiter(')>operator(]) operator(.) string<delimiter(")content(.)delimiter(")>operator(;) - -comment(// Works) -predefined(echo) string<delimiter(")content(This square is )inline<local_variable($square)operator(->)ident(width)>content( meters broad.)delimiter(")>operator(;) - -comment(// Won't work. For a solution, see the complex syntax.) -predefined(echo) string<delimiter(")content(This square is )inline<local_variable($square)operator(->)ident(width00)>content( centimeters broad.)delimiter(")>operator(;) -inline_delimiter(?>) -plain(For anything more complex, you should use the complex syntax. - -Complex (curly\) syntax - -This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. - -In fact, any value in the namespace can be included in a string with this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\\$ to get a literal {$. Some examples to make it clear: - -)inline_delimiter(<?php) -comment(// Show all errors) -predefined(error_reporting)operator(()exception(E_ALL)operator(\))operator(;) - -local_variable($great) operator(=) string<delimiter(')content(fantastic)delimiter(')>operator(;) - -comment(// Won't work, outputs: This is { fantastic}) -predefined(echo) string<delimiter(")content(This is )string({)content( )local_variable($great)content(})delimiter(")>operator(;) - -comment(// Works, outputs: This is fantastic) -predefined(echo) string<delimiter(")content(This is )inline<delimiter({)local_variable($great)delimiter(})>delimiter(")>operator(;) -predefined(echo) string<delimiter(")content(This is )local_variable(${great})delimiter(")>operator(;) - -comment(// Works) -predefined(echo) string<delimiter(")content(This square is )inline<delimiter({)local_variable($square)operator(->)ident(width)delimiter(})>content(00 centimeters broad.)delimiter(")>operator(;) - -comment(// Works) -predefined(echo) string<delimiter(")content(This works: )inline<delimiter({)local_variable($arr)operator([)integer(4)operator(])operator([)integer(3)operator(])delimiter(})>delimiter(")>operator(;) - -comment(// This is wrong for the same reason as $foo[bar] is wrong outside a string.) -comment(// In other words, it will still work, but only because PHP first looks for a) -comment(// constant named foo; an error of level E_NOTICE (undefined constant\) will be) -comment(// thrown.) -predefined(echo) string<delimiter(")content(This is wrong: )inline<delimiter({)local_variable($arr)operator([)ident(foo)operator(])operator([)integer(3)operator(])delimiter(})>delimiter(")>operator(;) - -comment(// Works. When using multi-dimensional arrays, always use braces around arrays) -comment(// when inside of strings) -predefined(echo) string<delimiter(")content(This works: )inline<delimiter({)local_variable($arr)operator([)string<delimiter(')content(foo)delimiter(')>operator(])operator([)integer(3)operator(])delimiter(})>delimiter(")>operator(;) - -comment(// Works.) -predefined(echo) string<delimiter(")content(This works: )delimiter(")> operator(.) local_variable($arr)operator([)string<delimiter(')content(foo)delimiter(')>operator(])operator([)integer(3)operator(])operator(;) - -predefined(echo) string<delimiter(")content(This works too: )inline<delimiter({)local_variable($obj)operator(->)ident(values)operator([)integer(3)operator(])operator(->)ident(name)delimiter(})>delimiter(")>operator(;) - -predefined(echo) string<delimiter(")content(This is the value of the var named )local_variable($name)content(: )inline<delimiter({)operator($)operator({)local_variable($name)operator(})delimiter(})>delimiter(")>operator(;) - -predefined(echo) string<delimiter(")content(This is the value of the var named by the return value of getName(\): )inline<delimiter({)operator($)operator({)ident(getName)operator(()operator(\))operator(})delimiter(})>delimiter(")>operator(;) - -predefined(echo) string<delimiter(")content(This is the value of the var named by the return value of )char(\\$)content(object->getName(\): )inline<delimiter({)operator($)operator({)local_variable($object)operator(->)ident(getName)operator(()operator(\))operator(})delimiter(})>delimiter(")>operator(;) -inline_delimiter(?>) -plain(It is also possible to access class properties using variables within strings using this syntax. - -)inline_delimiter(<?php) -reserved(class) class(foo) operator({) - reserved(var) local_variable($bar) operator(=) string<delimiter(')content(I am bar.)delimiter(')>operator(;) -operator(}) - -local_variable($foo) operator(=) reserved(new) ident(foo)operator(()operator(\))operator(;) -local_variable($bar) operator(=) string<delimiter(')content(bar)delimiter(')>operator(;) -local_variable($baz) operator(=) predefined(array)operator(()string<delimiter(')content(foo)delimiter(')>operator(,) string<delimiter(')content(bar)delimiter(')>operator(,) string<delimiter(')content(baz)delimiter(')>operator(,) string<delimiter(')content(quux)delimiter(')>operator(\))operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($foo)operator(->)local_variable($bar)delimiter(})>char(\\n)delimiter(")>operator(;) -predefined(echo) string<delimiter(")inline<delimiter({)local_variable($foo)operator(->)local_variable($baz)operator([)integer(1)operator(])delimiter(})>char(\\n)delimiter(")>operator(;) -inline_delimiter(?>) -plain(The above example will output: -I am bar. -I am bar. - -Note: Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}\) will not work for accessing the return values of functions or methods or the values of class constants or static class variables. -)inline_delimiter(<?php) -comment(// Show all errors.) -predefined(error_reporting)operator(()exception(E_ALL)operator(\))operator(;) - -reserved(class) class(beers) operator({) - reserved(const) ident(softdrink) operator(=) string<delimiter(')content(rootbeer)delimiter(')>operator(;) - reserved(public) reserved(static) local_variable($ale) operator(=) string<delimiter(')content(ipa)delimiter(')>operator(;) -operator(}) - -local_variable($rootbeer) operator(=) string<delimiter(')content(A & W)delimiter(')>operator(;) -local_variable($ipa) operator(=) string<delimiter(')content(Alexander Keith)char(\\')content(s)delimiter(')>operator(;) - -comment(// This works; outputs: I'd like an A & W) -predefined(echo) string<delimiter(")content(I'd like an )inline<delimiter({)operator($)operator({)ident(beers)operator(::)ident(softdrink)operator(})delimiter(})>char(\\n)delimiter(")>operator(;) - -comment(// This works too; outputs: I'd like an Alexander Keith's) -predefined(echo) string<delimiter(")content(I'd like an )inline<delimiter({)operator($)operator({)ident(beers)operator(::)local_variable($ale)operator(})delimiter(})>char(\\n)delimiter(")>operator(;) -inline_delimiter(?>) -plain(String access and modification by character - -Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. - -Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42]. - -Warning -Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte. - -Example #8 Some string examples -)inline_delimiter(<?php) -comment(// Get the first character of a string) -local_variable($str) operator(=) string<delimiter(')content(This is a test.)delimiter(')>operator(;) -local_variable($first) operator(=) local_variable($str)operator([)integer(0)operator(])operator(;) - -comment(// Get the third character of a string) -local_variable($third) operator(=) local_variable($str)operator([)integer(2)operator(])operator(;) - -comment(// Get the last character of a string.) -local_variable($str) operator(=) string<delimiter(')content(This is still a test.)delimiter(')>operator(;) -local_variable($last) operator(=) local_variable($str)operator([)predefined(strlen)operator(()local_variable($str)operator(\))operator(-)integer(1)operator(])operator(;) - -comment(// Modify the last character of a string) -local_variable($str) operator(=) string<delimiter(')content(Look at the sea)delimiter(')>operator(;) -local_variable($str)operator([)predefined(strlen)operator(()local_variable($str)operator(\))operator(-)integer(1)operator(]) operator(=) string<delimiter(')content(e)delimiter(')>operator(;) - -inline_delimiter(?>) - -plain(String conversion to numbers - -)inline_delimiter(<?php) -local_variable($foo) operator(=) integer(1) operator(+) string<delimiter(")content(10.5)delimiter(")>operator(;) comment(// $foo is float (11.5\)) -local_variable($foo) operator(=) integer(1) operator(+) string<delimiter(")content(-1.3e3)delimiter(")>operator(;) comment(// $foo is float (-1299\)) -local_variable($foo) operator(=) integer(1) operator(+) string<delimiter(")content(bob-1.3e3)delimiter(")>operator(;) comment(// $foo is integer (1\)) -local_variable($foo) operator(=) integer(1) operator(+) string<delimiter(")content(bob3)delimiter(")>operator(;) comment(// $foo is integer (1\)) -local_variable($foo) operator(=) integer(1) operator(+) string<delimiter(")content(10 Small Pigs)delimiter(")>operator(;) comment(// $foo is integer (11\)) -local_variable($foo) operator(=) integer(4) operator(+) string<delimiter(")content(10.2 Little Piggies)delimiter(")>operator(;) comment(// $foo is float (14.2\)) -local_variable($foo) operator(=) string<delimiter(")content(10.0 pigs )delimiter(")> operator(+) integer(1)operator(;) comment(// $foo is float (11\)) -local_variable($foo) operator(=) string<delimiter(")content(10.0 pigs )delimiter(")> operator(+) float(1.0)operator(;) comment(// $foo is float (11\) ) -inline_delimiter(?>) - -inline_delimiter(<?php) -predefined(echo) string<delimiter(")char(\\$)content(foo==)local_variable($foo)content(; type is )delimiter(")> operator(.) predefined(gettype) operator(()local_variable($foo)operator(\)) operator(.) string<delimiter(")content(<br />)char(\\n)delimiter(")>operator(;) -inline_delimiter(?>) - -plain(If you want a parsed variable surrounded by curly braces, just double the curly braces: - -)inline_delimiter(<?php) - local_variable($foo) operator(=) string<delimiter(")content(bar)delimiter(")>operator(;) - predefined(echo) string<delimiter(")string({)inline<delimiter({)local_variable($foo)delimiter(})>content(})delimiter(")>operator(;) -inline_delimiter(?>) - -plain(Although current documentation says 'A string literal can be specified in four different ways: ...', actually there is a fifth way to specify a (binary\) string: - -)inline_delimiter(<?php) local_variable($binary) operator(=) string<modifier(b)delimiter(')content(This is a binary string)delimiter(')>operator(;) inline_delimiter(?>) diff --git a/test/scanners/php/strings.in.php b/test/scanners/php/strings.in.php deleted file mode 100644 index dd6ffc7..0000000 --- a/test/scanners/php/strings.in.php +++ /dev/null @@ -1,360 +0,0 @@ -Single quoted -<?php -echo 'this is a simple string'; - -echo 'You can also have embedded newlines in -strings this way as it is -okay to do'; - -// Outputs: Arnold once said: "I'll be back" -echo 'Arnold once said: "I\'ll be back"'; - -// Outputs: You deleted C:\*.*? -echo 'You deleted C:\\*.*?'; - -// Outputs: You deleted C:\*.*? -echo 'You deleted C:\*.*?'; - -// Outputs: This will not expand: \n a newline -echo 'This will not expand: \n a newline'; - -// Outputs: Variables do not $expand $either -echo 'Variables do not $expand $either'; -?> - -Double quoted - -<?php -$escape_sequences = "\n\r\t\v\f\\\$\"\000-\777\x0-\xFF"; -?> - -Heredoc - -Example #1 Invalid example -<?php -class foo { - public $bar = <<<EOT -bar -EOT; -} -?> - -Example #2 Heredoc string quoting example - -<?php -$str = <<<EOD -Example of string -spanning multiple lines -using heredoc syntax. -EOD; - -/* More complex example, with variables. */ -class foo -{ - var $foo; - var $bar; - - function foo() - { - $this->foo = 'Foo'; - $this->bar = array('Bar1', 'Bar2', 'Bar3'); - } -} - -$foo = new foo(); -$name = 'MyName'; - -echo <<<EOT -My name is "$name". I am printing some $foo->foo. -Now, I am printing some {$foo->bar[1]}. -This should print a capital 'A': \x41 -EOT; -?> - -The above example will output: - -My name is "MyName". I am printing some Foo. -Now, I am printing some Bar2. -This should print a capital 'A': A - -Example #3 Heredoc in arguments example - -<?php -var_dump(array(<<<EOD -foobar! -EOD -)); -?> - -Example #4 Using Heredoc to initialize static values - -<?php -// Static variables -function foo() -{ - static $bar = <<<LABEL -Nothing in here... -LABEL; -} - -// Class properties/constants -class foo -{ - const BAR = <<<FOOBAR -Constant example -FOOBAR; - - public $baz = <<<FOOBAR -Property example -FOOBAR; -} -?> - -Example #5 Using double quotes in Heredoc - -<?php -echo <<<"FOOBAR" -Hello $World! -FOOBAR; -?> - -Nowdoc - -Example #6 Nowdoc string quoting example - -<?php -$str = <<<'EOD' -Example of string -spanning multiple lines -using nowdoc syntax. -EOD; - -/* More complex example, with variables. */ -class foo -{ - public $foo; - public $bar; - - function foo() - { - $this->foo = 'Foo'; - $this->bar = array('Bar1', 'Bar2', 'Bar3'); - } -} - -$foo = new foo(); -$name = 'MyName'; - -echo <<<'EOT' -My name is "$name". I am printing some $foo->foo. -Now, I am printing some {$foo->bar[1]}. -This should not print a capital 'A': \x41 -EOT; -?> -The above example will output: - -My name is "$name". I am printing some $foo->foo. -Now, I am printing some {$foo->bar[1]}. -This should not print a capital 'A': \x41 - -Example #7 Static data example - -<?php -class foo { - public $bar = <<<'EOT' -bar -EOT; -} -?> - -Variable parsing - -When a string is specified in double quotes or with heredoc, variables are parsed within it. - -There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort. - -The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. - -Simple syntax - -If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. - -<?php -$beer = 'Heineken'; -echo "$beer's taste is great"; // works; "'" is an invalid character for variable names -echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer" -echo "He drank some ${beer}s"; // works -echo "He drank some {$beer}s"; // works -?> -Similarly, an array index or an object property can be parsed. With array indices, the closing square bracket (]) marks the end of the index. The same rules apply to object properties as to simple variables. - -<?php -// These examples are specific to using arrays inside of strings. -// When outside of a string, always quote array string keys and do not use -// {braces}. - -// Show all errors -error_reporting(E_ALL); - -$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); - -// Works, but note that this works differently outside a string -echo "A banana is $fruits[banana]."; - -// Works -echo "A banana is {$fruits['banana']}."; - -// Works, but PHP looks for a constant named banana first, as described below. -echo "A banana is {$fruits[banana]}."; - -// Won't work, use braces. This results in a parse error. -echo "A banana is $fruits['banana']."; - -// Works -echo "A banana is " . $fruits['banana'] . "."; - -// Works -echo "This square is $square->width meters broad."; - -// Won't work. For a solution, see the complex syntax. -echo "This square is $square->width00 centimeters broad."; -?> -For anything more complex, you should use the complex syntax. - -Complex (curly) syntax - -This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. - -In fact, any value in the namespace can be included in a string with this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$. Some examples to make it clear: - -<?php -// Show all errors -error_reporting(E_ALL); - -$great = 'fantastic'; - -// Won't work, outputs: This is { fantastic} -echo "This is { $great}"; - -// Works, outputs: This is fantastic -echo "This is {$great}"; -echo "This is ${great}"; - -// Works -echo "This square is {$square->width}00 centimeters broad."; - -// Works -echo "This works: {$arr[4][3]}"; - -// This is wrong for the same reason as $foo[bar] is wrong outside a string. -// In other words, it will still work, but only because PHP first looks for a -// constant named foo; an error of level E_NOTICE (undefined constant) will be -// thrown. -echo "This is wrong: {$arr[foo][3]}"; - -// Works. When using multi-dimensional arrays, always use braces around arrays -// when inside of strings -echo "This works: {$arr['foo'][3]}"; - -// Works. -echo "This works: " . $arr['foo'][3]; - -echo "This works too: {$obj->values[3]->name}"; - -echo "This is the value of the var named $name: {${$name}}"; - -echo "This is the value of the var named by the return value of getName(): {${getName()}}"; - -echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"; -?> -It is also possible to access class properties using variables within strings using this syntax. - -<?php -class foo { - var $bar = 'I am bar.'; -} - -$foo = new foo(); -$bar = 'bar'; -$baz = array('foo', 'bar', 'baz', 'quux'); -echo "{$foo->$bar}\n"; -echo "{$foo->$baz[1]}\n"; -?> -The above example will output: -I am bar. -I am bar. - -Note: Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables. -<?php -// Show all errors. -error_reporting(E_ALL); - -class beers { - const softdrink = 'rootbeer'; - public static $ale = 'ipa'; -} - -$rootbeer = 'A & W'; -$ipa = 'Alexander Keith\'s'; - -// This works; outputs: I'd like an A & W -echo "I'd like an {${beers::softdrink}}\n"; - -// This works too; outputs: I'd like an Alexander Keith's -echo "I'd like an {${beers::$ale}}\n"; -?> -String access and modification by character - -Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. - -Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42]. - -Warning -Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte. - -Example #8 Some string examples -<?php -// Get the first character of a string -$str = 'This is a test.'; -$first = $str[0]; - -// Get the third character of a string -$third = $str[2]; - -// Get the last character of a string. -$str = 'This is still a test.'; -$last = $str[strlen($str)-1]; - -// Modify the last character of a string -$str = 'Look at the sea'; -$str[strlen($str)-1] = 'e'; - -?> - -String conversion to numbers - -<?php -$foo = 1 + "10.5"; // $foo is float (11.5) -$foo = 1 + "-1.3e3"; // $foo is float (-1299) -$foo = 1 + "bob-1.3e3"; // $foo is integer (1) -$foo = 1 + "bob3"; // $foo is integer (1) -$foo = 1 + "10 Small Pigs"; // $foo is integer (11) -$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2) -$foo = "10.0 pigs " + 1; // $foo is float (11) -$foo = "10.0 pigs " + 1.0; // $foo is float (11) -?> - -<?php -echo "\$foo==$foo; type is " . gettype ($foo) . "<br />\n"; -?> - -If you want a parsed variable surrounded by curly braces, just double the curly braces: - -<?php - $foo = "bar"; - echo "{{$foo}}"; -?> - -Although current documentation says 'A string literal can be specified in four different ways: ...', actually there is a fifth way to specify a (binary) string: - -<?php $binary = b'This is a binary string'; ?> diff --git a/test/scanners/php/suite.rb b/test/scanners/php/suite.rb deleted file mode 100644 index 8d72ce3..0000000 --- a/test/scanners/php/suite.rb +++ /dev/null @@ -1,2 +0,0 @@ -class PHP < CodeRay::TestCase -end diff --git a/test/scanners/php/test.expected.raydebug b/test/scanners/php/test.expected.raydebug deleted file mode 100644 index 652e366..0000000 --- a/test/scanners/php/test.expected.raydebug +++ /dev/null @@ -1,498 +0,0 @@ -inline_delimiter(<?php) -comment(/** - * Zip class file - * - * @package fnord.bb - * @subpackage archive - */) - -comment(// Unlock?) -reserved(if)operator(()operator(!)predefined(defined)operator(()string<delimiter(')content(UNLOCK)delimiter(')>operator(\)) operator(||) operator(!)constant(UNLOCK)operator(\)) - predefined(die)operator(()operator(\))operator(;) - -comment(// Load the parent archive class) -predefined(require_once)operator(()constant(ROOT_PATH)operator(.)string<delimiter(')content(/classes/archive.class.php)delimiter(')>operator(\))operator(;) - -comment(/** - * Zip class - * - * @author Manni <manni@fnord.name> - * @copyright Copyright (c\) 2006, Manni - * @version 1.0 - * @link http://www.pkware.com/business_and_developers/developer/popups/appnote.txt - * @link http://mannithedark.is-a-geek.net/ - * @since 1.0 - * @package fnord.bb - * @subpackage archive - */) -reserved(class) class(Zip) reserved(extends) constant(Archive) operator({) - comment(/** - * Outputs the zip file - * - * This function creates the zip file with the dirs and files given. - * If the optional parameter $file is given, the zip file is will be - * saved at that location. Otherwise the function returns the zip file's content. - * - * @access public - * - * @link http://www.pkware.com/business_and_developers/developer/popups/appnote.txt - * @param string $filename The path where the zip file will be saved - * - * @return bool|string Returns either true if the fil is sucessfully created or the content of the zip file - */) - reserved(function) function(out)operator(()local_variable($filename) operator(=) pre_constant(false)operator(\)) operator({) - comment(// Empty output) - local_variable($file_data) operator(=) predefined(array)operator(()operator(\))operator(;) comment(// Data of the file part) - local_variable($cd_data) operator(=) predefined(array)operator(()operator(\))operator(;) comment(// Data of the central directory) - - comment(// Sort dirs and files by path length) - predefined(uksort)operator(()local_variable($this)operator(->)ident(dirs)operator(,) string<delimiter(')content(sort_by_length)delimiter(')>operator(\))operator(;) - predefined(uksort)operator(()local_variable($this)operator(->)ident(files)operator(,) string<delimiter(')content(sort_by_length)delimiter(')>operator(\))operator(;) - - comment(// Handle dirs) - reserved(foreach)operator(()local_variable($this)operator(->)ident(dirs) reserved(as) local_variable($dir)operator(\)) operator({) - local_variable($dir) operator(.=) string<delimiter(')content(/)delimiter(')>operator(;) - comment(// File part) - - comment(// Reset dir data) - local_variable($dir_data) operator(=) string<delimiter(')delimiter(')>operator(;) - - comment(// Local file header) - local_variable($dir_data) operator(.=) string<delimiter(")char(\\x50)char(\\x4b)char(\\x03)char(\\x04)delimiter(")>operator(;) comment(// Local file header signature) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(10)operator(\))operator(;) comment(// Version needed to extract) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// General purpose bit flag) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Compression method) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file time) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file date) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// crc-32) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Compressed size) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Uncompressed size) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($dir)operator(\))operator(\))operator(;) comment(// File name length) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Extra field length) - - local_variable($dir_data) operator(.=) local_variable($dir)operator(;) comment(// File name) - local_variable($dir_data) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// Extra field (is empty\)) - - comment(// File data) - local_variable($dir_data) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// Dirs have no file data) - - comment(// Data descriptor) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// crc-32) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Compressed size) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Uncompressed size) - - comment(// Save current offset) - local_variable($offset) operator(=) predefined(strlen)operator(()predefined(implode)operator(()string<delimiter(')delimiter(')>operator(,) local_variable($file_data)operator(\))operator(\))operator(;) - - comment(// Append dir data to the file part) - local_variable($file_data)operator([)operator(]) operator(=) local_variable($dir_data)operator(;) - - comment(// Central directory) - - comment(// Reset dir data) - local_variable($dir_data) operator(=) string<delimiter(')delimiter(')>operator(;) - - comment(// File header) - local_variable($dir_data) operator(.=) string<delimiter(")char(\\x50)char(\\x4b)char(\\x01)char(\\x02)delimiter(")>operator(;) comment(// Local file header signature) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Version made by) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(10)operator(\))operator(;) comment(// Version needed to extract) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// General purpose bit flag) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Compression method) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file time) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file date) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// crc-32) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Compressed size) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Uncompressed size) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($dir)operator(\))operator(\))operator(;) comment(// File name length) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Extra field length) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// File comment length) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Disk number start) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Internal file attributes) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(16)operator(\))operator(;) comment(// External file attributes) - local_variable($dir_data) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) local_variable($offset)operator(\))operator(;) comment(// Relative offset of local header) - - local_variable($dir_data) operator(.=) local_variable($dir)operator(;) comment(// File name) - local_variable($dir_data) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// Extra field (is empty\)) - local_variable($dir_data) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// File comment (is empty\)) - - comment(/* - // Data descriptor - $dir_data .= pack("V", 0\); // crc-32 - $dir_data .= pack("V", 0\); // Compressed size - $dir_data .= pack("V", 0\); // Uncompressed size - */) - - comment(// Append dir data to the central directory data) - local_variable($cd_data)operator([)operator(]) operator(=) local_variable($dir_data)operator(;) - operator(}) - - comment(// Handle files) - reserved(foreach)operator(()local_variable($this)operator(->)ident(files) reserved(as) local_variable($name) operator(=>) local_variable($file)operator(\)) operator({) - comment(// Get values) - local_variable($content) operator(=) local_variable($file)operator([)integer(0)operator(])operator(;) - - comment(// File part) - - comment(// Reset file data) - local_variable($fd) operator(=) string<delimiter(')delimiter(')>operator(;) - - comment(// Detect possible compressions) - comment(// Use deflate) - reserved(if)operator(()predefined(function_exists)operator(()string<delimiter(')content(gzdeflate)delimiter(')>operator(\))operator(\)) operator({) - local_variable($method) operator(=) integer(8)operator(;) - - comment(// Compress file content) - local_variable($compressed_data) operator(=) predefined(gzdeflate)operator(()local_variable($content)operator(\))operator(;) - - comment(// Use bzip2) - operator(}) reserved(elseif)operator(()predefined(function_exists)operator(()string<delimiter(')content(bzcompress)delimiter(')>operator(\))operator(\)) operator({) - local_variable($method) operator(=) integer(12)operator(;) - - comment(// Compress file content) - local_variable($compressed_data) operator(=) predefined(bzcompress)operator(()local_variable($content)operator(\))operator(;) - - comment(// No compression) - operator(}) reserved(else) operator({) - local_variable($method) operator(=) integer(0)operator(;) - - comment(// Do not compress the content :P) - local_variable($compressed_data) operator(=) local_variable($content)operator(;) - operator(}) - - comment(// Local file header) - local_variable($fd) operator(.=) string<delimiter(")char(\\x50)char(\\x4b)char(\\x03)char(\\x04)delimiter(")>operator(;) comment(// Local file header signature) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(20)operator(\))operator(;) comment(// Version needed to extract) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// General purpose bit flag) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) local_variable($method)operator(\))operator(;) comment(// Compression method) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file time) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file date) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(crc32)operator(()local_variable($content)operator(\))operator(\))operator(;) comment(// crc-32) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($compressed_data)operator(\))operator(\))operator(;) comment(// Compressed size) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($content)operator(\))operator(\))operator(;) comment(// Uncompressed size) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($name)operator(\))operator(\))operator(;) comment(// File name length) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Extra field length) - - local_variable($fd) operator(.=) local_variable($name)operator(;) comment(// File name) - local_variable($fd) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// Extra field (is empty\)) - - comment(// File data) - local_variable($fd) operator(.=) local_variable($compressed_data)operator(;) - - comment(// Data descriptor) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(crc32)operator(()local_variable($content)operator(\))operator(\))operator(;) comment(// crc-32) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($compressed_data)operator(\))operator(\))operator(;) comment(// Compressed size) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($content)operator(\))operator(\))operator(;) comment(// Uncompressed size) - - comment(// Save current offset) - local_variable($offset) operator(=) predefined(strlen)operator(()predefined(implode)operator(()string<delimiter(')delimiter(')>operator(,) local_variable($file_data)operator(\))operator(\))operator(;) - - comment(// Append file data to the file part) - local_variable($file_data)operator([)operator(]) operator(=) local_variable($fd)operator(;) - - comment(// Central directory) - - comment(// Reset file data) - local_variable($fd) operator(=) string<delimiter(')delimiter(')>operator(;) - - comment(// File header) - local_variable($fd) operator(.=) string<delimiter(")char(\\x50)char(\\x4b)char(\\x01)char(\\x02)delimiter(")>operator(;) comment(// Local file header signature) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Version made by) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(20)operator(\))operator(;) comment(// Version needed to extract) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// General purpose bit flag) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) local_variable($method)operator(\))operator(;) comment(// Compression method) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file time) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Last mod file date) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(crc32)operator(()local_variable($content)operator(\))operator(\))operator(;) comment(// crc-32) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($compressed_data)operator(\))operator(\))operator(;) comment(// Compressed size) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($content)operator(\))operator(\))operator(;) comment(// Uncompressed size) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($name)operator(\))operator(\))operator(;) comment(// File name length) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Extra field length) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// File comment length) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Disk number start) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Internal file attributes) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) integer(32)operator(\))operator(;) comment(// External file attributes) - local_variable($fd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) local_variable($offset)operator(\))operator(;) comment(// Relative offset of local header) - - local_variable($fd) operator(.=) local_variable($name)operator(;) comment(// File name) - local_variable($fd) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// Extra field (is empty\)) - local_variable($fd) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// File comment (is empty\)) - - comment(/* - // Data descriptor - $fd .= pack("V", crc32($content\)\); // crc-32 - $fd .= pack("V", strlen($compressed_data\)\); // Compressed size - $fd .= pack("V", strlen($content\)\); // Uncompressed size - */) - - comment(// Append file data to the central directory data) - local_variable($cd_data)operator([)operator(]) operator(=) local_variable($fd)operator(;) - operator(}) - - comment(// Digital signature) - local_variable($digital_signature) operator(=) string<delimiter(')delimiter(')>operator(;) - local_variable($digital_signature) operator(.=) string<delimiter(")char(\\x50)char(\\x4b)char(\\x05)char(\\x05)delimiter(")>operator(;) comment(// Header signature) - local_variable($digital_signature) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Size of data) - local_variable($digital_signature) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// Signature data (is empty\)) - - local_variable($tmp_file_data) operator(=) predefined(implode)operator(()string<delimiter(')delimiter(')>operator(,) local_variable($file_data)operator(\))operator(;) comment(// File data) - local_variable($tmp_cd_data) operator(=) predefined(implode)operator(()string<delimiter(')delimiter(')>operator(,) local_variable($cd_data)operator(\))operator(.) comment(// Central directory) - local_variable($digital_signature)operator(;) comment(// Digital signature) - - comment(// End of central directory) - local_variable($eof_cd) operator(=) string<delimiter(')delimiter(')>operator(;) - local_variable($eof_cd) operator(.=) string<delimiter(")char(\\x50)char(\\x4b)char(\\x05)char(\\x06)delimiter(")>operator(;) comment(// End of central dir signature) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Number of this disk) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// Number of the disk with the start of the central directory) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) predefined(count)operator(()local_variable($cd_data)operator(\))operator(\))operator(;) comment(// Total number of entries in the central directory on this disk) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) predefined(count)operator(()local_variable($cd_data)operator(\))operator(\))operator(;) comment(// Total number of entries in the central directory) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($tmp_cd_data)operator(\))operator(\))operator(;) comment(// Size of the central directory) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(V)delimiter(")>operator(,) predefined(strlen)operator(()local_variable($tmp_file_data)operator(\))operator(\))operator(;) comment(// Offset of start of central directory with respect to the starting disk number) - local_variable($eof_cd) operator(.=) predefined(pack)operator(()string<delimiter(")content(v)delimiter(")>operator(,) integer(0)operator(\))operator(;) comment(// .ZIP file comment length) - local_variable($eof_cd) operator(.=) string<delimiter(')delimiter(')>operator(;) comment(// .ZIP file comment (is empty\)) - - comment(// Content of the zip file) - local_variable($data) operator(=) local_variable($tmp_file_data)operator(.) - comment(// $extra_data_record.) - local_variable($tmp_cd_data)operator(.) - local_variable($eof_cd)operator(;) - - comment(// Return content?) - reserved(if)operator(()operator(!)local_variable($filename)operator(\)) - reserved(return) local_variable($data)operator(;) - - comment(// Write to file) - reserved(return) predefined(file_put_contents)operator(()local_variable($filename)operator(,) local_variable($data)operator(\))operator(;) - operator(}) - - comment(/** - * Load a zip file - * - * This function loads the files and dirs from a zip file from the harddrive. - * - * @access public - * - * @param string $file The path to the zip file - * @param bool $reset Reset the files and dirs before adding the zip file's content? - * - * @return bool Returns true if the file was loaded sucessfully - */) - reserved(function) function(load_file)operator(()local_variable($file)operator(,) local_variable($reset) operator(=) pre_constant(true)operator(\)) operator({) - comment(// Check whether the file exists) - reserved(if)operator(()operator(!)predefined(file_exists)operator(()local_variable($file)operator(\))operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// Load the files content) - local_variable($content) operator(=) exception(@)predefined(file_get_contents)operator(()local_variable($file)operator(\))operator(;) - - comment(// Return false if the file cannot be opened) - reserved(if)operator(()operator(!)local_variable($content)operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// Read the zip) - reserved(return) local_variable($this)operator(->)ident(load_string)operator(()local_variable($content)operator(,) local_variable($reset)operator(\))operator(;) - operator(}) - - comment(/** - * Load a zip string - * - * This function loads the files and dirs from a string - * - * @access public - * - * @param string $string The string the zip is generated from - * @param bool $reset Reset the files and dirs before adding the zip file's content? - * - * @return bool Returns true if the string was loaded sucessfully - */) - reserved(function) function(load_string)operator(()local_variable($string)operator(,) local_variable($reset) operator(=) pre_constant(true)operator(\)) operator({) - comment(// Reset the zip?) - reserved(if)operator(()local_variable($reset)operator(\)) operator({) - local_variable($this)operator(->)ident(dirs) operator(=) predefined(array)operator(()operator(\))operator(;) - local_variable($this)operator(->)ident(files) operator(=) predefined(array)operator(()operator(\))operator(;) - operator(}) - - comment(// Get the starting position of the end of central directory record) - local_variable($start) operator(=) predefined(strpos)operator(()local_variable($string)operator(,) string<delimiter(")char(\\x50)char(\\x4b)char(\\x05)char(\\x06)delimiter(")>operator(\))operator(;) - - comment(// Error) - reserved(if)operator(()local_variable($start) operator(===) pre_constant(false)operator(\)) - predefined(die)operator(()string<delimiter(')content(Could not find the end of central directory record)delimiter(')>operator(\))operator(;) - - comment(// Get the ecdr) - local_variable($eof_cd) operator(=) predefined(substr)operator(()local_variable($string)operator(,) local_variable($start)operator(+)integer(4)operator(,) integer(18)operator(\))operator(;) - - comment(// Unpack the ecdr infos) - local_variable($eof_cd) operator(=) predefined(unpack)operator(()string<delimiter(')content(vdisc1/)delimiter(')>operator(.) - string<delimiter(')content(vdisc2/)delimiter(')>operator(.) - string<delimiter(')content(ventries1/)delimiter(')>operator(.) - string<delimiter(')content(ventries2/)delimiter(')>operator(.) - string<delimiter(')content(Vsize/)delimiter(')>operator(.) - string<delimiter(')content(Voffset/)delimiter(')>operator(.) - string<delimiter(')content(vcomment_lenght)delimiter(')>operator(,) local_variable($eof_cd)operator(\))operator(;) - - comment(// Do not allow multi disc zips) - reserved(if)operator(()local_variable($eof_cd)operator([)string<delimiter(')content(disc1)delimiter(')>operator(]) operator(!=) integer(0)operator(\)) - predefined(die)operator(()string<delimiter(')content(multi disk stuff is not yet implemented :/)delimiter(')>operator(\))operator(;) - - comment(// Save the interesting values) - local_variable($cd_entries) operator(=) local_variable($eof_cd)operator([)string<delimiter(')content(entries1)delimiter(')>operator(])operator(;) - local_variable($cd_size) operator(=) local_variable($eof_cd)operator([)string<delimiter(')content(size)delimiter(')>operator(])operator(;) - local_variable($cd_offset) operator(=) local_variable($eof_cd)operator([)string<delimiter(')content(offset)delimiter(')>operator(])operator(;) - - comment(// Get the central directory record) - local_variable($cdr) operator(=) predefined(substr)operator(()local_variable($string)operator(,) local_variable($cd_offset)operator(,) local_variable($cd_size)operator(\))operator(;) - - comment(// Reset the position and the list of the entries) - local_variable($pos) operator(=) integer(0)operator(;) - local_variable($entries) operator(=) predefined(array)operator(()operator(\))operator(;) - - comment(// Handle cdr) - reserved(while)operator(()local_variable($pos) operator(<) predefined(strlen)operator(()local_variable($cdr)operator(\))operator(\)) operator({) - comment(// Check header signature) - comment(// Digital signature) - reserved(if)operator(()predefined(substr)operator(()local_variable($cdr)operator(,) local_variable($pos)operator(,) integer(4)operator(\)) operator(==) string<delimiter(")char(\\x50)char(\\x4b)char(\\x05)char(\\x05)delimiter(")>operator(\)) operator({) - comment(// Get digital signature size) - local_variable($tmp_info) operator(=) predefined(unpack)operator(()string<delimiter(')content(vsize)delimiter(')>operator(,) predefined(substr)operator(()local_variable($cdr)operator(,) local_variable($pos) operator(+) integer(4)operator(,) integer(2)operator(\))operator(\))operator(;) - - comment(// Read out the digital signature) - local_variable($digital_sig) operator(=) predefined(substr)operator(()local_variable($header)operator(,) local_variable($pos) operator(+) integer(6)operator(,) local_variable($tmp_info)operator([)string<delimiter(')content(size)delimiter(')>operator(])operator(\))operator(;) - - reserved(break)operator(;) - operator(}) - - comment(// Get file header) - local_variable($header) operator(=) predefined(substr)operator(()local_variable($cdr)operator(,) local_variable($pos)operator(,) integer(46)operator(\))operator(;) - - comment(// Unpack the header information) - local_variable($header_info) operator(=) exception(@)predefined(unpack)operator(()string<delimiter(')content(Vheader/)delimiter(')>operator(.) - string<delimiter(')content(vversion_made_by/)delimiter(')>operator(.) - string<delimiter(')content(vversion_needed/)delimiter(')>operator(.) - string<delimiter(')content(vgeneral_purpose/)delimiter(')>operator(.) - string<delimiter(')content(vcompression_method/)delimiter(')>operator(.) - string<delimiter(')content(vlast_mod_time/)delimiter(')>operator(.) - string<delimiter(')content(vlast_mod_date/)delimiter(')>operator(.) - string<delimiter(')content(Vcrc32/)delimiter(')>operator(.) - string<delimiter(')content(Vcompressed_size/)delimiter(')>operator(.) - string<delimiter(')content(Vuncompressed_size/)delimiter(')>operator(.) - string<delimiter(')content(vname_length/)delimiter(')>operator(.) - string<delimiter(')content(vextra_length/)delimiter(')>operator(.) - string<delimiter(')content(vcomment_length/)delimiter(')>operator(.) - string<delimiter(')content(vdisk_number/)delimiter(')>operator(.) - string<delimiter(')content(vinternal_attributes/)delimiter(')>operator(.) - string<delimiter(')content(Vexternal_attributes/)delimiter(')>operator(.) - string<delimiter(')content(Voffset)delimiter(')>operator(,) - local_variable($header)operator(\))operator(;) - - comment(// Valid header?) - reserved(if)operator(()local_variable($header_info)operator([)string<delimiter(')content(header)delimiter(')>operator(]) operator(!=) integer(33639248)operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// New position) - local_variable($pos) operator(+=) integer(46)operator(;) - - comment(// Read out the file name) - local_variable($header_info)operator([)string<delimiter(')content(name)delimiter(')>operator(]) operator(=) predefined(substr)operator(()local_variable($cdr)operator(,) local_variable($pos)operator(,) local_variable($header_info)operator([)string<delimiter(')content(name_length)delimiter(')>operator(])operator(\))operator(;) - - comment(// New position) - local_variable($pos) operator(+=) local_variable($header_info)operator([)string<delimiter(')content(name_length)delimiter(')>operator(])operator(;) - - comment(// Read out the extra stuff) - local_variable($header_info)operator([)string<delimiter(')content(extra)delimiter(')>operator(]) operator(=) predefined(substr)operator(()local_variable($cdr)operator(,) local_variable($pos)operator(,) local_variable($header_info)operator([)string<delimiter(')content(extra_length)delimiter(')>operator(])operator(\))operator(;) - - comment(// New position) - local_variable($pos) operator(+=) local_variable($header_info)operator([)string<delimiter(')content(extra_length)delimiter(')>operator(])operator(;) - - comment(// Read out the comment) - local_variable($header_info)operator([)string<delimiter(')content(comment)delimiter(')>operator(]) operator(=) predefined(substr)operator(()local_variable($cdr)operator(,) local_variable($pos)operator(,) local_variable($header_info)operator([)string<delimiter(')content(comment_length)delimiter(')>operator(])operator(\))operator(;) - - comment(// New position) - local_variable($pos) operator(+=) local_variable($header_info)operator([)string<delimiter(')content(comment_length)delimiter(')>operator(])operator(;) - - comment(// Append this file/dir to the entry list) - local_variable($entries)operator([)operator(]) operator(=) local_variable($header_info)operator(;) - operator(}) - - comment(// Check whether all entries where read sucessfully) - reserved(if)operator(()predefined(count)operator(()local_variable($entries)operator(\)) operator(!=) local_variable($cd_entries)operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// Handle files/dirs) - reserved(foreach)operator(()local_variable($entries) reserved(as) local_variable($entry)operator(\)) operator({) - comment(// Is a dir?) - reserved(if)operator(()local_variable($entry)operator([)string<delimiter(')content(external_attributes)delimiter(')>operator(]) operator(&) integer(16)operator(\)) operator({) - local_variable($this)operator(->)ident(add_dir)operator(()local_variable($entry)operator([)string<delimiter(')content(name)delimiter(')>operator(])operator(\))operator(;) - reserved(continue)operator(;) - operator(}) - - comment(// Get local file header) - local_variable($header) operator(=) predefined(substr)operator(()local_variable($string)operator(,) local_variable($entry)operator([)string<delimiter(')content(offset)delimiter(')>operator(])operator(,) integer(30)operator(\))operator(;) - - comment(// Unpack the header information) - local_variable($header_info) operator(=) exception(@)predefined(unpack)operator(()string<delimiter(')content(Vheader/)delimiter(')>operator(.) - string<delimiter(')content(vversion_needed/)delimiter(')>operator(.) - string<delimiter(')content(vgeneral_purpose/)delimiter(')>operator(.) - string<delimiter(')content(vcompression_method/)delimiter(')>operator(.) - string<delimiter(')content(vlast_mod_time/)delimiter(')>operator(.) - string<delimiter(')content(vlast_mod_date/)delimiter(')>operator(.) - string<delimiter(')content(Vcrc32/)delimiter(')>operator(.) - string<delimiter(')content(Vcompressed_size/)delimiter(')>operator(.) - string<delimiter(')content(Vuncompressed_size/)delimiter(')>operator(.) - string<delimiter(')content(vname_length/)delimiter(')>operator(.) - string<delimiter(')content(vextra_length)delimiter(')>operator(,) - local_variable($header)operator(\))operator(;) - - comment(// Valid header?) - reserved(if)operator(()local_variable($header_info)operator([)string<delimiter(')content(header)delimiter(')>operator(]) operator(!=) integer(67324752)operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// Get content start position) - local_variable($start) operator(=) local_variable($entry)operator([)string<delimiter(')content(offset)delimiter(')>operator(]) operator(+) integer(30) operator(+) local_variable($header_info)operator([)string<delimiter(')content(name_length)delimiter(')>operator(]) operator(+) local_variable($header_info)operator([)string<delimiter(')content(extra_length)delimiter(')>operator(])operator(;) - - comment(// Get the compressed data) - local_variable($data) operator(=) predefined(substr)operator(()local_variable($string)operator(,) local_variable($start)operator(,) local_variable($header_info)operator([)string<delimiter(')content(compressed_size)delimiter(')>operator(])operator(\))operator(;) - - comment(// Detect compression type) - reserved(switch)operator(()local_variable($header_info)operator([)string<delimiter(')content(compression_method)delimiter(')>operator(])operator(\)) operator({) - comment(// No compression) - reserved(case) integer(0)operator(:) - comment(// Ne decompression needed) - local_variable($content) operator(=) local_variable($data)operator(;) - reserved(break)operator(;) - - comment(// Gzip) - reserved(case) integer(8)operator(:) - reserved(if)operator(()operator(!)predefined(function_exists)operator(()string<delimiter(')content(gzinflate)delimiter(')>operator(\))operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// Uncompress data) - local_variable($content) operator(=) predefined(gzinflate)operator(()local_variable($data)operator(\))operator(;) - reserved(break)operator(;) - - comment(// Bzip2) - reserved(case) integer(12)operator(:) - reserved(if)operator(()operator(!)predefined(function_exists)operator(()string<delimiter(')content(bzdecompress)delimiter(')>operator(\))operator(\)) - reserved(return) pre_constant(false)operator(;) - - comment(// Decompress data) - local_variable($content) operator(=) predefined(bzdecompress)operator(()local_variable($data)operator(\))operator(;) - reserved(break)operator(;) - - comment(// Compression not supported -> error) - reserved(default)operator(:) - reserved(return) pre_constant(false)operator(;) - operator(}) - - comment(// Try to add file) - reserved(if)operator(()operator(!)local_variable($this)operator(->)ident(add_file)operator(()local_variable($entry)operator([)string<delimiter(')content(name)delimiter(')>operator(])operator(,) local_variable($content)operator(\))operator(\)) - reserved(return) pre_constant(false)operator(;) - operator(}) - - reserved(return) pre_constant(true)operator(;) - operator(}) -operator(}) - -reserved(function) operator(&)function(byref)operator(()operator(\)) operator({) - local_variable($x) operator(=) predefined(array)operator(()operator(\))operator(;) - reserved(return) local_variable($x)operator(;) -operator(}) -inline_delimiter(?>) diff --git a/test/scanners/php/test.in.php b/test/scanners/php/test.in.php deleted file mode 100644 index 90de747..0000000 --- a/test/scanners/php/test.in.php +++ /dev/null @@ -1,498 +0,0 @@ -<?php -/** - * Zip class file - * - * @package fnord.bb - * @subpackage archive - */ - -// Unlock? -if(!defined('UNLOCK') || !UNLOCK) - die(); - -// Load the parent archive class -require_once(ROOT_PATH.'/classes/archive.class.php'); - -/** - * Zip class - * - * @author Manni <manni@fnord.name> - * @copyright Copyright (c) 2006, Manni - * @version 1.0 - * @link http://www.pkware.com/business_and_developers/developer/popups/appnote.txt - * @link http://mannithedark.is-a-geek.net/ - * @since 1.0 - * @package fnord.bb - * @subpackage archive - */ -class Zip extends Archive { - /** - * Outputs the zip file - * - * This function creates the zip file with the dirs and files given. - * If the optional parameter $file is given, the zip file is will be - * saved at that location. Otherwise the function returns the zip file's content. - * - * @access public - * - * @link http://www.pkware.com/business_and_developers/developer/popups/appnote.txt - * @param string $filename The path where the zip file will be saved - * - * @return bool|string Returns either true if the fil is sucessfully created or the content of the zip file - */ - function out($filename = false) { - // Empty output - $file_data = array(); // Data of the file part - $cd_data = array(); // Data of the central directory - - // Sort dirs and files by path length - uksort($this->dirs, 'sort_by_length'); - uksort($this->files, 'sort_by_length'); - - // Handle dirs - foreach($this->dirs as $dir) { - $dir .= '/'; - // File part - - // Reset dir data - $dir_data = ''; - - // Local file header - $dir_data .= "\x50\x4b\x03\x04"; // Local file header signature - $dir_data .= pack("v", 10); // Version needed to extract - $dir_data .= pack("v", 0); // General purpose bit flag - $dir_data .= pack("v", 0); // Compression method - $dir_data .= pack("v", 0); // Last mod file time - $dir_data .= pack("v", 0); // Last mod file date - $dir_data .= pack("V", 0); // crc-32 - $dir_data .= pack("V", 0); // Compressed size - $dir_data .= pack("V", 0); // Uncompressed size - $dir_data .= pack("v", strlen($dir)); // File name length - $dir_data .= pack("v", 0); // Extra field length - - $dir_data .= $dir; // File name - $dir_data .= ''; // Extra field (is empty) - - // File data - $dir_data .= ''; // Dirs have no file data - - // Data descriptor - $dir_data .= pack("V", 0); // crc-32 - $dir_data .= pack("V", 0); // Compressed size - $dir_data .= pack("V", 0); // Uncompressed size - - // Save current offset - $offset = strlen(implode('', $file_data)); - - // Append dir data to the file part - $file_data[] = $dir_data; - - // Central directory - - // Reset dir data - $dir_data = ''; - - // File header - $dir_data .= "\x50\x4b\x01\x02"; // Local file header signature - $dir_data .= pack("v", 0); // Version made by - $dir_data .= pack("v", 10); // Version needed to extract - $dir_data .= pack("v", 0); // General purpose bit flag - $dir_data .= pack("v", 0); // Compression method - $dir_data .= pack("v", 0); // Last mod file time - $dir_data .= pack("v", 0); // Last mod file date - $dir_data .= pack("V", 0); // crc-32 - $dir_data .= pack("V", 0); // Compressed size - $dir_data .= pack("V", 0); // Uncompressed size - $dir_data .= pack("v", strlen($dir)); // File name length - $dir_data .= pack("v", 0); // Extra field length - $dir_data .= pack("v", 0); // File comment length - $dir_data .= pack("v", 0); // Disk number start - $dir_data .= pack("v", 0); // Internal file attributes - $dir_data .= pack("V", 16); // External file attributes - $dir_data .= pack("V", $offset); // Relative offset of local header - - $dir_data .= $dir; // File name - $dir_data .= ''; // Extra field (is empty) - $dir_data .= ''; // File comment (is empty) - - /* - // Data descriptor - $dir_data .= pack("V", 0); // crc-32 - $dir_data .= pack("V", 0); // Compressed size - $dir_data .= pack("V", 0); // Uncompressed size - */ - - // Append dir data to the central directory data - $cd_data[] = $dir_data; - } - - // Handle files - foreach($this->files as $name => $file) { - // Get values - $content = $file[0]; - - // File part - - // Reset file data - $fd = ''; - - // Detect possible compressions - // Use deflate - if(function_exists('gzdeflate')) { - $method = 8; - - // Compress file content - $compressed_data = gzdeflate($content); - - // Use bzip2 - } elseif(function_exists('bzcompress')) { - $method = 12; - - // Compress file content - $compressed_data = bzcompress($content); - - // No compression - } else { - $method = 0; - - // Do not compress the content :P - $compressed_data = $content; - } - - // Local file header - $fd .= "\x50\x4b\x03\x04"; // Local file header signature - $fd .= pack("v", 20); // Version needed to extract - $fd .= pack("v", 0); // General purpose bit flag - $fd .= pack("v", $method); // Compression method - $fd .= pack("v", 0); // Last mod file time - $fd .= pack("v", 0); // Last mod file date - $fd .= pack("V", crc32($content)); // crc-32 - $fd .= pack("V", strlen($compressed_data)); // Compressed size - $fd .= pack("V", strlen($content)); // Uncompressed size - $fd .= pack("v", strlen($name)); // File name length - $fd .= pack("v", 0); // Extra field length - - $fd .= $name; // File name - $fd .= ''; // Extra field (is empty) - - // File data - $fd .= $compressed_data; - - // Data descriptor - $fd .= pack("V", crc32($content)); // crc-32 - $fd .= pack("V", strlen($compressed_data)); // Compressed size - $fd .= pack("V", strlen($content)); // Uncompressed size - - // Save current offset - $offset = strlen(implode('', $file_data)); - - // Append file data to the file part - $file_data[] = $fd; - - // Central directory - - // Reset file data - $fd = ''; - - // File header - $fd .= "\x50\x4b\x01\x02"; // Local file header signature - $fd .= pack("v", 0); // Version made by - $fd .= pack("v", 20); // Version needed to extract - $fd .= pack("v", 0); // General purpose bit flag - $fd .= pack("v", $method); // Compression method - $fd .= pack("v", 0); // Last mod file time - $fd .= pack("v", 0); // Last mod file date - $fd .= pack("V", crc32($content)); // crc-32 - $fd .= pack("V", strlen($compressed_data)); // Compressed size - $fd .= pack("V", strlen($content)); // Uncompressed size - $fd .= pack("v", strlen($name)); // File name length - $fd .= pack("v", 0); // Extra field length - $fd .= pack("v", 0); // File comment length - $fd .= pack("v", 0); // Disk number start - $fd .= pack("v", 0); // Internal file attributes - $fd .= pack("V", 32); // External file attributes - $fd .= pack("V", $offset); // Relative offset of local header - - $fd .= $name; // File name - $fd .= ''; // Extra field (is empty) - $fd .= ''; // File comment (is empty) - - /* - // Data descriptor - $fd .= pack("V", crc32($content)); // crc-32 - $fd .= pack("V", strlen($compressed_data)); // Compressed size - $fd .= pack("V", strlen($content)); // Uncompressed size - */ - - // Append file data to the central directory data - $cd_data[] = $fd; - } - - // Digital signature - $digital_signature = ''; - $digital_signature .= "\x50\x4b\x05\x05"; // Header signature - $digital_signature .= pack("v", 0); // Size of data - $digital_signature .= ''; // Signature data (is empty) - - $tmp_file_data = implode('', $file_data); // File data - $tmp_cd_data = implode('', $cd_data). // Central directory - $digital_signature; // Digital signature - - // End of central directory - $eof_cd = ''; - $eof_cd .= "\x50\x4b\x05\x06"; // End of central dir signature - $eof_cd .= pack("v", 0); // Number of this disk - $eof_cd .= pack("v", 0); // Number of the disk with the start of the central directory - $eof_cd .= pack("v", count($cd_data)); // Total number of entries in the central directory on this disk - $eof_cd .= pack("v", count($cd_data)); // Total number of entries in the central directory - $eof_cd .= pack("V", strlen($tmp_cd_data)); // Size of the central directory - $eof_cd .= pack("V", strlen($tmp_file_data)); // Offset of start of central directory with respect to the starting disk number - $eof_cd .= pack("v", 0); // .ZIP file comment length - $eof_cd .= ''; // .ZIP file comment (is empty) - - // Content of the zip file - $data = $tmp_file_data. - // $extra_data_record. - $tmp_cd_data. - $eof_cd; - - // Return content? - if(!$filename) - return $data; - - // Write to file - return file_put_contents($filename, $data); - } - - /** - * Load a zip file - * - * This function loads the files and dirs from a zip file from the harddrive. - * - * @access public - * - * @param string $file The path to the zip file - * @param bool $reset Reset the files and dirs before adding the zip file's content? - * - * @return bool Returns true if the file was loaded sucessfully - */ - function load_file($file, $reset = true) { - // Check whether the file exists - if(!file_exists($file)) - return false; - - // Load the files content - $content = @file_get_contents($file); - - // Return false if the file cannot be opened - if(!$content) - return false; - - // Read the zip - return $this->load_string($content, $reset); - } - - /** - * Load a zip string - * - * This function loads the files and dirs from a string - * - * @access public - * - * @param string $string The string the zip is generated from - * @param bool $reset Reset the files and dirs before adding the zip file's content? - * - * @return bool Returns true if the string was loaded sucessfully - */ - function load_string($string, $reset = true) { - // Reset the zip? - if($reset) { - $this->dirs = array(); - $this->files = array(); - } - - // Get the starting position of the end of central directory record - $start = strpos($string, "\x50\x4b\x05\x06"); - - // Error - if($start === false) - die('Could not find the end of central directory record'); - - // Get the ecdr - $eof_cd = substr($string, $start+4, 18); - - // Unpack the ecdr infos - $eof_cd = unpack('vdisc1/'. - 'vdisc2/'. - 'ventries1/'. - 'ventries2/'. - 'Vsize/'. - 'Voffset/'. - 'vcomment_lenght', $eof_cd); - - // Do not allow multi disc zips - if($eof_cd['disc1'] != 0) - die('multi disk stuff is not yet implemented :/'); - - // Save the interesting values - $cd_entries = $eof_cd['entries1']; - $cd_size = $eof_cd['size']; - $cd_offset = $eof_cd['offset']; - - // Get the central directory record - $cdr = substr($string, $cd_offset, $cd_size); - - // Reset the position and the list of the entries - $pos = 0; - $entries = array(); - - // Handle cdr - while($pos < strlen($cdr)) { - // Check header signature - // Digital signature - if(substr($cdr, $pos, 4) == "\x50\x4b\x05\x05") { - // Get digital signature size - $tmp_info = unpack('vsize', substr($cdr, $pos + 4, 2)); - - // Read out the digital signature - $digital_sig = substr($header, $pos + 6, $tmp_info['size']); - - break; - } - - // Get file header - $header = substr($cdr, $pos, 46); - - // Unpack the header information - $header_info = @unpack('Vheader/'. - 'vversion_made_by/'. - 'vversion_needed/'. - 'vgeneral_purpose/'. - 'vcompression_method/'. - 'vlast_mod_time/'. - 'vlast_mod_date/'. - 'Vcrc32/'. - 'Vcompressed_size/'. - 'Vuncompressed_size/'. - 'vname_length/'. - 'vextra_length/'. - 'vcomment_length/'. - 'vdisk_number/'. - 'vinternal_attributes/'. - 'Vexternal_attributes/'. - 'Voffset', - $header); - - // Valid header? - if($header_info['header'] != 33639248) - return false; - - // New position - $pos += 46; - - // Read out the file name - $header_info['name'] = substr($cdr, $pos, $header_info['name_length']); - - // New position - $pos += $header_info['name_length']; - - // Read out the extra stuff - $header_info['extra'] = substr($cdr, $pos, $header_info['extra_length']); - - // New position - $pos += $header_info['extra_length']; - - // Read out the comment - $header_info['comment'] = substr($cdr, $pos, $header_info['comment_length']); - - // New position - $pos += $header_info['comment_length']; - - // Append this file/dir to the entry list - $entries[] = $header_info; - } - - // Check whether all entries where read sucessfully - if(count($entries) != $cd_entries) - return false; - - // Handle files/dirs - foreach($entries as $entry) { - // Is a dir? - if($entry['external_attributes'] & 16) { - $this->add_dir($entry['name']); - continue; - } - - // Get local file header - $header = substr($string, $entry['offset'], 30); - - // Unpack the header information - $header_info = @unpack('Vheader/'. - 'vversion_needed/'. - 'vgeneral_purpose/'. - 'vcompression_method/'. - 'vlast_mod_time/'. - 'vlast_mod_date/'. - 'Vcrc32/'. - 'Vcompressed_size/'. - 'Vuncompressed_size/'. - 'vname_length/'. - 'vextra_length', - $header); - - // Valid header? - if($header_info['header'] != 67324752) - return false; - - // Get content start position - $start = $entry['offset'] + 30 + $header_info['name_length'] + $header_info['extra_length']; - - // Get the compressed data - $data = substr($string, $start, $header_info['compressed_size']); - - // Detect compression type - switch($header_info['compression_method']) { - // No compression - case 0: - // Ne decompression needed - $content = $data; - break; - - // Gzip - case 8: - if(!function_exists('gzinflate')) - return false; - - // Uncompress data - $content = gzinflate($data); - break; - - // Bzip2 - case 12: - if(!function_exists('bzdecompress')) - return false; - - // Decompress data - $content = bzdecompress($data); - break; - - // Compression not supported -> error - default: - return false; - } - - // Try to add file - if(!$this->add_file($entry['name'], $content)) - return false; - } - - return true; - } -} - -function &byref() { - $x = array(); - return $x; -} -?> |