*a constant parallactic paradox, sublime*

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.

2 responses

  1. 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

  2. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s