Will Hunter Posted April 24, 2020 Share Posted April 24, 2020 Hello. I am currently editing my register.php file, which creates the user's account, but also at the same time makes a new file - their profile page. Here is the function: // Create profile page. $handle = fopen($username.".php", 'w'); $pageContent = ""; function createProfile() { fwrite($handle, $pageContent); } createProfile(); This function works, but not when I put this code inside -> https://pastebin.com/9j3rR12v. I have tried to swap all of the " for ' but it doesn't work. What happens is the register.php page does not load at all. It says there is an error. However, it loads the register.php page when I put: // Create profile page. $profileContents = include (profileContents.txt); $handle = fopen($username.".php", 'w'); $pageContent = "<!DOCTYPE html> <head><title>The title has changed.</title></head>"; function createProfile() { fwrite($handle, $profileContents); } createProfile(); When I then make an account, it creates the file, but the content doesn't go inside. A link to the code I would like to be displayed can be found here (compressed version): https://pastebin.com/9j3rR12v If you are able to help me out, it would mean the world. Many thanks. Emergency email: whunter@voltboxgaming.co.uk Link to comment Share on other sites More sharing options...
Ingolme Posted April 24, 2020 Share Posted April 24, 2020 When you wrap the code inside a function, it loses access to all of the variables that are outside of the function. You need to pass them into the function as function arguments. Link to comment Share on other sites More sharing options...
Will Hunter Posted April 25, 2020 Author Share Posted April 25, 2020 That partially worked. // Create profile page. function createProfile() { $handle = fopen($username.".php", 'w'); $pageContent = "<!DOCTYPE html> <head><title>The title has changed.</title></head>"; fwrite($handle, $pageContent); } createProfile(); ^ This code put the content in the page, but didn't name the file with the variable $username. Link to comment Share on other sites More sharing options...
Ingolme Posted April 25, 2020 Share Posted April 25, 2020 There's no variable $username inside the function. Link to comment Share on other sites More sharing options...
Will Hunter Posted April 26, 2020 Author Share Posted April 26, 2020 So I would only need to redefine the variable inside the function? Link to comment Share on other sites More sharing options...
Ingolme Posted April 26, 2020 Share Posted April 26, 2020 Code inside a function only has access to the function's arguments and variables that were declared inside the function. Did you really need to wrap your code in a function? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now