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.
|// following code will display the contents of the web page http://www.example.com
$url = “http://www.example.com”;
$contents = file_get_contents($url);
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.
|// send an email to firstname.lastname@example.org
$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”.
|// 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”;
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.
Instead of fwrite you could use string file_put_contents($filename, $data [, $flags [, $resource]]);
My favourite functions are str_replace, key_exists (array_key_exists) and strpos.
5. May 2008 at 15:39
your right! Thanks for the helpful comment! I’m also gonna post an article about my favorite string and array functions. str_replace is also one of them. 😀
6. May 2008 at 03:49