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 needed | breaking 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 |
strcmp | comparison (returns 3 values (i.e. inferior, equal or superior)) |
/** ... */(1) | documentation comment (non nestable) |
Err:510 | equality / inequality (shallow) |
( ... ) | grouping expressions |
__LINE__ __FILE__ | information about the current line and file |
eval | runtime 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 / break | breaking control flow (continue / break) |
Return(3) | breaking control flow (returning a value) |
if (c) ... | if_then |
if (c): ... endif | if_then |
if (c) b1 elseif (c2) b2 else b3 | if_then_else |
if (c): b1 elseif (c2): b2 else: b3 endif | if_then_else |
c ? b1 : b2 | if_then_else |
for | loop (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) e | cast (upcast) |
Object Oriented & Reflexivity | |
class | class declaration |
this | current instance |
get_class | get the type/class corresponding to an object/instance/value |
object->method(para) | method invocation |
get_class_methods | methods available |
o2 = o(4) | object cloning |
$o2 = $o | object cloning |
new | object creation |
new class_name(...) | object creation |
is_a | testing class membership |
Strings | |
s[n] | accessing n-th character |
chr | ascii to character |
ord | character to ascii |
(string) e | convert something to a string (see also string interpolation) |
str_repeat | duplicate n times |
substr | extract a substring |
strpos | locate a substring |
strrpos | locate a substring (starting at the end) |
all strings allow multi-line strings | multi-line |
serialize | serialize (marshalling) |
simple print (on strings) | |
Echo(5) | simple print (on strings) |
printf | simple print (printf-like) |
sprintf | sprintf-like |
0 | string concatenation |
Err:510 | string equality & inequality |
strlen | string 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) |
string | type name |
unserialize | unserialize (un-marshalling) |
strtoupper / strtolower | upper / lower case character |
strtoupper / strtolower | uppercase / lowercase / capitalized string |
Booleans | |
false | false value |
NULL | false value |
0(6) | false value |
0 | false value |
"" | false value |
"0" | false value |
array() | false value |
! | logical not |
|| / && | logical or / and (short circuit) |
or / and | logical or / and (short circuit) |
true | true value |
bool | type name |
boolean | type name |
Bags and Lists | |
array_unshift | adding an element at the beginning (list cons) (side-effect) |
array_push | adding an element at the end (side-effect) |
foreach | for each element do something |
array_shift | get the first element and remove it |
array_pop | get the last element and remove it |
in_array | is 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 |
0 | list concatenation |
array_merge | list concatenation |
array(a, b, c) | list constructor |
count | list size |
a[i] | list/array indexing |
array_unique | remove duplicates |
array_reverse | reverse |
Sort(7) | sort |
array_map | transform a list (or bag) in another one |
array | type 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_keys | dictionary (list of keys) |
array_values | dictionary (list of values) |
array_merge(8) | dictionary (merge) |
unset(h[k]) | dictionary (remove by key) |
array | dictionary (type name) |
?: | optional value (null coalescing) |
range | range (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) |
pow | exponentiation (power) |
log10 | logarithm (base 10) |
log | logarithm (base e) |
% | modulo (modulo of -3 / 2 is -1) |
-0 | negation |
1000.0, 1E3 | numbers syntax (floating point) |
1000 | numbers syntax (integers) |
mathematical | operator priorities and associativities (addition vs multiplication) |
rand | random (random number) |
mt_rand | random (random number) |
srand | random (seed the pseudo random generator) |
sqrt / exp / abs | square root / e-exponential / absolute value |
sin / cos / tan | trigonometry (basic) |
/ round / floor / ceil | truncate / round / floor / ceil |
float | type name (floating point) |
int | type name (integers) |
integer | type 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