Useful PHP Functions
These are three of my favorite PHP functions. π
string file_get_contents ( string $filename [, int $flags [, resource $context [, int $offset [, int $maxlen ]]]] ) – Get the contents of a url.
Example:
// following code will display the contents of the web page http://www.example.com $url = “http://www.example.com”; $contents = file_get_contents($url); echo $contents; |
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) – Requires an email server. This functions programatically sends an email based on the given parameters.
Example:
// send an email to someone@somedomain.com $to=”someone@somedomain.com $subj = “example”; $message=”this is an email”; if(mail($to, $subj, $message)) echo “Email was sent”; else echo “Email sending failed.”; |
int fwrite ( resource $handle , string $string [, int $length ] ) – Write something to a file. Note that the file and it’s parent directory should have permissions granted to a user such as “apache” or “www-data”.
Example:
// open a file called test.txt and if it does not exist, attempt to create it; place the pointer at the begining of the file $handle = fopen(“test.txt”, “a+”); $data = “I am programatically written.\n”; fwrite($handle, $data); fclose($handle); |
I’ll stick with these 3 functions for the meantime. With this though, you can make programs such as:
- A simple email interface. π
- A logging system using just the last function
- A proxy browser or something.