
DigitalDan's Scientific Website
PHP Crib Sheet
NoteThe list is not complete, please let me know about errors and omissions.
$_COOKIE | returns array of parameters passed to current web-page via cookies See note on Cookies | |
$_GET | returns array of parameters from after the ? in current web-page address | |
$_POST | returns array of parameters passed to current web-page using the <form> option of post | |
$_SESSION | returns array of parameters held in the SESSION array Consider data security if information is sensitive | |
/* ... */ | comment | |
// | comment | |
abs | Absolute value | |
acos | arc-cosine | |
acosh | arc-hyperbolic-cosine | |
array_diff( .., ...) | return values present in either array but not found in the other array | |
array_intersect( .., ...) | return values present in both arrays | |
array_key_exists( ..., .. ) | returns true/false dependingon whether array ... contains the key .. | |
array_merge( .. , ... ) | merge two (or more) arrays | |
array_reverse( .. ) | Reverse the order of elements in array .. | |
array_unique( .. ) | remove duplicate entries from array .. | |
array | defines a variable array | |
arsort( .. ) | Sort (descending) the array .. | |
asin | arc-sine | |
asinh | arc-hyperbolic-sine | |
asort( .. ) | Sort the array .. | |
atan | arc-tangent | |
atan2 | arc-tangent-2 | |
atanh | arc-hyperbolic-tangent | |
base_convert | convert between bases | |
bindec | binary to decimal | |
break | Stop a loop (for, while, do, switch etc.) | |
ceil | round up to nearest integer | |
checkdate | validate a date | |
class | defines a class | |
closedir | close directory | |
continue | Start next pass of loop (for, while, do, etc.) | |
cos | cosine | |
cosh | Hyperbolic cosine | |
date_parse | Array containing date information | |
date_sun_info | Array containing sunrise/sunset information | |
date_sunrise | sunrise | |
date_sunset | sunset | |
date | get information about a date | |
decbin | Decimal to binary | |
dechex | Decimal to hex | |
decoct | Decimal to octal | |
deg2rad | degrees to radians | |
do{ ... } while( .. ); | Repeat ... while .. is true always run ... at least once | |
easter_date | date of easter | |
echo | Send to browser as HTML | |
else | Used as part of an If to indicate action when Not true | |
elseif | similar to "else { if" | |
ereg_replace | Replace characters in a string using regular expression rules | |
ereg | Applies regular expression to string | |
eregi_replace | Replace characters in a string using regular expression rules (not case sensitive) | |
eregi | Applies regular expression to string but not case sensitive | |
exp | exponent of e | |
expm1 | exp(number) - 1 | |
fclose | close a file | |
fgetcsv | read data from a .csv file | |
fgets | read data from file | |
fgets | read one line of data from a file | |
file_exists( ... ) | returns true or false depending on whether the file can be found | |
file_get_contents | copy the entire contents of a file into a variable | |
file_put_contents | create a file using the data in a variable | |
fileatime | returns timestamp when file last accessed | |
filectime | returns timestamp when file created | |
filegroup | returns the file group associated with a file | |
fileinode | returns the files inode | |
filemtime | returns timestamp when file last modified | |
fileowner | returns file owner | |
fileperms | returns file permissions | |
filepos | get current position for reading/writing within a file | |
filesize | returns size of file (in bytes) | |
filetype | returns type of file | |
floor | round down to nearest integer | |
fmod | remainder (modulus) | |
fopen | open a file | |
for(.. = ..; ..; ..;){ .. } | for ... next loop | |
foreach(.. as ... => ....){ ..... } | Loop though every element n an array | |
fputcsv | write data to file in .csv format | |
fread | read data from file | |
fseek | set current position for reading/writing within a file | |
function ..([..[,..]]) { ... } | defines a function with optional parameters | |
fwrite | write to file | |
getdate( ... ) | break a date down in to month, year, weekday etc. See below for parameters | |
getowd | get owner of current directory | |
gettimeofday | time | |
hexdec | hex to decimal | |
hypot | hypotenuse of a right-angle triangle | |
if | if .. then ... | |
include .. | insert all code contained in the file .. | |
is_executable | returns the file attribute indicating iof it is executabe | |
is_file | returns true if a real file and false if a link | |
is_finite | is number finite | |
is_infinite | is number infinite | |
is_link | returns true if file is a link | |
is_nan | is not a number | |
is_readable | returns true if file can be read and false if unreadable | |
is_writeable | returns true if file can be written to | |
isset( .. ) | returns true/false depending on whether the variable exists particularly useful with $_GET, $_POST etc. | |
krsort( .. ) | Sort (descending) the array .. into key sequence | |
ksort( .. ) | Sort the array .. into key sequence | |
log | Loge | |
log10 | Log10 | |
log1p | log(1 + number) | |
max(..,..,..) | return largest number in list | |
max | maximum value | |
min(..,..,..) | return smallest number in list | |
min | miniumum value | |
mktime(..,..,..,..,..,..) | sets a date variable parameters in order are hour,month,second,month,day,year | |
mt_rand(..,...) | return random integer between .. and ... | |
mt_srand(..,...) | return random integer between .. and ... | |
octdec | Octal to decimal | |
pi | 3.141... | |
pow | Exponential expression | |
print_r( .. ) | output every element in array .. | |
printf(..) | format the sting .. and print | |
rad2deg | radians to degrees | |
rand(..,...) | return random integer between .. and ... | |
rand | random number (integer) | |
readdir | read directory entry | |
return | Return a value from a function | |
rewinddir | reset directory pointer | |
round(..[,...]) | round number down to ... decimal places nearest integer if ... ommited | |
round | round to nearest integer | |
serialize( .. ) | Convert array .. into a specially formatted string | |
Set-Cookie | sets up a cookie See note on cookies | |
sin | Sine | |
sinh | Hyperbolic sine | |
sprintf(..) | format the sting .. uses same settings as printf shown below | |
sqrt | Square root | |
srand | seed random number generator | |
strlen( .. ) | Number of characters in string .. | |
strpos( .., ... ) | position of first ... in string .. strings start at character 0 | |
strstr( .., ... ) | returns all characters in string .. after(and including) the first ... | |
strtotime(..) | Convert dates as text into a date variable expects US date formats, unpredictable with UK formats | |
substr(..,...[,....]) | .... letters from string .. staring at position ... strings start at character 0 if .... omitted, all characters to end of string returned | |
switch (..){ case: ...; break; default: .... } | Select action to take based on .. | |
tan | Tangent | |
tanh | Hyperbolic tangent | |
unserialize( .. ) | Convert a serialised string into an array | |
while( .. ){ ... } | Repeat ... while .. is true |
Maths Operators | |
% | modulus (remainder) |
* | multiply |
*= | ..%=... equivilent to .. = .. % ... |
*= | ..-=... equivilent to .. = .. * ... |
/ | divide |
/= | ..+=... equivilent to .. = .. / ... |
++ | ..++ equivilent to .. = .. + 1 ++.. adds 1 before showing result |
+ | add |
+= | ..+=... equivilent to .. = .. + ... |
-- | ..-- equivilent to .. = .. - 1 --.. subtracts 1 before showing result |
- | subtract |
-= | ..-=... equivilent to .. = .. - ... |
String Operators | |
!= | is not the same as |
> | is alphabetically after |
>= | is alphabetically the same or after |
< | is alphabetically before |
<= | is alphabetically the same or before |
. | concatenate |
== | is the same as |
File access | |
a+ | read and write file (add to end of file if file already exists) |
a | write to file (add to end of file if file already exists) |
r+ | read and write (starting at beginning of file) |
r | read (starting at beginning of file) |
w+ | read and write file (overwrite entire file if it already exists) |
w | write file (overwrite entire file if it already exists) |
Conditional operators etc. | |
! | not |
!= | is not equal to |
&& | and |
> | is greater than |
>= | is greater than or equal to |
< | is less than |
<= | is less than or equal to |
|| | or |
== | is equal to |
=== | is equal to and the same type of variable |
and | and |
or | or |
xor or | exclusive or |
Date(..[,date_variable]) Current date assumed if date_variable omitted Some systems fail on dates before 1970 or after 2037 | |
a | morning/afternoon am-pm |
A | morning/afternoon AM-PM |
B | internet time |
c | full date in ISO format |
d | day of month 01-31 |
D | day of week Sun-Sat |
e | time zone identifier |
F | name of month January-December |
G | hour 0-23 |
g | hour 1-12 |
H | hour 00-23 |
h | hour 01-12 |
I | Is this daylight saving time |
i | minute 00-59 |
j | day of month 1-31 |
l | day of week Sunday-Saturday |
L | is it a leap year? |
M | month Jan-Dec |
m | month number 01-12 |
n | month number 1-12 |
N | numeric day of week |
o | 4 digit year adjusted if weeknumber in different year |
O | offset from Greenwich Meen Time {hours} |
P | offset from Greenwich Mean Time {returns hh:mm} |
r | full date in RFC format |
S | day on month suffix st-nd-rd-th |
s | seconds 00-59 |
t | number of days in month |
T | timezone abbreviation |
u | microseconds {needs DateTime::format()} |
U | seconds since 1-Jan-1980 (Unix epoch date) |
w | number for day of week 0-7 (0-Sunday, 6=Saturday) |
W | week number 0-53 |
y | last 2 digits of year 70-37 |
Y | year 1970-2037 |
z | day of year 0-365 |
Z | timezone offset in seconds |
getdate( .. ) | |
hours | hours 0-23 |
mday | day of month 0-31 |
minutes | minutes 0-59 |
mon | month number 1-12 |
month | January-December |
seconds | seconds0-59 |
wday | number for day of week 0-6 (0=Sun, 6=Sat) |
weekday | Sunday-Saturday |
yday | day of year 0-365 |
year | year 1970-2037 |
printf( .. ) | |
%.. | next format must pad to .. characters |
b | binary |
c | ascii character |
d | decimal |
e | exponential |
f | floating point |
o | octal |
s | string |
u | unsigned |
x | hexadecimal (lowercase a-f) |
X | hexadecimal (uppercase A-F) |
Regular Expressions | |
.. - ... | any character between .. and ... in computer alphabet |
. | any character |
? | the previous test is optional |
[:alnum:] | a-z, A-Z or 0-9 |
[:alpha:] | a-z or A-Z |
[:digit:] | 0-9 |
[:lower:] | a-z |
[:print:] | all normal (printable) characters including space |
[:punct:] | all characters except a-z, A-Z, 0-9 and space |
[:space;] | space |
[:upper:] | A-Z |
Cookies should be avoided whenever possible for many reasons including these...
- Cookies have been misused in the past and web users are gradually becoming aware of this
- Cookies are not secure, hence totally inappropriate for log-in, shopping carts etc.
- If the Cookie data is that important, store it securely - don't leave it on show for every user of that PC
- Cookies can alienate customers (whilst their use MAY be reasonable, some customers will doubt this)
- Cookies can be blocked by web users. They are more likely to change web-sites than to alter cookie settings
- When browsers detect 3rd Party cookies, customers may get the impression that you have a total disrespect for their personal data
- If you must use cookies, please tell you readers PRECISELY what information you are keeping on their computer and indicate why you need each piece of information
DigitalDan.uk is part of the DigitalDan.co.uk group