Jump to content

Function call in foreach()


Go to solution Solved by requinix,

Recommended Posts

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

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
  }

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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']);

 

Link to comment
Share on other sites

Posted (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 by TechnoDiver
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.