How can PHP scripts be written to ensure proper file creation permissions like chown and chmod?

When creating files using PHP scripts, it's important to set proper file permissions using functions like `chown` and `chmod` to control who can access or modify the files. To ensure proper file creation permissions, you can use these functions after creating the file to set the desired ownership and permissions.

// Create a new file
$file = fopen("example.txt", "w");

// Set the owner of the file
chown("example.txt", "username");

// Set the permissions of the file
chmod("example.txt", 0644);