1

Hey all. I have a processForm function and a displayForm function. If there are missing form fields the processForm function returns an array of missing fields. This is all fine and dandy until I try to include this array into the displayForm function. Here's the problem:

If I don't do this:

displayForm($missingFields=array());

then my validateField function throws a warning that it is expecting the parameter to be an array. However, this overwrites the array returned by the processForm function.

I hope I'm clear. Thanks for any help.

Full Code:

if(isset($_POST['action']) && $_POST['action'] = "login")
{
    $messages = processForm();
}

processForm()

if($errorMessages)
{
     return array("errors" => $errorMessages, "missing" => $missingFields);
}
else 
{
    $_SESSION['user'] = $user;
    header("Location: index.php");
}

form.php

(!isLoggedIn())? displayForm($messages['errors']=array(),$messages['missing']=array()) : null;

These are the sections of the code I'm having trouble with.

Thanks again.

6
  • Please show us how you're using that code, the problem is not very clear at the moment. Commented Jan 28, 2011 at 5:06
  • OK I'll edit the above...few minutes... Commented Jan 28, 2011 at 5:07
  • I could be jumping the gun here by suggesting posting on codereview.stackexchange.com. It sounds like your trouble may be buried too deep in your code for a regular SO question. Otherwise, you'll have to be way more specific in showing the problem lines. Commented Jan 28, 2011 at 5:29
  • @Tablekin: also, just a suggestion for posting code examples on SO: you can write out all the code you want in a text editer such as Notepad++, select all text, hit tab once to insert proper indenting, then copy/paste into here so everything is automatically properly formatted. Commented Jan 28, 2011 at 5:36
  • @Phil Brown: yes apparently I was jumping the gun. Commented Jan 28, 2011 at 5:49

2 Answers 2

3

You don't set default argument values in the call, you set them in the signature, for example

function displayForm($arg1 = array()) {
    ...
}

When you write

displayForm($messages['errors']=array())

this is actually doing something like this

$messages['error'] = array(); // set $messages['error'] to an empty array
displayForm($messages['error']); // pass an empty array to displayForm

This is because in PHP, the return value from an assignment is the value assigned.

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

1 Comment

Thanks much. That was the problem. Cheers.
0

Why are you using this:

displayForm($messages['errors']=array(),$messages['missing']=array())

When you writing "$messages['errors']=array()", this is setting $messeges to blank array. So the parameter is blank. You can just write:

displayForm($messages['errors'],$messages['missing'])

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.