Here's a cheat sheet to add to the collection of PHP programmers, happy coding!



Language Category: Object Oriented, Dynamically typed
Various
{ ... }block (grouping statements, especially when statements are not expressions)
nothing neededbreaking lines (useful when end-of-line and/or indentation has a special meaning)
/* ... */commenting (non nestable)
#commenting (until end of line)
//commenting (until end of line)
< > <= >=comparison
strcmpcomparison (returns 3 values (i.e. inferior, equal or superior))
/** ... */(1)documentation comment (non nestable)
Err:510equality / inequality (shallow)
( ... )grouping expressions
__LINE__ __FILE__information about the current line and file
evalruntime evaluation
case-sensitive: variables
case-insensitive: keywords, functions, constants...
tokens (case-sensitivity (keywords, variable identifiers...))
[_a-zA-Z][_a-zA-Z0-9]*tokens (variable identifier regexp)
=variable assignment or declaration (assignment)
Functions
create_function(',','...')anonymous function
f(a,b,...)function call
f()function call (with no parameter)
Return(3)function return value (breaks the control flow)
Control Flow
continue / breakbreaking control flow (continue / break)
Return(3)breaking control flow (returning a value)
if (c) ...if_then
if (c): ... endifif_then
if (c) b1 elseif (c2) b2 else b3if_then_else
if (c): b1 elseif (c2): b2 else: b3 endifif_then_else
c ? b1 : b2if_then_else
forloop (for "a la C" (while + initialisation))
for ($i = 1; $i <= 10; $i--) ...loop (for each value in a numeric range, 1 decrement)
for ($i = 1; $i <= 10; $i++) ...loop (for each value in a numeric range, 1 increment (see also the entries about ranges))
for ($i = 1; $i <= 10; $i += 2) ...loop (for each value in a numeric range, free increment)
while (c) ...loop (while condition do something)
switch (val) {
case v1: ...; break;
case v2: case v3: ...; break;
default: ...;
}
multiple selection (switch)
;sequence
Types
(t) ecast (upcast)
Object Oriented & Reflexivity
classclass declaration
thiscurrent instance
get_classget the type/class corresponding to an object/instance/value
object->method(para)method invocation
get_class_methodsmethods available
o2 = o(4)object cloning
$o2 = $oobject cloning
newobject creation
new class_name(...)object creation
is_atesting class membership
Strings
s[n]accessing n-th character
chrascii to character
ordcharacter to ascii
(string) econvert something to a string (see also string interpolation)
str_repeatduplicate n times
substrextract a substring
strposlocate a substring
strrposlocate a substring (starting at the end)
all strings allow multi-line stringsmulti-line
serializeserialize (marshalling)
printsimple print (on strings)
Echo(5)simple print (on strings)
printfsimple print (printf-like)
sprintfsprintf-like
0string concatenation
Err:510string equality & inequality
strlenstring size
"\n"strings (end-of-line (without writing the real CR or LF character))
"... $v ..."strings (with interpolation of variables)
<<strings (with interpolation of variables)
'...'strings (with no interpolation of variables)
stringtype name
unserializeunserialize (un-marshalling)
strtoupper / strtolowerupper / lower case character
strtoupper / strtoloweruppercase / lowercase / capitalized string
Booleans
falsefalse value
NULLfalse value
0(6)false value
0false value
""false value
"0"false value
array()false value
!logical not
|| / &&logical or / and (short circuit)
or / andlogical or / and (short circuit)
truetrue value
booltype name
booleantype name
Bags and Lists
array_unshiftadding an element at the beginning (list cons) (side-effect)
array_pushadding an element at the end (side-effect)
foreachfor each element do something
array_shiftget the first element and remove it
array_popget the last element and remove it
in_arrayis an element in the list
foreach($l as $i => $v)iterate with index
join(s, l)join a list of strings in a string using a glue string
implode(s, l)join a list of strings in a string using a glue string
0list concatenation
array_mergelist concatenation
array(a, b, c)list constructor
countlist size
a[i]list/array indexing
array_uniqueremove duplicates
array_reversereverse
Sort(7)sort
array_maptransform a list (or bag) in another one
arraytype name
Various Data Types
h[k]dictionary (access: read/write)
array( a => b, c => d )dictionary (constructor)
isset(h[k]), array_key_exists(k, h)dictionary (has the key ?)
array_keysdictionary (list of keys)
array_valuesdictionary (list of values)
array_merge(8)dictionary (merge)
unset(h[k])dictionary (remove by key)
arraydictionary (type name)
?:optional value (null coalescing)
rangerange (inclusive .. inclusive)
Mathematics
+ / - / * / /addition / subtraction / multiplication / division
& / | / ^bitwise operators (and / or / xor)
~bitwise operators (bitwise inversion)
<< / >>bitwise operators (left shift / right shift / unsigned right shift)
powexponentiation (power)
log10logarithm (base 10)
loglogarithm (base e)
%modulo (modulo of -3 / 2 is -1)
-0negation
1000.0, 1E3numbers syntax (floating point)
1000numbers syntax (integers)
mathematicaloperator priorities and associativities (addition vs multiplication)
randrandom (random number)
mt_randrandom (random number)
srandrandom (seed the pseudo random generator)
sqrt / exp / abssquare root / e-exponential / absolute value
sin / cos / tantrigonometry (basic)
/ round / floor / ceiltruncate / round / floor / ceil
floattype name (floating point)
inttype name (integers)
integertype name (integers)
Remarks
(1) for C, it is not a standard convention, but it is the more widespread
(2) === and !== differ from == and != when the objects' type differ
(3) in Lua, "return xxx" can only appear before a block end. in Matlab, only in inline('...')
(4) object cloning is the default, uses the copy constructor in C++
(5) in BourneShell, adding an end-of-line
(6) beware of 0.0 which is true in Pike!
(7) in Scheme, not standard, but nearly standard
(8) right-bias

Post a Comment

 
Top