TechnoDiver Posted May 31, 2021 Share Posted May 31, 2021 I've made this code for a very simple registration form practice: //assign form variables $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $username = $_POST['username']; function clean_names($dirty_name) { strip_tags($dirty_name); str_replace(' ', '', $dirty_name); ucfirst(strtolower($dirty_name)); //return $dirty_name; } $names = array($firstname, $lastname, $username); if(isset($_POST['register_button'])) { // WHY IS THIS FUNC CALL NOT WORKING?? foreach($names as $name) { clean_names($name); } echo "First Name: " . $firstname . "<br>"; echo "Last Name: " . $lastname . "<br>"; echo "Username: " . $username . "<br>"; } The values are returned but they haven't been put through the clean_names function. I'm really new with PHP. Can someone tell me why this isn't working? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/ Share on other sites More sharing options...
gw1500se Posted May 31, 2021 Share Posted May 31, 2021 How do yo know it is not going through that function, since it returns nothing? Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586929 Share on other sites More sharing options...
Barand Posted May 31, 2021 Share Posted May 31, 2021 When you pass a variable to a function it passes a copy of that variable. You are changing those copies. You need to return the cleaned value and assign that returned value to the variable. $firstname = clean_name($firstname) Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586930 Share on other sites More sharing options...
requinix Posted June 1, 2021 Share Posted June 1, 2021 ...and the same applies to other functions, like strip_tags and str_replace and strtolower and ucfirst. They return new values and do not modify the variables you gave them. Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586936 Share on other sites More sharing options...
TechnoDiver Posted June 1, 2021 Author Share Posted June 1, 2021 Ok, thanks for the replies. The quoted out return in the function was a mistake from editing the copy/paste that I didn't catch before posting, it's not really in the code. Concerning how functions return values, that means that theoretically the following amended code should work: function clean_names($dirty_name) { $dirty_name = strip_tags($dirty_name); $dirty_name = str_replace(' ', '', $dirty_name); $dirty_name = ucfirst(strtolower($dirty_name)); return $dirty_name; } $names = array($firstname, $lastname, $username); if(isset($_POST['register_button'])) { // WHY IS THIS FUNC CALL NOT WORKING?? foreach($names as $name) { $name = clean_names($name); } echo "First Name: " . $firstname . "<br>"; echo "Last Name: " . $lastname . "<br>"; echo "Username: " . $username . "<br>"; } To my inexperienced eye it looks like it should work. It seems so simple but it's not working. I feel like it's some small, stupid oversight from me. I've watched 2 videos and skimmed the functions sections of 3 tuts hoping to get a cue that would open my eyes to the issue. I didn't. What's the issue here? Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586946 Share on other sites More sharing options...
gw1500se Posted June 1, 2021 Share Posted June 1, 2021 Poor programing practice. The funtion should be: function clean_names($dirty_name) { $clean_name = strip_tags($dirty_name); $clean_name = str_replace(' ', '', $clean_name); $clean_name = ucfirst(strtolower($clean_name)); return $clean_name; } Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586947 Share on other sites More sharing options...
requinix Posted June 1, 2021 Share Posted June 1, 2021 Just because $name came from $names which is an array made up from those three variables, does not mean that you can modify $name and have that "work its way" back to the original variables. If you want to clean the $firstname, $lastname, and $username variables then you should clean the $firstname, $lastname, and $username variables. Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586949 Share on other sites More sharing options...
TechnoDiver Posted June 1, 2021 Author Share Posted June 1, 2021 12 minutes ago, gw1500se said: Poor programing practice. The funtion should be: function clean_names($dirty_name) { $clean_name = strip_tags($dirty_name); $clean_name = str_replace(' ', '', $clean_name); $clean_name = ucfirst(strtolower($clean_name)); return $clean_name; } gw1500se, thank you, you've helped me think like a better programmer 5 minutes ago, requinix said: Just because $name came from $names which is an array made up from those three variables, does not mean that you can modify $name and have that "work its way" back to the original variables. This is making more sense to me. I was looking at this part and wondering how $name 'got into' $firstname, $lastname etc. and felt this was a weak point. 8 minutes ago, requinix said: If you want to clean the $firstname, $lastname, and $username variables then you should clean the $firstname, $lastname, and $username variables. I had this code initially (I'm very new at PHP): //registration form values // //first name // $firstname = strip_tags($_POST['firstname']); //remove html tags // $firstname = str_replace(' ', '', $firstname); //remove spaces // $firstname = ucfirst(strtolower($firstname)); // //last name // $lastname = strip_tags($_POST['lastname']); //remove html tags // $lastname = str_replace(' ', '', $lastname); //remove spaces // $lastname = ucfirst(strtolower($lastname)); // //user name // $username = strip_tags($_POST['username']); //remove html tags // $username = str_replace(' ', '', $username); //remove spaces // $username = ucfirst(strtolower($username)); but it's so redundant it was a prime candidate for a function. I want to become much better at coding and am really interested in exposing myself to different approaches to the same challenge. So how would you do this? What do you feel is the tightest, most efficient way of making this happen? Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586950 Share on other sites More sharing options...
Solution requinix Posted June 1, 2021 Solution Share Posted June 1, 2021 Right. And the function is a good decision to reduce duplication. What I mean is, this doesn't have to be much more complicated than just $firstname = clean_names($_POST['firstname']); $lastname = clean_names($_POST['lastname']); $username = clean_names($_POST['username']); Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586951 Share on other sites More sharing options...
TechnoDiver Posted June 1, 2021 Author Share Posted June 1, 2021 (edited) 8 minutes ago, requinix said: Right. And the function is a good decision to reduce duplication. What I mean is, this doesn't have to be much more complicated than just $firstname = clean_names($_POST['firstname']); $lastname = clean_names($_POST['lastname']); $username = clean_names($_POST['username']); That's really awesome, requinix. I would never have thought to do it that way. This thread really helped me think differently about things. I appreciate the help. I hope I'll be able to contribute knowledge here someday too. Until then I'm sure I'll have more questions. Thanks again Out of curiosity, how would you do it using a foreach loop. I mean, how would you have moved $name into $firstname, $lastname, $username with a foreach loop and a function? Edited June 1, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/312826-function-call-in-foreach/#findComment-1586952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.