diff options
Diffstat (limited to 'test/scanners/php/pleac.expected.raydebug')
-rw-r--r-- | test/scanners/php/pleac.expected.raydebug | 5516 |
1 files changed, 0 insertions, 5516 deletions
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(}) |