if you want to generate random characters, you can use the rand() function while setting the characters into an array. that is, you feed the characters (0-9, a-z and anything else you want) into an array
$characters = array("a", "b", "c" ... and so on)
then you use rand() to select an index number in the array
$first_index = rand(1, 100) (this is assuming you have 100 characters in the array)
then assign that character to a variable
$first_char = $characters[$first_index]
and so on for as many characters as you want in a password.
when you've generated enough characters, you can combine/concatenate them into a variable which will be the password
$random_pass = $first_char . $second_char...and so on
this is just way one of doing it, there are others which you may like better, such as using a counter inside a loop to count generated characters until you reach a predetermined number of characters, after which you combine them into a single password variable, or you may even "assemble" the password during each iteration, one character at a time.
now, as for encrypting the password, there are several ways to do this, you can hash it if you want a non-reversible encryption of it, or you can use a reversible encryption. both have their pros and cons. non-reversible can't be "hacked" because you can't take the hash and work out the password used; while reversible methods can be used to send a user back his/her password if they forget it.
using the md5() function to hash a password, it would look something like this
$pass_hash = md5($pass_cleartext)
using the sha1() function, you write it the same way
$pass_hash = sha1($pass_cleartext)
both md5() and sha1() are non-reversible methods.
however, if you want to learn more about reversible encryption with PHP, check out what the PHP manual has to say about the mcrypt() function at
http://www.php.net/manual/en/ref.mcrypt.php