You could try to execute a command line to the htaccess, something like this:
exec("./htpasswd -c $path.htpasswd $userid");
The thing is, this would make the command line ask for the password twice, and I don't know if all you have to do is perform this twice:
exec("$password");
I'm not sure. You can check out how the command line is likely to behave here:
http://www.webdeveloper.com/servers/servers_htaccess_magic.html
For more info, check out the manual on the exec() function, and its "See also" functions:
http://php.net/manual/function.exec.php