0

im new to php and having some trouble with parameter passing in functions. I can't get my function to execute properly. This is the function.

     function validateUser($username){

          if(!empty($_POST)) 
          { 

              if(strlen($_POST['username']) < 5) 
              { 

                 die("Please enter a valid username"); 

              } 
}

thanks

3
  • 2
    your function need one more bracket } maybe. Or, what did you get exactly ?? Commented May 19, 2013 at 15:52
  • You can see what comes to your script in the $_POST variable by executing echo "<pre>"; print_r($_POST); echo "</pre>"; Commented May 19, 2013 at 15:52
  • 1
    Your code is a bit strange because you pass user name to function in parameter named $username but inside of the function you don't use this parameter and validate what you have in $_POST['username'] Commented May 19, 2013 at 15:53

2 Answers 2

1

The parameter of the function is what you pass to the function when you call it. You then access it within your function like any other variable.

function validateUser($username){
    if(strlen($username) < 5) 
    { 
        die("Please enter a valid username"); 
    } 
}

You then would call it using the value you want to validate like this:

validateUser($_POST['username']);

Tip: Don't use die() in your functions. Have them return true or false and then have the code that calls it decide how to handle the error.

Sign up to request clarification or add additional context in comments.

Comments

1

The parameters ain't passed in a POST variable. The way you formed your parameters when defining the function is the way you use them.

function validateUser($username)
{
    if(strlen($username) < 5) 
    { 
        return 0;
    } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.