Useful PHP Functions

These are three of my favorite PHP functions. :D

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 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. :D
  • A logging system using just the last function
  • A proxy browser or something.

2 Responses to “Useful PHP Functions”

  1. butzi Says:

    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.

  2. Xyldrae Diane Jacob Says:

    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. :D

Leave a Reply