1

Ok, so I just finished off a function for validating the firstname field on a form.

This function works correctly on its own.

But since I want to make this function re-usable for more than one website, I added an if statement for whether or not to use it. The following code explain this:

Related PHP code:

//Specify what form elements need validating:
$validateFirstname = true;

//array to store error messages
$mistakes = array();

if ($validateFirstname=true) {
    //Call first name validation function
    $firstname = '';
    if (!empty($_POST['firstname'])) {
        $firstname = mysql_real_escape_string(stripslashes(trim($_POST['firstname'])));
    }
    $firstname = validFirstname($firstname);
    if ($firstname === '') {
        $mistakes[] = 'Your first name is either empty or Enter only ALPHABET characters.';
    }
    function validFirstname($firstname) {
        if (!ctype_alpha(str_replace(' ', '', $firstname))) {
            return '';
        } else {
            return $firstname;
        }
    }
}

So without this if ($validateFirstname=true) the code runs fine, but the moment I add it; I get the following error message:

Fatal error: Call to undefined function validFirstname()

Are you not able to use functions in if statements at all in PHP? I'm fairly new to using them in this way.

1
  • This is not what you want to do: if ($validateFirstname=true) Commented Apr 20, 2013 at 17:30

3 Answers 3

4

Conditional functions (functions defined inside the conditions) must be defined before they are referred. Here's what manual says:

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.

So if you want to use it that way, you should put it either at the beginning of the if condition or outside the condition.

// Either:
if ($validateFirstname==true) {
    function validFirstname($firstname) {}
}

// Or, and I'd rather do it this way, because function is
// created during "compilation" phase
function validFirstname($firstname) {}
if ($validateFirstname==true) {
    // ...
}

Also not that function (even if created inside the condition) is pushed to global scope:

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

So once code is evaluated it doesn't matter if it's declared inside condition or intentionally in global scope.

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

1 Comment

Thanks for this @Vyktor! Makes complete sense now. :-)
2

Functions that are declared in a conditional context (like if body), you can only use after their declaration.

if ($validateFirstname == true) {
    //Call first name validation function
    function validFirstname($firstname) {
        // function body
    }
    // $firstname initialisation
    $firstname = validFirstname($firstname);
    // ...
}

(P.s.: changed $validateFirstname = true to $validateFirstname == true which should be what you want)

Comments

0
if($validateFirstname=true)

you are assigning the value "true" to $validateFirstname here you should use a "==" for comparison e.g

 if($validateFirstname==true)

that might help your "if" problem

Comments

Your Answer

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